git - Jenkins 中的条件代理
问题描述
我正在尝试在通过分支定义的 Jenkins 中设置条件代理,例如:
cloud = myconditional()
pipeline {
agent {
kubernetes {
cloud cloud
}
}
并且,该函数myconditional
在库中定义/vars/myconditional.groovy
def call() {
def cloud = "clusterB"
echo "Branch ${env.GIT_BRANCH}"
if ("${env.GIT_BRANCH}" != "master") {
echo "use clusterA"
cloud = "clusterA"
}else{
echo "use clusterB"
}
return cloud
...
但我明白了Branch null
。
其他使用方式scm
def getGitBranchName() {
return scm.branches[0].name
}
def call() {
def cloud = "clusterB"
def branch = getGitBranchName()
echo "Branch ${branch}"
if (branch != "master") {
echo "use clusterA"
cloud = "clusterA"
}else{
echo "use clusterB"
}
return cloud
但我明白org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use method hudson.plugins.git.BranchSpec getName
了:而且我无权更改 Jenkins 配置。
我尝试打印环境变量,但我得到了这个:
def call() {
sh 'env > env.txt'
sh 'cat env.txt'
....
我有:org.jenkinsci.plugins.workflow.steps.MissingContextVariableException: Required context class hudson.FilePath is missing
我的问题是,我如何才能将实际的分支从管道中取出{}?
非常感谢
解决方案
如果您使用的是 Jenkins Multibranch Pipeline,则可以使用以下变量获取分支名称。
env.BRANCH_NAME
您可以使用以下条件:
if ("${BRANCH_NAME}" != "master" ) {
...
} else {
...
}
推荐阅读
- python - Python 在类实例与本地(numpy)变量上的性能
- php - PHP 中的多维数组 - 如何使用 foreach 添加值
- javascript - 在一个 webpack 配置中构建 react 应用程序和小部件 js
- kubernetes-operator - 无法在“openshift-marketplace”之外安装操作符
- android - 这是检测当前活动片段的好方法吗?
- javascript - 我似乎无法修复的无效挂钩调用
- python - 如何更新窗口中显示的 matplotlib 图表?
- ruby - rbenv 上的 ruby 3.0.0 在哪里
- python - ModuleNotFoundError:bash 中没有名为“selenium”的模块
- php - Wordpress Woo Minicart 插件,带有针对不同语言的购物车字符串和购物车页面链接的 Polylang 问题