如何从Jenkinsfile触发组织扫描?

问题描述 投票:1回答:1

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

jenkins jenkins-pipeline
1个回答
1
投票

您可以使用build步骤触发组织文件夹作业(然后扫描repos / branches),就像任何其他作业一样。我为多分支作业实现了这个,但我认为它对组织文件夹作业的工作原理相同。

有一点需要注意的是,目前(或者至少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"
                }
            }
        }
    }

希望这可以帮助。如果您提供更多详细信息,答案可能会更好地根据您的用例进行定制。

© www.soinside.com 2019 - 2024. All rights reserved.