Wednesday, 8 July 2020

Execute Jenkins pipeline step basing on changeset

Jenkins

Suppose you have a git repository, and want to connect it to Jenkins to start a CI/CD pipeline when some specific files are changed.

You can do it checking the changeset. Let's see some examples.


Suppose your repository has this structure:

nodejs-example
├── Dockerfile
├── configurations
│   ├── dev
│   │   └── config.yaml
│   └── prod
│       └── config.yaml
├── index.js
├── package.json


If you want to do a build step only if index.js changes, then do:

stage("Build step example") {
  when{
    changeset "index.js"
  }
  steps {
    script {
      sh 'npm i'
    }
  }
}


If you want to do a build step only if a file inside configurations folder changes, then do:

stage("Build step example") {
  when{
    anyOf{
      changeset "configurations/**/*.yaml"
    }
  }
  steps {
    script {
      sh 'npm i'
    }
  }
}


If you want to do a build step only if all the files in configurations folder change, then do:

stage("Build step example") {
  when{
    allOf{
      changeset "configurations/**/*.yaml"
    }
  }
  steps {
    script {
      sh 'npm i'
    }
  }
}


If you want to do a build step only if none of the files in configurations folder change, then do:

stage("Build step example") {
  when{
    not{
      anyOf{
        changeset "configurations/**/*.yaml"
      }
    }
  }
  steps {
    script {
      sh 'npm i'
    }
  }
}


No comments:

Post a Comment