I have a Jenkins job with a branch parameter, set to poll SCM every 5 minutes, and run a pipeline script from SCM:
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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With