Jenkins - restarting a pipeline on a different node fails on 'git checkout' (git plugin)

589 Views Asked by At

There is a job that gets triggered on pull request creation or update. I do not have the permissions to edit the job itself. Sometimes it will fail due to network connection problems, timeouts, etc. In this case I'd like to be able to restart the build with the same parameters. However in case of a restart, the build fails when checking out. It also skips the merge step for some reason

Is it possible to force the git-plugin to 'start fresh' on every build through Jenkinsfile? I was hoping that 'CleanBeforeCheckout' extension would solve my problem, but unfortunately this did not help.

Now for the details. Here's what my checkout step looks like in Jenkinsfile:

    gitBranches = [[name: 'origin/pull-requests/**'], [name:"origin/$fromBranch"]]
    gitExtensions = [
        [
            $class: 'UserIdentity', 
            email: email, 
            name: name
        ],
        [
            $class: 'PreBuildMerge',
                options: 
                [
                    fastForwardMode: 'FF',
                    mergeRemote    : 'origin',
                    mergeTarget    : toBranch
                ]
        ],
        [
            $class: 'CleanBeforeCheckout', 
            deleteUntrackedNestedRepositories: false
        ]
    ]   
    println "Cloning repo with url '$gitUrl'"   
    checkout changelog: true, poll: true, scm: [
            $class                           : 'GitSCM',
            branches                         : gitBranches,
            doGenerateSubmoduleConfigurations: false,
            extensions                       : gitExtensions,
            gitTool                          : 'Default',
            submoduleCfg                     : [],
            userRemoteConfigs                : [[credentialsId: credentialsId, url: gitUrl]]
    ]

On the first run, the git checkout never fails. Here's what it looks like:

Selected Git installation does not exist. Using Default
The recommended git tool is: NONE
using credential <credential_id>
Cloning the remote Git repository
Cloning repository <repo_url>
 > git init /home/jenkins/agent/workspace/<workspace_id> # timeout=10
Fetching upstream changes from <repo_url>
 > git --version # timeout=10
 > git --version # 'git version 2.7.1'
using GIT_ASKPASS to set credentials 
 > git fetch --tags --progress <repo_url> +refs/heads/*:refs/remotes/origin/* # timeout=10
Avoid second fetch
Merging Revision <my_feature_hash> (origin/feature/my_feature) to origin/develop, UserMergeOptions{mergeRemote='origin', mergeTarget='develop', mergeStrategy='DEFAULT', fastForwardMode='FF'}
Checking out Revision <target_hash> (origin/develop)
Commit message: "Merge commit '<my_feature_hash>' into HEAD"
First time build. Skipping changelog.
 > git config remote.origin.url <repo_url> # timeout=10
 > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
   <...>
Seen 27 remote branches
 > git show-ref --tags -d # timeout=10
 > git rev-parse origin/develop^{commit} # timeout=10
 > git config core.sparsecheckout # timeout=10
 > git checkout -f origin/develop # timeout=10
 > git remote # timeout=10
 > git config --get remote.origin.url # timeout=10
using GIT_ASKPASS to set credentials 
 > git merge --ff <my_feature_hash> # timeout=10
 > git rev-parse HEAD^{commit} # timeout=10
    <...>
Seen 27 remote branches
 > git show-ref --tags -d # timeout=10
 > git config core.sparsecheckout # timeout=10
 > git checkout -f <target_hash> # timeout=10

However, when restarting the build from the Jenkins UI, the job fails with such output:

Selected Git installation does not exist. Using Default
The recommended git tool is: NONE
using credential <credential id>
Cloning the remote Git repository
Cloning repository <repository_url>
 > git init /home/jenkins/agent/workspace/<workspace_id> # timeout=10
Fetching upstream changes <repository_url>
 > git --version # timeout=10
 > git --version # 'git version 2.7.1'
using GIT_ASKPASS to set credentials 
 > git fetch --tags --progress <repository_url> +refs/heads/*:refs/remotes/origin/* # timeout=10
Avoid second fetch
Checking out Revision <target_hash> (origin/develop)
 > git config remote.origin.url <repository_url> # timeout=10
 > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
   <...>
Seen 27 remote branches
 > git show-ref --tags -d # timeout=10
 > git config core.sparsecheckout # timeout=10
 > git checkout -f <target_hash> # timeout=10
Could not checkout <target_hash>

The problem supposedly arises because the git-plugin saves the previous build state somehow. This is bad because my builds will run on different nodes every time.

0

There are 0 best solutions below