jenkins - 如何从 Jenkinsfile 中“立即扫描存储库”
问题描述
build
我可以使用该命令调用另一个 jenkins 作业。有没有办法可以告诉另一个工作进行分支扫描?
多分支管道作业有一个 UI 按钮“立即扫描存储库”。当您按下此按钮时,它将检查已配置的 SCM 存储库并检测所有分支并为每个分支创建子作业。
我有一个多分支管道作业,为此我选择了“抑制自动 SCM 触发”选项,因为我只希望它在我从另一个作业中调用它时运行。由于选择了此选项,多分支管道不会自动检测何时将新分支添加到存储库。(如果我在 UI 中单击“立即扫描存储库”,它将检测到它们。)
本质上,我有一个多分支管道作业,我想从另一个使用相同 git 存储库的多分支管道作业中调用它。
node {
if(env.BRANCH_NAME == "the-branch-I-want" && other_criteria) {
//scanScm "../my-other-multibranch-job" <--- scanScm is a fake command I made up
build "../my-other-multibranch-job/${env.BRANCH_NAME}"
我在那条线上得到一个错误build
,因为目标多分支管道作业还不知道它的BRANCH_NAME
存在。我需要一种方法来从当前作业触发目标作业中的 SCM 重新扫描。
解决方案
与您自己发现的类似,我可以贡献我的优化,实际上等待扫描完成(但受脚本安全性约束):
// Helper functions to trigger branch indexing for a certain multibranch project.
// The permissions that this needs are pretty evil.. but there's currently no other choice
//
// Required permissions:
// - method jenkins.model.Jenkins getItemByFullName java.lang.String
// - staticMethod jenkins.model.Jenkins getInstance
//
// See:
// https://github.com/jenkinsci/pipeline-build-step-plugin/blob/3ff14391fe27c8ee9ccea9ba1977131fe3b26dbe/src/main/java/org/jenkinsci/plugins/workflow/support/steps/build/BuildTriggerStepExecution.java#L66
// https://stackoverflow.com/questions/41579229/triggering-branch-indexing-on-multibranch-pipelines-jenkins-git
void scanMultiBranchAndWaitForJob(String multibranchProject, String branch) {
String job = "${multibranchProject}/${branch}"
// the `build` step does not support waiting for branch indexing (ComputedFolder job type),
// so we need some black magic to poll and wait until the expected job appears
build job: multibranchProject, wait: false
echo "Waiting for job '${job}' to appear..."
while (Jenkins.instance.getItemByFullName(job) == null || Jenkins.instance.getItemByFullName(job).isDisabled()) {
sleep 3
}
}
推荐阅读
- css - 滑块元素动画延迟
- python - 使用 Kabsch 算法进行 3d 最佳旋转
- html - 修复了 CSS 中的列布局
- angular - SpyOn SideNav 找不到给定的方法
- java - Drools 硬约束实现
- ios - 最新 Xcode 更新后无法将 NativeScript 部署到 iOS?
- powershell - 如何使用 powershell 访问 MTP 设备的内容?
- javascript - 在运行时反应从服务器导入图像
- apache-spark - NettyBlockTransferService 不尊重 spark.blockManager.port 配置
- c++ - 对象数组的构造函数中的随机数