首页 > 解决方案 > 在 Jenkins 管道中不起作用凭证

问题描述

(method: username and password)在 Jenkins 中使用 name创建了凭据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

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

标签: jenkinsgroovyjenkins-pipelinejenkins-groovy

解决方案


查看 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}"
                    '''
                }
            }
        }
    }
}

推荐阅读