从jenkins pipeline传递一个值给shell脚本



如何在管道作业运行期间将值从jenkins传递给shell脚本。我有一个shell脚本,想要动态地传递这些值。

#!/usr/bin/env bash
....
/some code
....
export USER="" // <--- want to pass this value from pipeline
export password=""  //<---possibly as a secret

jenkins管道执行上面的shell脚本

node('abc'){
stage('build'){
sh "cd .."
sh "./script.sh"
}
}

您可以这样做:

pipeline {

agent any
environment {
USER_PASS_CREDS = credentials('user-pass')
}
stages {
stage('build') {
steps {
sh "cd .."
sh('./script.sh ${USER_PASS_CREDS_USR} ${USER_PASS_CREDS_PSW}')
}
}
}
}

credentials来自使用凭据API和凭据插件。你的另一个选择是凭据绑定插件,它允许你包括凭据作为构建步骤的一部分:

stage('build with creds') {
steps {
withCredentials([usernamePassword(credentialsId: 'user-pass', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
// available as an env variable, but will be masked if you try to print it out any which way
// note: single quotes prevent Groovy interpolation; expansion is by Bourne Shell, which is what you want
sh 'echo $PASSWORD'
// also available as a Groovy variable
echo USERNAME
// or inside double quotes for string interpolation
echo "username is $USERNAME"
sh('./script.sh $USERNAME $PASSWORD')
}
}
}

希望这对你有帮助。

最新更新