如果您想远程控制构建,请使用 Jerkins cli - 我发现它非常有用 http://jenkinshost:8080/cli
您需要正确配置 ssh 密钥,将运行 cli 的用户的公钥添加到要使用 Jenkins 用户配置在 Jenkins 中运行作业的用户(不在命令行上
测试密钥设置
java -jar jenkins=cli.jar -s http://jenkinshost:8080 who-am-i
这应该会报告哪个用户将用于在 Jenkins 中运行构建
但我认为您可以使用 Conditional Build Step 插件解决您的问题
https://wiki.jenkins-ci.org/display/JENKINS/Conditional+BuildStep+Plugin
这将允许您在构建步骤周围放置一个条件包装器,即
if branch==branchA then
trigger step - deploy to clusterA
if branch==branchB then
trigger step - deploy to clusterB
我个人觉得这个插件有点笨拙,它使工作配置页面有点混乱
我想出的另一个解决方案是始终调用子作业,然后让它决定它是否运行。
所以我在子作业开始时有一个脚本步骤来查看它是否应该运行
if [${branch}="Not the right branch name" ] ; then
echo "EXIT_GREEN"
exit 1
fi
你现在已经失败了这个作业,这会导致父作业变红,但是通过使用 Groovy Postbuild 插件https://wiki.jenkins-ci.org/display/JENKINS/Groovy+Postbuild+Plugin,你可以像这样添加一个 post build 步骤
if (manager.logContains(".*EXIT_GREEN.*")) {
manager.addBadge("info.gif","This job had nothing to do")
manager.build.@result = hudson.model.Result.SUCCESS
}
子作业已绿色运行(带有针对构建的信息图标),但实际上没有做任何事情。显然,如果分支是您要部署的分支,那么第一个脚本步骤不会运行 exit 1 并且作业会照常继续