1

I have a pipeline script, and would like to take different actions depending on the changes of the migrations folder.

Basically would be a workflow like this

  1. Pull changes in the repository
  2. Check if the migrations/ folder has new migrations or changes
  3. If changes are present, run migrations, if not, continue

I'm not sure how could I achieve this, I'm using version 2.1 and the git plugin. This repo is on a private server

Francisco Rangel
  • 129
  • 1
  • 11

2 Answers2

0

In your case, 'included region' feature from Git plugin should help. See this answer for details.

So, for pipeline, you can generate the correct syntax using pipeline-syntax generator (under http://<JENKINS_IP>:<JENKINS_PORT>/job/<PATH_TO_PIPELINE_JOB>/pipeline-syntax/ job in Sample Step: checkout -> SCM: Git -> Additional Behaviours -> Polling ignores commits in certain paths). It will be something like this:

checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'PathRestriction', excludedRegions: '', includedRegions: 'migrations/.*']], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'test', url: 'http://test.com/test.git']]])

Check this documentation for details (extensions -> includedRegions).

For job dsl syntax it will be like this:

scm {
    git {
        remote {
            ...
        }
        extensions {
            cleanBeforeCheckout()
            disableRemotePoll() // this is important for path restrictions to work
            configure { git ->
                git / 'extensions' / 'hudson.plugins.git.extensions.impl.PathRestriction' {
                    includedRegions "somepath/.*"
                    excludedRegions "README.md\n\\.gitignore\npom.xml"
                }
            }
        }
    }
}

Also, you can use GitHub/GitLab/BitBucket webhooks to build a project when a change is pushed to repository. See this example for Github and BitBucket configuration and this example for GitLab configuration.

If you want to build the project only for changes in migrations folder and not for any changes in repository, you can configure comment regex for triggering a build and add this specific comment (e.g., "[changes in migrations folder]") to the commit every time you want to trigger a build.

biruk1230
  • 3,042
  • 4
  • 16
  • 29
  • Forgot to add that I'm using the git plugin, the repo is in a private server, That may be a valid approach, if there is no other way I'll probably do that. Though ideally I would like it to be based on the folder itself, my goal is to make this work with the minimal amount of manual intervention – Francisco Rangel Dec 27 '18 at 13:11
  • Probably you can use 'included region' feature from Git plugin (see updated answer). – biruk1230 Dec 27 '18 at 13:31
  • I saw that but I can't find that option, It should be under additional behaviors but I can't find it, is it not available for pipelines? – Francisco Rangel Jan 27 '19 at 19:11
  • In pipelines it's under `extensions`. If you're using pipeline-syntax generator: you can find it in **Additional Behaviours -> Polling ignores commits in certain paths** option. Updated in answer with more details. – biruk1230 Jan 28 '19 at 09:00
0

There's probably a way to do it directly with the plugin, but I only get the option for included regions if I add another branch source as "Single repository & branch", so for now I implemented this solution:

I added this to my Jenkinsfile, to check for changes on the migrations/ folder

script {
    env.CONTAINS_MIGRATIONS = sh (
        script: 'git diff --name-only --diff-filter=AMDR --cached HEAD^',
        returnStdout: true
    ).trim()

    if (env.CONTAINS_MIGRATIONS.contains('migrations')) {
        // Do migrations related stuff
    }
}

I'm doing this considering that is unlikely to have filename conflicts, and if they happen is not a big deal

Francisco Rangel
  • 129
  • 1
  • 11