当我在 sh 脚本中执行 curl 时,我有以下代码和之前阶段的用户名、密码和模块名没有被填充。
请让我知道我需要修复它
def USERNAME
def PASSWORD
def MODULE_NAME
node {
try {
stage('userAuth') {
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: 'ID-Creds',
usernameVariable: 'user', passwordVariable: 'pwd']]) {
USERNAME="$user"
PASSWORD="$pwd"
echo "$USERNAME:$PASSWORD" //This is fine
}
}
stage('readPOM') {
def pom = readMavenPom file: 'pom.xml'
MODULE_NAME = pom.module
echo "$MODULE_NAME" //This is printing fine
}
stage('do curl') {
def revision = sh(script: '''
AUTH="$USERNAME:$PASSWORD"; //Not getting populated getting empty
RespInfo=$(curl -u $AUTH "https://host/apis/${MODULE_NAME}/deployments"); //Not getting populated getting empty for modulename
currntRev=$(jq -r .revision[0].name <<< "${RespInfo}");
echo $currntRev
''',returnStdout: true).split()
}
}
catch (e) {
throw e
} finally {
}
}
最佳答案
您必须使用双引号 ( """
) 来应用字符串插值:
def revision = sh(script: """
AUTH=\"$USERNAME:$PASSWORD\"; //Not getting populated getting empty
关于shell - 变量没有在 jenkins 管道的 sh 中填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57028622/