gpt4 book ai didi

jenkins - 如何从 Jenkinsfile 触发组织扫描?

转载 作者:行者123 更新时间:2023-12-04 15:59:02 25 4
gpt4 key购买 nike

有没有办法从 Jenkinsfile 触发组织扫描?我们使用带有 GitHub 分支源插件的 Jenkins 2.25

最佳答案

您可以像任何其他作业一样使用 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"
}
}
}
}

希望这对您有所帮助。如果您提供更多详细信息,答案可能会更好地适合您的用例。

关于jenkins - 如何从 Jenkinsfile 触发组织扫描?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50977861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com