是否有一种方法可以从Jenkinsfile触发组织扫描? 我们正在将Jenkins 2.25与GitHub Branch Source插件一起使用
答案 0 :(得分:1)
您可以像其他任何作业一样,使用build
步骤来触发组织文件夹作业(然后扫描仓库/分支)。我为多分支作业实现了此功能,但我认为它对组织文件夹作业的作用相同。
要注意的一件事是,当前(或至少在3个月前实现此功能),您迫不及待要完成这项工作。如果流水线的其余部分需要这样做,则需要做一些工作。我的用例是,我们推送一个新分支,然后要构建该分支,因此我们触发扫描,等待该分支(= job)出现,然后最终触发它。
// 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..."
// the branch could be disabled, if it had existed before and got deleted. Probably this never occurs
// with real release branches, but might more likely occur when you touch this very file.
while (Jenkins.instance.getItemByFullName(job) == null || Jenkins.instance.getItemByFullName(job).isDisabled()) {
sleep 3
}
}
这是它的用法:
stage('Build Artifacts') {
steps {
// trigger branch indexing for "PRODUCT-build" job
echo 'Running branch indexing'
scanMultiBranchAndWaitForJob(buildProject, releaseBranchName(version))
// trigger "PRODUCT-build/release-1.2.0" job using the build step (wait until it finishes successfully)
echo "Triggering build for branch '${releaseBranchName(version)}'"
script {
// we accept UNSTABLE builds
buildResult = build job: "../${buildProject}/${releaseBranchName(version)}", propagate: false
if (!buildResult.result in ['SUCCESS', 'UNSTABLE']) {
error "Build failed"
}
}
}
}
希望这会有所帮助。如果您提供更多详细信息,则答案可能会更适合您的用例。
答案 1 :(得分:0)
我也试图强制我以编程方式创建的分支在 Jenkins 中“出现”。
这可以通过使用分支所属路径中提到的“扫描”按钮手动完成,但是,如何以编程方式完成?
使用 pipeline build step plugin 你可以:
build wait: false, job: {{ path where my branch is supposed to appear}} //Equivalent to clicking on "Scan" button
sleep 10 //Wait till above action is completed
build job: {{ my job full path }} //Build the job of the branch created programmatically
例如在我的情况下是:
build wait: false, job:"myProject/myRepo"
sleep 30
build job: "myProject/myRepo/branchCreatedProgrammatically"
注意:在路径上执行 wait: false
时需要 build
参数,因为您不能等待非作业项。