美文网首页
Jenkins的使用及需要注意的一些事

Jenkins的使用及需要注意的一些事

作者: sickle4j | 来源:发表于2020-09-03 13:33 被阅读0次

    首先新建一个工作流:

    image.png
    image.png

    然后配置新工作流的配置:

    1.General

    General是构建任务的一些基本配置。名称,描述之类的。

    image

    参数化构建:
    可以使用参数进行构建


    image.png

    通过和jira关联,触发jenkins构建:


    image.png

    最重要的pipeline脚本:

    pipeline {
        agent any
    
        environment {
           manual = 0
        }
    
        stages{
            stage('judge version') { //创建一个流程上的节点,节点的名字
                steps{
                    script{
                        //如果version有值说明是通过Jenkins手动启动 如果没有值说明是jira触发
                        if(version==""){
                           //版本号
                           def versions = "${version_id}"  //把上图中jenkins parameter中的参数传递过来
                           def newversions = "${versions.minus('[').minus(']')}" //因为传递过来的数据是带[]所以去除一下
                           //echo "issus_type   ${issus_type}"
                           if(newversions.indexOf(',') != -1){
                               newversions = newversions.substring(0,newversions.indexOf(','))
                           }
                           version = newversions
                           manual = 1
                        }
                    }
                    
                    echo "versionin :$version"
                }
            }
    //[https://jenkinsci.github.io/jira-steps-plugin/steps/user/jira_assignable_user_search/](https://jenkinsci.github.io/jira-steps-plugin/steps/user/jira_assignable_user_search/) jira 插件文档
            stage('change jira status'){ //创建一个新的数据流节点
                steps{
                    script{
                         if (manual==1){ //上一个节点如果是jira触发,manual就会为1
                          //withEnv to override the default site (or if there is not global site) 设置jira的环境,在系统管理中,系统设置中,JIRA Steps参数
                             withEnv(['JIRA_SITE=jirasite']) { 
    
                               def transitionInput =
                                  [
                                      transition: [
                                          id: '181'  //监听的jira的工作流号码
                                      ]
                                  ]
                                  jiraTransitionIssue idOrKey:"${JIRA_ISSUE_KEY}" , input: transitionInput
                             }
                        }
                    }
                }
            }
            stage('checkout repository') {
                steps{
                     git branch: "develop", credentialsId: 'token', url: 'github地址'
                }
            }
            // stage('build') {
            //     steps{
            //       sh 'chmod +x mvnw'
            //       sh './mvnw clean package -Pprod,zipkin -Dmaven.test.skip=true'
            //     }
            // }
            
            stage('analysis && build') {
                  steps{
                    withSonarQubeEnv('sonar') {
                        sh 'mvn clean package -Pprod,zipkin sonar:sonar -Dmaven.test.skip=true';
                    }
                  }
            }
              
            stage("Quality Gate") {
                steps {
                    script{
                        timeout(time: 5, unit: 'MINUTES') {
                        // waitForQualityGate abortPipeline: true
                        def qg = waitForQualityGate() // Reuse taskId previously collected by withSonarQubeEnv
                        if (qg.status != 'OK') {
                                
                            def props = readProperties  file: 'target/sonar/report-task.txt'
                               //发送给谁
                               mail to: 'xxx@qq.com',
                              //抄送?
                            cc: 'xxx@qq.com',
                            subject: "sonar门禁不达标",
                            body: "项目${props.projectKey}的sonar门禁不达标\n详情请看 ${props.dashboardUrl}"
                                
                            error "Pipeline aborted due to quality gate failure: ${qg.status}"
                        }
                    }
                }
    
                }
            }
            
    
            stage('create & push image to registry'){
                steps{
                    script {
                        //def str = 'sudo docker images | grep registry.ehuatai.com/dev_cs/customer | awk \'{print \$3 }\' | xargs sudo docker rmi'
                       //sh str
                      //sh(script: "sudo docker images|grep registry.ehuatai.com/dev_cs/customer|awk '{print \$3 }'|xargs sudo docker rmi")
                        version = "${version}.${BUILD_NUMBER}"
                        //使用自定义docker ,网上多是docker.withRegistry
                        withDockerRegistry(credentialsId: 'xxxxx',url:'www.baidu.com') {
                             def customImage = docker.build("仓库地址/包名:$version","./target/")
                             customImage.push()
                        }
                    }
                }
            }
            stage('deploy'){
            // replace right vars
                steps{
                    // run deploy scripts
                    script{
                      //deployments/customer  rancher中customer的服务
                      sh(script: "sudo /usr/local/bin/kubectl set image deployments/customer    customer=仓库地址/包名:$version -n eapp")
                    }
                }
            }
        }
        post{
            success{
                script {
                    if (manual==1){
                        withEnv(['JIRA_SITE=jirasite']) {
                            def transitionInput = [
                                transition: [ id: '161' ]
                            ]
                            jiraTransitionIssue idOrKey:"${JIRA_ISSUE_KEY}" , input: transitionInput
                        }
                    }
                }
            }
            failure{
                script {
                    if (manual==1){
                        withEnv(['JIRA_SITE=jirasite']) {
                            def transitionInput = [
                                transition: [id: '171']
                            ]
                            jiraTransitionIssue idOrKey:"${JIRA_ISSUE_KEY}" , input: transitionInput
                        }
                    }
                }
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:Jenkins的使用及需要注意的一些事

          本文链接:https://www.haomeiwen.com/subject/qazrjktx.html