Jenkins - 将 withCredentials 翻译成声明式语法

Jenkins - translate withCredentials into declarative style syntax

我有一个使用脚本语法编写的 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'
                    }
                )
            }
        }
     }
}

到目前为止,我已将相关凭据设置为环境变量,但由于在原始管道中,命令中未引用这些凭据,它们只是将命令环绕为 'withCredentials',我不确定如何达到相同的结果。知道怎么做吗?

首先看一下official documentation

您的案例管道如下所示:

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}"
            }
        }
    }
}
  Jenkinsfile (Declarative Pipeline)
   pipeline {
      agent any
        parameters {
string(name: 'STATEMENT', defaultValue: 'hello; ls /', description: 'What should I say?')
 }
  stages {
    stage('Example') {
     steps {
    /* CORRECT */
    sh('echo ${STATEMENT}')
      }
     }
    }
   }

visit here