pipeline 语法实例一

半城伤御伤魂 提交于 2020-08-13 20:19:25

pipeline 语法实例一

Maven 编译 java 工程

/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*

#!groovy
timestamps {
group_id = "www.jenkins-ci.cn"
artifact_id = "process-agent"
version = ""
packaging = "tar.gz"
artifact_description = "process-agent"
group_id_path = group_id.replaceAll("\\.", "/")
nexus_encdata_ci_url = "http://110.139.139.153:18090/service/local/repositories/jenkins-data-ci/content"


node("master") {
    remote_rootdir = WORKSPACE.replaceAll("/workspace/${JOB_BASE_NAME}", "")
    
    stage("CheckOut") {
        git_repo_url = "ssh://git@110.139.139.118:12020/${repo_name}"
        //checkout([$class: 'GitSCM', branches: [[name: "*/${branch_name}"]], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: "${working_dir}"], [$class: 'CheckoutOption', timeout: 100]], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'gitlab-10.7.0', url: "${git_repo_url}"]]])
        //checkout([$class: 'GitSCM', branches: [[name: "*/${branch_name}"]], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: "${working_dir}"], [$class: 'CheckoutOption', timeout: 100]], submoduleCfg: [], userRemoteConfigs: [[url: "${git_repo_url}"]]])
        
        sh """if [ -d ${working_dir}/.git ]; then
            cd ${working_dir};
            current_branch=\$(git branch |sed -e "s/\\* //g");
            if [ \"\${current_branch}\" != \"${branch_name}\" ]; then
                git checkout -f .;
                git checkout -b ${branch_name};
                git pull ${git_repo_url} ${branch_name};
            else
                git checkout -f .;
                git pull ${git_repo_url} ${branch_name};
            fi    
        else
            git clone ${git_repo_url} -b ${branch_name} ${working_dir};
        fi"""
        
    }
   
    stage("Build") {
        sh "cd ${working_dir} && mvn -B -e -Dmaven.test.skip=true clean compile package"
        //sh "cd ${working_dir} && mvn -B -e -Dmaven.test.skip=true clean compile package -Ddeploy.encrypt -Dencrypt.tool.dir=/usr/local/jar-encrypt"
    }
    
    stage("SonarQube") {
        //sh "cd ${working_dir} && pwd " //mvn sonar:sonar -Dsonar.projectKey=process-agent -Dsonar.host.url=http://110.139.139.153:19000 -Dsonar.login=19a2f5971ec8ca982282505675f4ace19a2f59c636"
        //sh "cd ${working_dir} && cd process-agent && mvn -B -Dmaven.test.skip=true clean package"
        
    }
    
    stage("Install NexusOSS") {
        //sh "cd ${working_dir} && pwd && cd process-agent/target && mvn deploy:deploy-file -DgroupId=com.jenkins-ci -DartifactId=process-agent -Dversion=1.0.1-SNAPSHOT -Dpackaging=jar -Dfile=/home/jenkins/workspace/pipeline-process-agent-CI/process-agent/process-agent-core/target/data-engine.jar -Durl=http://110.139.139.153:18090/nexus/content/repositories/snapshots/ -DrepositoryId=snapshots"
        //sh "cd ${working_dir} && cd process-agent && mvn -B -Dmaven.test.skip=true clean install"
        sh "ssh root@110.119.151.134 'cp -rf /mnt/data/deploy/process-agent /mnt/data/deploy/scm-backup/'"
        sleep 1
        sh "ssh root@110.119.151.134 'rm -rf /mnt/data/deploy/scmdeploy/process-agent*.tar.gz'"
    }
    
    stage("Test Verification") {
        sh "cd ${working_dir} && scp -r process-agent/target/process-agentl-*.tar.gz root@110.119.151.134:/mnt/data/deploy/scmdeploy"
        
        sh "ssh root@110.119.151.134 'cd /mnt/data/deploy/scmdeploy && tar -xvf process-agent-*.tar.gz'"
        sh "ssh root@110.119.151.134 rm -rf /mnt/data/deploy/process-agent && pwd"
        sh "ssh root@110.119.151.134 'mv /mnt/data/deploy/scmdeploy/process-agent /mnt/data/deploy'"
        sh "ssh root@110.119.151.134 'cd /mnt/data/deploy/process-agent && sh start.sh start'"
    }
   
    stage("Release") {
        //sh "cd ${working_dir} && pwd && cd process-agent/target && scp data-engine.jar root@110.119.151.141:/mnt/data/deploy/scmdeploy"
        //sh "cd ${working_dir} && pwd && mvn deploy:deploy-file -DgroupId=com.jenkins-ci -DartifactId=process-agent -Dversion=1.0-SNAPSHOT -Dpackaging=jar -Dfile=/home/jenkins/workspace/process-agent-CI/BASE-COMPONENTS/target/web-bcm.jar -Durl=http://110.139.139.153:18090/nexus/content/repositories/snapshots/ -DrepositoryId=snapshots"
        //sh "cd ${working_dir} && cd process-agent && mvn -B -Dmaven.test.skip=true clean package"
    }
    
    stage("QualityReport") {
        //sh "cd ${working_dir} && pwd && cd /home/jenkins/sonar_script/python3 sonarqube3.py process-agent"
        //sh "cd ${working_dir} && pwd && mvn deploy:deploy-file -DgroupId=com.jenkins -DartifactId=process-agent -Dversion=1.0-SNAPSHOT -Dpackaging=jar -Dfile=/home/jenkins/workspace/process-agent-CI/BASE-COMPONENTS/target/web-bcm.jar -Durl=http://110.139.139.153:18090/nexus/content/repositories/snapshots/ -DrepositoryId=snapshots"
        //sh "cd ${working_dir} && cd process-agent && mvn -B -Dmaven.test.skip=true clean package"
    }
    /*post {
        always{
            emailext body:
                '''<body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4" offset="0">
                        <table width="95%" cellpadding="0" cellspacing="0"  style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">
                            <tr>
                                <td>
                                    <b><font color="#0B610B">Build Summary</font></b>
                                    <hr size="2" width="100%" align="center" />
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <ul>
                                        <li>Project Name: ${PROJECT_NAME}</li>
                                        <li>Build Number: # ${BUILD_NUMBER}</li>
                                        <li>Build Cause: ${CAUSE}</li>
                                        <li>Build Status: ${BUILD_STATUS}</li>
                                        <li>Build Log: <a href="${BUILD_URL}console">${BUILD_URL}console</a></li>
                                        <li>Build Url: <a href="${BUILD_URL}">${BUILD_URL}</a></li>
                                        <li>Project Url: <a href="${PROJECT_URL}">${PROJECT_URL}</a></li>
                                    </ul>
                                </td>
                            </tr>
                        </table>
                   </body>''',
            subject: '[Jenkins Build Notification] ${PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS}!',
            to: '12345@qq.com;',
            from: '12345@163.com'
        }
        success {
            //当此Pipeline成功时打印消息
            echo 'success'
        }
        failure {
            //当此Pipeline失败时打印消息
            echo 'failure'
        }
        unstable {
            //当此Pipeline 为不稳定时打印消息
            echo 'unstable'        
        }
        aborted {
            //当此Pipeline 终止时打印消息
            echo 'aborted'    
        }
        changed {
            //当pipeline的状态与上一次build状态不同时打印消息
            echo 'changed'            
        }        
    }*/
    
    /*post {
        success {
            emailext (
                subject: "SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",
                body: """<p>SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':</p>
                    <p>Check console output at "<a href="${env.BUILD_URL}">${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>"</p>""",
                to: "user1@qq.com,user2@qq.com",
                from: "admin@sina.com"
            )
        }
        failure {
            emailext (
                subject: "FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",
                body: """<p>FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':</p>
                    <p>Check console output at "<a href="${env.BUILD_URL}">${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>"</p>""",
                to: "user1@qq.com,user2@qq.com",
                from: "admin@sina.com"
            )
        }
    }*/
    
    /*stage("Upload artifact") {
        version = sh (
            script: 'date +%Y%m%d-%H%M%S',
            returnStdout: true
        ).trim()
        
        sh "cd ${working_dir} && cd 01bin/deployment && is_exist_file=\$(ls ${artifact_id}*.${packaging} |wc -l) && if [ \${is_exist_file} -ne 0 ]; then rm -f ${artifact_id}*.${packaging}; fi"
        sh "cd ${working_dir} && cd 01bin/deployment && tar -czvf ${artifact_id}-${version}.${packaging} MVS_${product_version}_Server"
        sh "cd ${working_dir} && cd 01bin/deployment && mv ${artifact_id}-${version}.${packaging} /tmp/ && /usr/bin/python ${remote_rootdir}/artifact_pull_push.py -g ${group_id} -a ${artifact_id} -v ${version} -p ${packaging} -P true -d '${artifact_description}'"
    }*/
    
    /* publishHTML (target: [
       allowMissing: false,
       alwaysLinkToLastBuild: false,
       keepAll: true,
       reportDir: 'coverage',
       reportFiles: 'index.html',
       reportName: "RCov Report"
      ]) /*
      post{
    success {
            script {
                if (sendmail == 'yes') {
           emailext body: '''<body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4"
    offset="0">
    <table width="95%" cellpadding="0" cellspacing="0"
        style="font-size: 11pt; font-family: Tahoma, Arial, Helvetica, sans-serif">
        <tr>
            <td><br />
            <b><font color="#0B610B">构建信息</font></b>
            <hr size="2" width="100%" align="center" /></td>
        </tr>
        <tr>
            <td>
                <ul>
                    <li>构建名称:${JOB_NAME}</li>
                    <li>构建结果: <span style="color:green"> ${BUILD_STATUS}</span></li>
                    <li>构建编号:${BUILD_NUMBER}  </li>
                    <li>GIT 地址:${git_url}</li>                    
                    <li>GIT 分支:${git_branch}</li>
                    <li>变更记录: ${CHANGES,showPaths=true,showDependencies=true,format="<pre><ul><li>提交ID: %r</li><li>提交人:%a</li><li>提交时间:%d</li><li>提交信息:%m</li><li>提交文件:<br />%p</li></ul></pre>",pathFormat="         %p <br />"}
                </ul>
            </td>
        </tr>
    </table>
</body>
</html>
''', subject: '${PROJECT_NAME}', to: 'aaa@163.com,'
                }
            }
        }
    }
    
    /*stage("jira issue") {
        if (issue_id != "") {
            withEnv(['JIRA_SITE=jira-7']) {
                echo "issue_id: ${issue_id}"
                
                //def fields = jiraGetFields idOrKey: "${issue_id}"
                //echo issue.data.toString()
                
                def issue = jiraGetIssue idOrKey: "${issue_id}"
                issue_type = issue.data.fields.issuetype.name
                issue_type_id = issue.data.fields.issuetype.id
                issue_status = issue.data.fields.status.name
                //echo issue.data.fields.issuetype.name
                //echo issue.data.fields.status.name
                
                if (issue_type == "任务" && issue_status == "已分配") {
                    def transitionInput = [transition: [id: '171']]  // accept
                    response = jiraTransitionIssue idOrKey: "${issue_id}", input: transitionInput
                    echo response.successful.toString()
                    echo response.data.toString()
                } else if (issue_type == "缺陷" && issue_status == "已分配") {
                    def transitionInput = [transition: [id: '171']]  // confirm
                    response = jiraTransitionIssue idOrKey: "${issue_id}", input: transitionInput
                    echo response.successful.toString()
                    echo response.data.toString()
                } else {
                    echo "当前 Issue 状态为:${issue_status}, 状态不作更新!"
                }
                
                // add artifact download link
                nexus_artifact_url = nexus_encdata_ci_url + "/" + group_id_path + "/" + artifact_id + "/" + version + "/" + artifact_id+"-"+version+"."+packaging
                //jiraAddComment idOrKey: "${issue_id}", comment: "发布包下载:${nexus_artifact_url}"
                
                project_key = issue_id.replaceAll("-.*", "")
                echo "project_key: ${project_key}"
                def project = jiraGetProject idOrKey: "${project_key}"
                //echo project.data.toString()
                project_id = project.data.id.toString()
                
                def editIssue = [fields: [ project: [id: "${project_id}"],
                                 customfield_10200: "${nexus_artifact_url}",
                                 customfield_10201: ["${product_version}"],
                                 issuetype: [id: "${issue_type_id}"]]]

                response = jiraEditIssue idOrKey: "${issue_id}", issue: editIssue
                issue_edit_response = response.successful.toString()
                echo "issue edit response: ${issue_edit_response}"
            }
        }
    }*/
    
}

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!