groovy 在Jenkins管道中没有工作凭证

uhry853o  于 2022-11-01  发布在  Jenkins
关注(0)|答案(2)|浏览(207)

我在Jenkins中创建了名为wwe_hello的凭据(method: username and password)。为了测试,我创建了名为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

哪里,我有错误。我做的一切都根据工作的例子和文件。但我不明白,为什么它不工作。

fruv7luv

fruv7luv1#

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

相关问题