Gitlab checkout.groovy fails after upgrade to 12.1.17 from 12.0.1

Viewed 48

In Jenkins i had an analysis job. The job used to checkout and build the merge request sent to the target branch. However, after upgrading the gitlab version from 12.0.1 to 12.1.17 i am unable to checkout source branch.

Below is the groovy script i was using.

#!/usr/bin/env groovy

def call() {
    if (env.gitlabMergeRequestId) {
        sh "echo '${env.gitlabMergeRequestId}'"
        sh "echo 'Merge request detected. Merging...'"
        def credentialsId = scm.userRemoteConfigs[0].credentialsId
        checkout ([
            $class: 'GitSCM',
            branches: [[name: "${env.gitlabSourceNamespace}/${env.gitlabSourceBranch}"]],
            extensions: [
                [$class: 'PruneStaleBranch'],
                [$class: 'CleanCheckout'],
                [
                    $class: 'PreBuildMerge',
                    options: [
                        fastForwardMode: 'NO_FF',
                        mergeRemote: env.gitlabTargetNamespace,
                        mergeTarget: env.gitlabTargetBranch
                    ]
                ]
            ],
            userRemoteConfigs: [
                [
                    credentialsId: credentialsId,
                    name: env.gitlabTargetNamespace,
                    url: env.gitlabTargetRepoHttpURL
                ],
                [
                    credentialsId: credentialsId,
                    name: env.gitlabSourceNamespace,
                    url: env.gitlabSourceRepoHttpURL
                ]
            ]
        ])
    } else {
        sh "echo 'No merge request detected. Checking out current branch'"
        checkout ([
            $class: 'GitSCM',
            branches: scm.branches,
            extensions: [
                    [$class: 'PruneStaleBranch'],
                    [$class: 'CleanCheckout']
            ],
            userRemoteConfigs: scm.userRemoteConfigs
        ])
    }
}
1 Answers

I was able to solve it by adding in the branches

branches: [[name: "refs/heads/${env.gitlabSourceBranch}"]]

Related