[Jenkins Pipeline Shell] Groovy变量BUILD_USER如何传递给Shell

通过Groovy获取BUILD_USER,如何传递给Shell

解决办法:参考 https://stackoverflow.com/questions/41553303/pass-groovy-variable-to-shell-script

以前是这样的,不管用:

${BUILD_USER}

 

改成下面这样就可以了:

'''+BUILD_USER+'''

 

 

pipeline {
    agent {
        node {
            label 'awsjenklinux && !awsjenklinux1'
        }
    }
    
    environment {
        BUILD_USER = ""
    }
    
    parameters {
        string(name: 'CodeBranch', defaultValue: 'master')
        string(name: 'AwsAccountId', defaultValue: '000000')
        string(name: 'AwsRoleName', defaultValue: 'TESTTESTTEST')
        string(name: 'MailTo', defaultValue: 'TEST@SERVER.com')
        choice(name: 'Region', choices:'CH\nCA')
        choice(name: 'TestEnv', choices:'UAT\nPROD\nPROD1Set\nSTG')
    }

    stages {
        stage('Run JMeter Script') {
            steps {
                 // Get BUILD_USER via User Build var plugin
                wrap([$class:'BuildUser']){
                    script{
                        BUILD_USER = "${env.BUILD_USER}"
                    }
                }
                echo "Build User: ${BUILD_USER}"

                git url: 'https://msstash.TEST.com/scm/auto/automation-test-jmeter.git', branch: '${CodeBranch}'
                
                sh '''
                    set +x                    
                    resp=$(aws sts assume-role --role-arn "arn:aws:iam::${AwsAccountId}:role/${AwsRoleName}" --duration-seconds 3600 --role-session-name "$(basename ${PWD:0:60})")
                    unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN
                    export AWS_ACCESS_KEY_ID=$(echo $resp | jq -r .Credentials.AccessKeyId )
                    export AWS_SECRET_ACCESS_KEY=$(echo $resp | jq -r .Credentials.SecretAccessKey )
                    export AWS_SESSION_TOKEN=$(echo $resp | jq -r .Credentials.SessionToken )

                    aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 00000000.dkr.ecr.us-east-1.amazonaws.com
                    docker build --build-arg REGION=${Region} --build-arg TEST_ENV=${TestEnv} --build-arg MAIL_TO=${MailTo} --build-arg JENKINS_BUILD_URL=$BUILD_URL --build-arg TRIGGER_BY="'''+BUILD_USER+'''" .
                '''
            }
        }
    }
}

  

posted on 2022-05-03 15:25  张缤分  阅读(848)  评论(0编辑  收藏  举报

导航