I've been trying to convert a Jenkins Freestyle project into a Pipeline project which uses a shared library for helper functions, but I have no success in getting the Jenkins Pipeline to trigger a build when changes are made in the source repository.
I've attempted to define the build trigger in multiple parts of the groovy script, but none of them seem to have any effect.
To prevent the trigger from looking at the git repository used by the shared library, the shared library has been configured to not Include @Library changes in job recent changes
.
I expected the pollSCM module to trigger a build, when changes are encountered in a branch starting with the prefix MyBranchPrefix
, just like my Jenkins Freestyle project does, but the Git Polling Log
always states:
Started on 9. dec. 2024 12.00.01
no polling baseline in D:\Workspace\My build name@libs\abcdef01234564789 on
Done. Took 0 ms
No changes
My Pipeline contains the following groovy script (Which attempts to define build triggers twice, but neither trigger works):
@Library("MySharedLibrary") _
pipeline {
agent any
triggers {
pollSCM '* * * * *'
}
options {
lock resource: 'MyLock'
}
environment {
branch = ".*/(?i)MyBranchPrefix[^/]*"
}
stages {
stage('Pull source') {
steps {
script {
properties([pipelineTriggers([pollSCM('* * * * *')])])
}
cleanWs()
git branch: $env.Branch, changelog: true, credentialsId: 'MyGitCredentials', poll: true, url: 'https://mygitserver.local/myrepository.git'
script {
echo 'Pulled source code'
}
}
}
}
}
Besides the triggers defined in the groovy script, I also tried adding a Build Trigger with `Poll SCM´ option enabled, in the Pipeline build, but without any luck.
Can anyone tell me how to properly define a build trigger that starts the build when any changes in a MyBranchPrefix
prefixed branch occurs in the https://mygitserver.local/myrepository.git
repository?
I finally managed to find a solution to the problem. Several undocumented settings prevented this script from being triggered.
scmGit
must be used instead.scmGit
doesn't set the GIT_BRANCH
environment variable, so this must be manually set after calling scmGit
.The working script looks like this:
@Library("MySharedLibrary") _
pipeline {
agent any
triggers {
pollSCM '* * * * *'
}
options {
lock resource: 'MyLock'
}
stages {
stage('Pull source') {
steps {
cleanWs()
script {
def scmVars = checkout scmGit(branches: [[name: ":.*/(?i)MyBranchPrefix[^/]*"]], userRemoteConfigs: [[credentialsId: "MyGitCredentials", url: "https://mygitserver.local/myrepository.git"]])
env.GIT_BRANCH = scmVars.GIT_BRANCH
}
}
}
}
}