groovy 未读取Jenkinsfile凭据作为参数

aamkag61  于 2023-01-16  发布在  Jenkins
关注(0)|答案(1)|浏览(161)

我正在尝试使用git克隆的credentials参数,但是我收到了变量找不到的错误
参数定义

credentials (credentialType: 'Username with password', defaultValue: 'fcb2d7c3-4b35-4ef2-bdf0-24fc4ff1137c', description: 'Git credential', name: 'git_credential', required: true)

阶段使用

withCredentials([usernamePassword(credentialsId: params.git_credential, passwordVariable: 'DOCKER_PASSWORD', usernameVariable: 'DOCKER_USERNAME')]) {
sh 'git reset --hard; git clone https://${DOCKER_USERNAME}:${DOCKER_PASSWORD}@repo_url/scm/${params.repository}.git --branch master'

错误:使用了错误的变量

vom3gejh

vom3gejh1#

pipeline {
  agent any
  parameters {
    // credentials : defaultValue is Credentials ID
    credentials(name: 'git_credential', description: 'Git credential', defaultValue: 'fcb2d7c3-4b35-4ef2-bdf0-24fc4ff1137c', credentialType: "Username with password", required: true )
  }
  stages {
    stage('Print') {
      steps {
        withCredentials([usernamePassword(
          credentialsId: '${git_credential}',
          usernameVariable: 'DOCKER_USERNAME',
          passwordVariable: 'DOCKER_PASSWORD',
        )]) {
          sh """
            echo id:${DOCKER_USERNAME} pw:${DOCKER_PASSWORD}
          """
        }
      }
    }
  }
}

这对我很有效
我希望这对你有帮助。

相关问题