1. 创建凭据
    Username with password 类型的凭据
  2. 流水线中使用
    #!groovy
    import org.devops.build
    import org.devops.utils
    def call(Map map) {
    
    def utils = new utils()
    def builder = new build()
    
    pipeline {
        agent any
        parameters {
            choice(name: '是否为紧急变更:', choices: ['否', '是'])
            choice(name: '紧急变更原因为:', choices: ['无', '上次版本失败', '正常紧急发版'])
        }
    
        environment {
            credentialsId = "gitlab_admin_token"
            nexusRawUsernamePassword = credentials('nexus3')
    //            artifact_directory = map.get('artifact_directory', "default")
    
            //maven
            POM_PATH = "${map.POM_PATH}"
            MAVEN_GOALS = "${map.MAVEN_GOALS}"
    
            ES_IP = "20.1.32.74:24100"
            pipeline_type = 'CI'
            runenv = 'pro'
        }
    
        options {
            timestamps()  //日志会有时间
            // skipDefaultCheckout()  //删除隐式checkout scm语句
            disableConcurrentBuilds() //禁止并行
            timeout(time: 1, unit: 'HOURS')  //流水线超时设置1h
        }
    
        stages {
            stage('拉取代码') {
                steps {
                    script {
                        utils.GetCodeBytag("${git_url}", "${credentialsId}")
                    }
                }
            }
            stage('编译构建') {
                steps {
                    script {
                        builder.mavenBuild("${POM_PATH}", "${MAVEN_GOALS}")
                    }
                }
            }
            stage('检索并提交制品库') {
                steps {
                    script {
                        def artifact_location = sh(returnStdout: true, script: '''
                            art_location=`find $WORKSPACE/target/ -maxdepth 2  -regextype 'posix-egrep' -regex  ".*.(jar|war)"|grep -v "original"`
                            echo ${art_location}
                             ''').trim()
                        env["artifact_location"] = artifact_location
    
                        def artifact_name = sh(returnStdout: true, script: '''
                            art_full_name=$(basename ${artifact_location})
                            art_name=${art_full_name:0:-4}
                            art_type=${art_full_name:0-4:4}
                            new_art_name=$art_name-${BUILD_ID}${art_type}
                            echo $new_art_name
                             ''').trim()
    
                        env["artifact_name"] = artifact_name
    
                        sh '''curl -s -i --user "${nexusRawUsernamePassword}" -X POST "http://xxxxx/service/rest/v1/components?repository=data" \
                            -H "accept: application/json" \
                            -H "Content-Type: multipart/form-data" \
                            -F "raw.directory=${artifact_directory}" \
                            -F "raw.asset1=@${artifact_location}" \
                            -F "raw.asset1.filename=${artifact_name}"'''
                    }
                }
            }
        }
    
        post {
            always {
                wrap([$class: 'BuildUser']) {
                    script {
                        currentBuild.description = manager.getEnvVariable("git_tag")
                        utils.NewPostoEs()
                    }
                }
            }
        }
    }
    }
    

发表评论

邮箱地址不会被公开。