Jenkins - 将 withCredentials 转换为声明式语法

fs3*_*314 2 jenkins jenkins-pipeline jenkins-declarative-pipeline

我有一个 jenkins 管道,它是使用脚本语法编写的,我需要使用声明式样式将其变成一个新管道。

这是我与 jenkins 的第一个项目,我一直在研究如何在声明性 jenkins 中翻译 withCredentials 语法。

原始(脚本化)管道如下所示:

stage('stage1') {
     steps {
        script {
            withCredentials([usernamePassword(credentialsId: CredentialsAWS, passwordVariable: 'AWS_SECRET_ACCESS_KEY', usernameVariable: 'AWS_ACCESS_KEY_ID')]) {
                parallel (
                    something: {
                         sh 'some commands where there is no reference to the above credentials'
                    }
                )
            }
        }
     }
}
Run Code Online (Sandbox Code Playgroud)

到目前为止,我已将有问题的凭据设置为环境变量,但由于在原始管道中这些未在命令中引用,但它们只是将命令包装为“withCredentials”,我不确定如何实现相同的结果。知道如何做到这一点吗?

Dmi*_*ich 7

首先看一下官方文档

对于您的案例管道将如下所示:

pipeline {
    agent any
    environment { 
        YOUR_CRED = credentials('CredentialsAWS') 
    }
    stages {
        stage('Call username and password from YOUR_CRED') {
            steps {
                echo "To call username use ${YOUR_CRED_USR}"
                echo "To call password use ${YOUR_CRED_PSW}"
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)