jenkins - Jenkinsfile:如何根据分支名称参数化凭据 ID?
问题描述
我在我的 Jenkinsfile 中使用凭据插件,如下所示 -
stage("stage name"){
steps{
withCredentials([usernamePassword(credentialsId: 'credId', usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]){
sh'''
statement 1
statement 2
'''
}
}
}
根据新要求,我需要根据分支名称使用不同的凭据 ID。这意味着如果分支名称是 master,我应该使用 credentialsId:'mastercred' 而对于其他分支,我应该使用 credentialsId:'othercred'。“withCredentials”块中的代码将是相同的,唯一的变化将是凭据Id。
我不想重复代码。有没有办法参数化这个credentialsId?
解决方案
您可以读取分支名称变量,并将 acredentialId
变量设置为在 withCredentials 上使用即可。例如:
stage("stage name"){
steps{
if (env.BRANCH_NAME == "master"){
credentialId = "mastercred"
}else
credentialId = "othercred"
}
withCredentials([usernamePassword(credentialsId: "${credentialId}", usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]){
sh'''
statement 1
statement 2
'''
}
}
}
推荐阅读
- javascript - 尝试将 javascript 按钮转换为闪电组件
- javascript - 从当前时间减去时间
- c# - 使用 Azure 函数输出绑定附加到日志文件
- spring - JdbcMetadataStore:初始化模式,但对于不使用 Boot 的 Spring 应用程序
- google-apps-script - 我可以将我的应用脚本库共享到另一个帐户吗
- datatable - 我需要将ckeckbox数据表传递给控制器
- javascript - 一周完成后切换或隐藏/显示链接
- python - 将多个列表从 Python 传递到 PHP 数组?
- arrays - Groovy 遍历 Maps 的 Arraylist
- linux - 有没有办法在页面大小之外对齐 mmap()ed 内存?