2

I have many functions in my Bitbucket Repository and one single Jenkinsfile to launch one job in order to deploy these python functions. I need to deploy the functions based on the changed files in the repository. The structure of my repo is like this:

-- functions
  -- func1
    -- app.py
  -- func2.py
    -- app.py
  -- fun3.py
    -- app.py

What I want to do is: when I change some function, only deploy this function and not the others. So when I commit a change, I need to look for the changed file and deploy the corresponding function. Inside the jenkinsfile I did something like this:

            sh '''
                last_commit=$(git describe --always)

                access_token=$(cat BITBUCKET_TOKEN)             
                changed_file=$(curl https://api.bitbucket.org/1.0/repositories/account/reponame/changesets/$last_commit?access_token=$access_token | jq -r .files[].file)
                echo $changed_file > CHANGED_FILE
            '''
           CHANGED_FILE = readFile 'CHANGED_FILE'
           if (CHANGED_FILE.contains('functions/func1')) {
              // instructions ... 
           }
           CHANGED_FILE = readFile 'CHANGED_FILE'
           if (CHANGED_FILE.contains('functions/func2')) {
              // instructions ... 
           }

Here I only get the last commit and I get from it only one changed file. I want to know what logic to use to process many commits with many changed files ? I tried to use Webhook plugin but it can't do this.

Souad
  • 4,856
  • 15
  • 80
  • 140

1 Answers1

2

You can run something like this (sorry for my bad Groovy):

def changeLogSets = this.currentBuild.rawBuild.changeSets
for (int i = 0; i < changeLogSets.size(); i++) {
   def entries = changeLogSets[i].items
   for (int j = 0; j < entries.length; j++) {
      def entry = entries[j]
      def files = new ArrayList(entry.affectedFiles)
      for (int k = 0; k < files.size(); k++) {
         def file = files[k]
           this.deployFunction(file.path)
      }
  }
}

It's working for me to build docker images. For changes only in one HUGE monorepo.

For running script uncheck: enter image description here

ozlevka
  • 1,988
  • 16
  • 28
  • I'm trying to use your solution but I get this: `Scripts not permitted to use method org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper getRawBuild. Administrators can decide whether to approve or reject this signature.` – Souad Feb 28 '19 at 13:31
  • I'm using a Jenkinsfile so I don't have this checkbox – Souad Feb 28 '19 at 14:13
  • https://stackoverflow.com/a/48104262/1573395. I hope you able to do this. If not you will need write java library... – ozlevka Feb 28 '19 at 14:15
  • I'm trying the second script from this link https://support.cloudbees.com/hc/en-us/articles/217630098-How-to-access-Changelogs-in-a-Pipeline-Job- I'm getting another type of error: an exception which occurred: in field com.cloudbees.groovy.cps.impl.BlockScopeEnv.locals in object com.cloudbees.groovy.cps.impl.BlockScopeEnv thank you – Souad Feb 28 '19 at 14:24
  • could you please check this question: https://stackoverflow.com/questions/54928577/groovy-is-not-entring-the-if-condition-in-jenkinsfile – Souad Feb 28 '19 at 15:02