【问题标题】:How can I set different Jenkins credentials depending on the git branch?如何根据 git 分支设置不同的 Jenkins 凭据?
【发布时间】:2022-01-19 02:01:06
【问题描述】:

我有一个 Jenkins Multibranch 项目,我需要根据我现在所在的 Git 分支设置一些凭据。例如:

# If I'm in master
MY_VARIABLE = credentials("master-credential")

# If I'm in develop
MY_VARIABLE = credentials("develop-credential")

# If I'm in QA
MY_VARIABLE = credentials("qa-credential")

现在,我尝试用不同的前缀命名我的变量,并按以下方式设置它们:

pipeline {
    agent any
    environment {
        MY_VARIABLE = credentials("${env.BRANCH_NAME}-credential")
    }
    stages {
        stage("Start") {
            steps {
                # use MY_VARIABLE on my steps
            }
        }

但它不起作用。

我想我可以为我的凭据使用不同的域,然后在设置它们时指定域,但我在文档中没有找到如何在 Jenkinsfile 上指定域。

如果有人可以帮助我,我将不胜感激。

谢谢!

【问题讨论】:

    标签: jenkins jenkins-pipeline jenkins-plugins jenkins-groovy


    【解决方案1】:

    虽然在environment 指令中使用credentials 的便利函数更简洁,但对于在credentialsId 中像这样解析的插值字符串,您需要使用withCredentials 插件和步骤方法。基于这个问题,我会假设你想要的类型绑定是string

    steps {
      withCredentials([string(credentialsId: "${env.BRANCH_NAME}-credential", variable: 'MY_VARIABLE')]) {
        # use MY_VARIABLE on my steps
      }
    }
    

    查看documentation了解更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-22
      • 1970-01-01
      • 2016-10-19
      • 2015-11-06
      • 1970-01-01
      • 2014-04-17
      • 2016-11-22
      相关资源
      最近更新 更多