简体   繁体   中英

How to trigger Organization Scan from Jenkinsfile?

Is there a way to trigger an Org Scan from a Jenkinsfile? We are using Jenkins 2.25 with a GitHub Branch Source Plugin

You can trigger the organisation folder job (which then scans for repos/branches) using the build step just like any other job. I implemented this for a multibranch job, but I assume it works the same for organisation folder jobs.

One thing to note is that currently (or at least 3 months ago when I implemented this), you can't wait for this job to be finished. If the remainder of your pipeline requires this, you need to work around a little bit. The usecase I have is that we push a new branch and then want to build that branch, so we trigger a scan, wait for the branch (=job) to appear and then finally trigger it.

// 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
    }
}

and this is how it can be used:

    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"
                }
            }
        }
    }

Hope this helps. If you provide more detail, answers could be a bit better tailored to you use case.

I was trying too to force a branch I created programmatically to "appear" in Jenkins.

This can be done manually by using the mentioned "Scan" button inside the path your branch belong, but, how to it programmatically?

Using pipeline build step plugin you can do:

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

for example in my case it was:

build wait: false, job:"myProject/myRepo"
sleep 30
build job: "myProject/myRepo/branchCreatedProgrammatically"

NB: The wait: false parameter when doing build on the path is required as you cannot wait for non job items.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM