无法在Jenkins管道中使用凭据

时间:2019-08-03 16:26:28

标签: jenkins groovy jenkins-pipeline jenkins-groovy

我已经在詹金斯(Jenkins)中创建了名为(method: username and password)的凭据wwe_hello。 为了进行测试,我创建了名称为test的管道:

pipeline {
    agent {label 'slave1'}
    environment {
        CREDS = credentials("wwe_hello")
    }      
    stages {
        stage('WWE') {

            steps {
                sh 'echo "$CREDS"'
            }
        }
    }
}

结果我有:

Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on slave1 in /var/lib/jenkins/workspace/test
[Pipeline] {
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: wwe_hello
Finished: FAILURE

在哪里,我有错误。我正在根据工作示例和文档进行所有操作。但是我不明白,为什么它不起作用。

1 个答案:

答案 0 :(得分:2)

查看withCredentials的管道步骤文档。您无需使用env创建环境变量-withCredentials会为您完成此操作:

pipeline {
    agent any
    stages {
        stage('only') {
            steps {
                withCredentials([
                    usernamePassword(
                        credentialsId: 'CRED', 
                        usernameVariable: 'USER', 
                        passwordVariable: 'PASS'
                        )]) {
                    sh '''
                        echo "The username is: ${USER}"
                        echo "The password is : ${PASS}"
                    '''
                }
            }
        }
    }
}