JobDSL / git scm checkout does not cope correctly with env-vars

I have Job DSL definition:

pipelineJob("qanix-webhook-nodes") {
    parameters {
        stringParam('SCM_PROJECT', '', 'the project on git-server')
        stringParam('SCM_REPO', '', 'the repo-name on git-server')
        stringParam('SCM_BRANCH', '', 'the branch on git-server')
        stringParam('SCM_COMMIT', '', 'The commit to run the build for')
        stringParam('SCM_SHORT_COMMIT', '', 'First few characters of SCM_COMMIT above')
    }

    definition {
        cpsScm {
            scm {
                git {
                    remote {
                        url("ssh://git@some-git-server.org:1234/project/reponame.git")
                        credentials("git-ssh")
                    }
                    branch('${SCM_COMMIT}');
                }
                scriptPath(".jenkins-ci/some-pipeline.groovy")
            }
        }
    }
}

Documentation clear on how ${SCM_COMMIT} is supposed to work:

Yet when I run this, jenkins fails to checkout the commit by hash (see picture), even though it got the correct hash and the corresponding commit is on the remote git server.

If I switch out ${SCM_COMMIT} for ${SCM_BRANCH}, it works, but that is error prone, because for instance it checks out the old HEAD if there was a force-push and has other such issues.

I presume this is because you use branch() directive in your DSL.

Even if in your SCM_COMMIT variable/parameter you pass just the commit, 42…0e without anything before it, Jenkins still mistakes it for a branch name, and attemtps to use the refs/heads/syntax with it. This causes git to lookup the 42…0ebranch, which does not exist, and so it fails.

I don’t have a Jenkins instance at hand and might be mistaking this for some other part of DSL, but consult with your http://jenkins.url/plugin/job-dsl/api-viewer/index.html reference. I think in addition to the handy git{}shorthand there should be also something like gitSCM with Dynamic label – extremely verbose but more configurable, allowing you to use other directives than just branch(). Refer to http://jenkins.url/plugin/job-dsl/api-viewer/index.html#path/job-scm-gitSCM on your controller.

1 Like

If that code results in a Pipeline that has the git Pipeline step, then you’re facing a documented limitation of the git Pipeline step. The git step only supports checkout of branches. The documentation of the git step says:

the git step does not support:

  • SHA-1 checkout
  • Tag checkout
  • Submodule checkout
  • Sparse checkout
  • Large file checkout (LFS)
  • Reference repositories
  • Branch merges
  • Repository tagging
  • Custom refspecs
  • Timeout configuration
  • Changelog calculation against a non-default reference
  • Stale branch pruning

The checkout step supports all those checkout forms. Examples are included in the git plugin documentation

1 Like