In Jenkins how do I properly set up a scm object and provide meta information on the build overview page?

Viewed 63

In a Jenkins / git / Gerrit setup I currently maintain a bunch of "pipeline-scm" jobs: e.g. one of them gets triggered by Gerrit on an incoming change request in order to validate this change. There is a "Gerrit Trigger" plugin for Jenkins that provides that provides job with a git refspec and the branch that change relates to - among lots of other details. When you setup a "Pipeline script from SCM" a job instance (build) will be provided with an scm object which can be used to checkout the change and also gets used by Jenkins itself to show some meta information on the build's overview, e.g. the commits that define this change:

enter image description here

Now here comes my problem:

For [..] reasons I need to turn this "Pipeline script from SCM" job into a "Pipeline script" job. I.e. instead of just using the auto-generated scm object (which is being used on the master node already to fetch the pipeline script (and most likely to utilize the meta information about this change)) to run checkout scm in my pipeline script I now have to create this object manually:

scm = [
    $class: "GitSCM",
    userRemoteConfigs: [[
        credentialsId: <SOME ID>,
        url: <REPO-URL>,
    ]],
    branches: [[
        name: <BRANCH-NAME>,
    ]],
]

checkout scm

This raises two questions:

  1. I know the Gerrit plugin provides details about a change in environment variables like GERRIT_PATCHSET_REVISION, GERRIT_REFSPEC and GERRIT_BRANCH - but how do I use them? I've seen examples which define branches using GERRIT_BRANCH, others use GERRIT_PATCHSET_REVISION others use a branch pattern like */master.

  2. how does the meta information which can be extracted from a valid scm instance get to the build's overview page? I guess the pipeline-scm mechanism makes the master node populate this page automatically, but now the master node has no scm object in advance anymore. Is there a mechanism like "currentBuild.updateMetaInfo(scm)`?

Is there any documentation about how to properly set up and use a scm object (as Jenkins does) in a standalone pipeline job (i.e. without the scm mechanism)?

Update: This scm configuration works for me in terms of checkout:

scm = [
    $class: "GitSCM",
    userRemoteConfigs: [[
        credentialsId: <MY_ID>,
        url: <MY_REPO_URL>,
        refspec: env["GERRIT_REFSPEC],
    ]],
    branches: [[name: "FETCH_HEAD"]],
];

Unfortunately even after checkout scm no "Changes" show up, instead the overview page only displays this:

enter image description here

The build matrix in the job overview also states there are no changes: enter image description here

1 Answers

Regarding your first question on how to use the GERRIT_ variables with the Jenkins Git SCM, check the docs:

To get the Git Plugin to download your change; set Refspec to $GERRIT_REFSPEC and the Choosing strategy to Gerrit Trigger. This may be under ''Additional Behaviours/Strategy For Choosing What To Build' rather than directly visible as depicted in the screenshot. You may also need to set 'Branches to build' to $GERRIT_BRANCH. If this does not work for you set Refspec to refs/changes/:refs/changes/* and 'Branches to build' to *$GERRIT_REFSPEC.

Regarding the second one:

The checkout step should add the information to the build status page.

Regarding your closing question:

The scm object is just a convenience object that represents the scm you would configure in Pipeline from SCM, so you dont need to provide the details specified there in the Pipeline script again.

To generate a checkout step for a Pipeline without SCM, use the Snippet Generator from the Pipeline Syntax page.

Related