无法在管道脚本中替换Jenkins内部版本号变量

时间:2020-03-12 05:38:10

标签: variables jenkins environment-variables jenkins-pipeline sh

我有一个管道在尝试传递内部版本号。

但是,当我打印内部版本号时,它的打印效果很好,在将其传递到阶段内的shell ansible命令时遇到了问题。

pipeline {
    agent { label 'Prod_Slave' }
    stages {

        stage('Ansible stuff') {
            parallel {
                stage('APP') {

            steps {
                echo "${env.BUILD_NUMBER}"
                         println "${env.BUILD_NUMBER}"
                         sh 'ansible-playbook $ansible_playbook/site.yml -e Latest_Build_Number4=${env.BUILD_NUMBER}'
                    }
                }

}
}

}
}

输出:

[Pipeline] 
echo (hide) 
16 [Pipeline] 
echo 16 [Pipeline] 
echo 16

 /script.sh: line 1: Latest_Build_Number4=${env.BUILD_NUMBER}: bad
 substitution

当我更改为:

sh 'ansible-playbook $ansible_playbook/site.yml -e Latest_Build_Number4="${env.BUILD_NUMBER}"'

输出:

 @2@tmp/durable-5ed077ca/script.sh: line 1: ${env.BUILD_NUMBER}: bad
 substitution

最后尝试了这个:

sh 'ansible-playbook $ansible_playbook/site.yml -e Latest_Build_Number4="$env.BUILD_NUMBER"'

输出:

  -e Latest_Build_Number4=.BUILD_NUMBER

1 个答案:

答案 0 :(得分:1)

单引号不允许替换。尝试使用双引号将美元符号转义:

sh "ansible-playbook \$ansible_playbook/site.yml -e Latest_Build_Number4=${env.BUILD_NUMBER}"