Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "Scan Repository Now" from a Jenkinsfile

Tags:

jenkins

I can call another jenkins job using the build command. Is there a way I can tell another job to do a branch scan?

A multibranch pipeline job has a UI button "Scan Repository Now". When you press this button, it will do a checkout of the configured SCM repository and detect all the branches and create subjobs for each branch.

I have a multibranch pipeline job for which I have selected the "Suppress automatic SCM triggering" option because I only want it to run when I call it from another job. Because this option is selected, the multibranch pipeline doesn't automatically detect when new branches are added to the repository. (If I click "Scan Repository Now" in the UI it will detect them.)

Essentially I have a multibranch pipeline job and I want to call it from another multibranch pipeline job that uses the same git repository.

node {
  if(env.BRANCH_NAME == "the-branch-I-want" && other_criteria) {
    //scanScm "../my-other-multibranch-job" <--- scanScm is a fake command I made up
    build "../my-other-multibranch-job/${env.BRANCH_NAME}"

I get an error on that build line, because the target multibranch pipeline job does not yet know that BRANCH_NAME exists. I need a way to trigger an SCM re-scan in the target job from this current job.

like image 558
Jonathan Lynch Avatar asked Dec 14 '22 13:12

Jonathan Lynch


1 Answers

Similar to what you figured out yourself, I can contribute my optimization that actually waits until the scan has finished (but is subject to Script Security):

// 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..."

    while (Jenkins.instance.getItemByFullName(job) == null || Jenkins.instance.getItemByFullName(job).isDisabled()) {
        sleep 3
    }
}
like image 79
StephenKing Avatar answered Dec 23 '22 18:12

StephenKing