shell - 变量没有在 jenkins 管道的 sh 中填充

标签 shell variables jenkins curl jenkins-pipeline

当我在 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/

相关文章:

ruby - MacPorts 如何安装软件包?如何激活通过 MacPorts 完成的 Ruby 安装?

python - Jenkins 主/从窗口

当所有代理都离线时,Jenkins 管道队列已满

node.js - 使用 jenkins 在 EC2 上运行的 pm2 不起作用?

linux - 在 linux 中备份完成后编写删除备份文件的脚本

linux - shell脚本错误

shell - 如何使用脚本暂停后台 shell?

JavaScript getScript() 不起作用?

c - 在作用域的开头声明 C89 局部变量?

java - JSP - 将变量传递给 servlet