如何在Jenkins pipeline脚本中将shell脚本变量作为groovy变量访问

cwxwcias  于 2023-02-24  发布在  Shell
关注(0)|答案(1)|浏览(218)

我已经通过terraform从sh """"""生成了shell脚本中设置的SGID值的输出,但是我想在groovy步骤中再次获得shell变量,但是我得到了以下错误。

stages {
    stage('test') {
      steps {
        script {
              try {
                sh '''
                    cd SG
                    export TF_VAR_aws_vpc_id="${vpc_id}"
                    export TF_VAR_SysName="${sysname}"
                    export TF_VAR_name="${name}"
                    export TF_VAR_Env="${environment}"
                    export TF_VAR_TSM="${TSM}"
                    export TF_VAR_SysOwner="${Sysowner}"
                    export TF_VAR_RequestItem="${RITM}"
                    export TF_VAR_CostCenter="${cc}"
                    export TF_VAR_BusinessUnit="${bu}"
                    export TF_VAR_Department="${dept}"
                    terraform init -no-color
                    terraform validate -no-color
                    terraform plan -no-color
                    terraform apply -auto-approve 
                    terraform output 
                    export SGID=$(terraform output)
                    echo $SGID
                '''
              } catch (err) {
                    echo "Caught: ${err}"
                    sh '''
                        cd SG
                        terraform destroy -auto-approve
                        echo "Terraform destroyed"  
                    '''
                    currentBuild.result = 'Failure'
                    echo "RESULT: ${currentBuild.result}"
                }       
            })
        }}}
    }
        post {
        success{ script {
            result = sh (   
                script: "python snow-api.py '${SGID} has provisioned on params.Account at ${timestamp}'",   
                returnStdout: true
            ).trim()            
        }}
    }
}

输出为:

Error when executing success post condition:
groovy.lang.MissingPropertyException: No such property: SGID for class: groovy.lang.Binding
    at groovy.lang.Binding.getVariable(Binding.java:63)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetProperty(SandboxInterceptor.java:271)
    at org.kohsuke.groovy.sandbox.impl.Checker$7.call(Checker.java:353)
v64noz0r

v64noz0r1#

你不能用你正在尝试的方法去做。作为一种变通方法,你可以做一些类似下面的事情。

stages {
    stage('test') {
      steps {
        script {
              try {
                sh '''
                    cd SG
                    export TF_VAR_aws_vpc_id="${vpc_id}"
                    export TF_VAR_SysName="${sysname}"
                    export TF_VAR_name="${name}"
                    export TF_VAR_Env="${environment}"
                    export TF_VAR_TSM="${TSM}"
                    export TF_VAR_SysOwner="${Sysowner}"
                    export TF_VAR_RequestItem="${RITM}"
                    export TF_VAR_CostCenter="${cc}"
                    export TF_VAR_BusinessUnit="${bu}"
                    export TF_VAR_Department="${dept}"
                    terraform init -no-color
                    terraform validate -no-color
                    terraform plan -no-color
                    terraform apply -auto-approve 
                    terraform output 
                    export SGID=$(terraform output)
                    echo $SGID >> SGID_OUT
                '''
              } catch (err) {
                    echo "Caught: ${err}"
                    sh '''
                        cd SG
                        terraform destroy -auto-approve
                        echo "Terraform destroyed"  
                    '''
                    currentBuild.result = 'Failure'
                    echo "RESULT: ${currentBuild.result}"
                }       
            })
        }}}
    }
        post {
        success{ script {
            result = sh (   
                script: "SGID=\$(cat SGID_OUT); python snow-api.py ${SGID} 'has provisioned on params.Account at ${timestamp}'",   
                returnStdout: true
            ).trim()            
        }}
    }
}

在这里,我将SGID写入一个文件,并在post步骤中阅读它。

相关问题