Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins pipeline with Git polling doesn't run

I have a Jenkins job with a branch parameter, set to poll SCM every 5 minutes, and run a pipeline script from SCM:

General and build triggers configuration

Pipeline configuration

The first thing the script does is delete the previous workspace and get a fresh copy of the source code:

#!/usr/bin/env groovy
node {
    try {
        stage('Get Source') {
            // Clear the workspace
            deleteDir()

            // Get the source code
            checkout scm
        }

        // Stages for building and running unit tests...
    }
}

According to the Git polling log, it's checking the repository every 5 minutes but finds no changes:

Started on Mar 13, 2019 4:29:34 PM
Using strategy: Default
[poll] Last Built Revision: Revision 47251333f2d6c740275f24dd667255e66f7b5665 (refs/remotes/origin/master)
using credential **********
 > git --version # timeout=10
using GIT_SSH to set credentials Jenkins SSH Authentication Key
 > git ls-remote -h [email protected]:myuser/myrepo.git # timeout=10
Found 1 remote heads on [email protected]:myuser/myrepo.git
Using strategy: Default
[poll] Last Built Revision: Revision 47251333f2d6c740275f24dd667255e66f7b5665 (refs/remotes/origin/master)
using credential **********
 > git --version # timeout=10
using GIT_SSH to set credentials Jenkins SSH Authentication Key
 > git ls-remote -h [email protected]:myuser/myrepo.git # timeout=10
Found 1 remote heads on [email protected]:myuser/myrepo.git
Done. Took 1.8 sec
No changes

However, there are several additional commits after 47251333f2d6c740275f24dd667255e66f7b5665 that have been pushed to the remote master branch.

I read here that the job has to be run manually once before SCM polling will start to work, but I have run it manually several times. What am I doing wrong?

like image 936
Big McLargeHuge Avatar asked Nov 16 '22 13:11

Big McLargeHuge


1 Answers

I think I figured out the problem. Because the branch specifier in my pipeline script configuration is */${BRANCH}, I had to specify this in the checkout step:

#!/usr/bin/env groovy
node {
    try {
        def repo = 'dice-seeker-android'
        def branch = params.Branch
        def credentialsID = params.CredentialsID

        stage('Get Source') {
            // Clear the workspace
            deleteDir()

            // Get the source code
            checkout([
                $class: 'GitSCM',
                branches: [[
                    name: '*/' + branch
                ]],
                extensions: [[
                    $class: 'RelativeTargetDirectory',
                    relativeTargetDir: repo
                ]],
                userRemoteConfigs: [[
                    credentialsId: credentialsID,
                    url: '[email protected]:myuser/' + repo + '.git'
                ]]
            ])
        }

        // Stages for building and running unit tests...
    }
}

This means I also had to include a credentials parameter that uses my SSH key.

Finally, I had to run the job manually once. Now it seems to be picking up changes.

If anyone has a better solution that requires less code, I'd still be interested in hearing about it.

like image 187
Big McLargeHuge Avatar answered Nov 30 '22 22:11

Big McLargeHuge