I have a declarative pipeline in Jenkins, stored in a Subversion repo, that must clone a Github repo once and, on subsequent runs, pull changes from the GitHub repo and call a Python script if changes were found. I could do the checking within the Python script but I can't get the git authentication to work that way, so I want to do the checking at pipeline level.
My pipeline looks like this:
pipeline {
environment {
GITHUB_URL='https://github.com/<snip>'
}
stages {
stage('git checkout') {
steps {
dir("${GITHUB_DIR}") {
// Checkout the GitHub repository here
checkout([$class: 'GitSCM',
branches: [[name: '*/main']],
extensions: [],
userRemoteConfigs: [[credentialsId: 'ntmlci-personal-access-token',
url: "${GITHUB_URL}"]]
])
}
}
stage('run script') {
steps {
// In the directory above the checkout, run the Python script
dir("${GITHUB_DIR}") {
withPythonEnv('/usr/bin/python3') {
sh '''
python3 -u myscript.py
'''
}
}
}
}
}
In Python I checked for changes like this:
my_repo = Repo(local_path)
current = my_repo.head.commit
my_repo.remotes.origin.pull()
isRepoChanged = False
if current != my_repo.head.commit:
isRepoChanged = True
But I don't know how to do that in a pipeline script. I would be grateful for some help with this.