打印詹金斯凭证ID

时间:2020-07-31 19:34:12

标签: jenkins-pipeline sh

有人知道我如何打印存储在詹金斯中的凭证吗? 下面的脚本引发:

Unexpected exception caught! groovy.lang.MissingPropertyException: No such property: crd for class: com.test.File

这是我的代码:

service.groovy

withCredentials([usernamePassword(
    credentialsId: vaultCredentialId, usernameVariable: 'ID', passwordVariable: 'CRED')]){


sh """
    crd=${CRED}
    for chars in `echo $crd | fold -w1`; do echo "value: $chars" ; done
"""

1 个答案:

答案 0 :(得分:2)

“ withCredentials”步骤将屏蔽与其机密匹配的所有输出,因此,如果要显示它们,则必须在该步骤之外进行。

pipeline {
    agent {
        label "master"
    }
    
    stages {
        stage("run") {
            steps {
                script {
                    userVar = null
                    passVar = null
                    withCredentials([usernamePassword(credentialsId: 'administrator-jenkins', passwordVariable: 'password', usernameVariable: 'username')]) {
                        userVar = username
                        passVar = password
                    }
                    echo "Username: ${userVar}"
                    echo "Password: ${passVar}"
                }
            }
        }
    }
}