1

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.

torek
  • 448,244
  • 59
  • 642
  • 775
DavidA
  • 2,053
  • 6
  • 30
  • 54
  • Would it be easier to just trigger the pipeline with a webhook such that it is guaranteed that the pipeline only executes if changes occurred? – Matthew Schuchard May 26 '22 at 13:55
  • @MattSchuchard Thanks, I did think of that actually. For security reasons, I don't want GitHub to notify our internal Jenkins instance. That's why I want to use polling (even though it is inefficient). – DavidA May 26 '22 at 13:57

1 Answers1

0

Your job/pipeline could:

That is

    }
    stage('Save Variable for next run'){
      steps {
        script {
          properties([
            parameters([
              string(defaultValue: "${scmVars.GIT_COMMIT}", description: 'Previous checked out commit', name: 'PREVIOUS_COMMIT', trim: true)
            ])
          ])
        }
      }
    }

That way, your pipeline can compare the ${scmVars.GIT_COMMIT} from the current checked out repository with the ${PREVIOUS_COMMIT} recorded on that last job execution.
If they differ, new commits have been published.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250