如何从Jenkinsfile触发组织扫描?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何从Jenkinsfile触发组织扫描?相关的知识,希望对你有一定的参考价值。

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

答案

您可以使用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"
                }
            }
        }
    }

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

以上是关于如何从Jenkinsfile触发组织扫描?的主要内容,如果未能解决你的问题,请参考以下文章

从jenkinsfile启动包含vault文件引用的Ansible playbook

如何在 Jenkins 声明式管道中设置声纳扫描仪

错误:在声明性 Jenkinsfile 中使用 Bash 脚本

如何设置 vscode 的代码片段,以便在自动完成后自动触发 vscode 的智能感知?

如何在本地测试对 Jenkinsfile 所做的更改?

在 Jenkins Pipeline/Jenkinsfile 中获取 git 分支名称