3

So we can have only one Jenkinsfile per job. And we share the same job for both merge and PR webhooks from github. How can we easily detect if the webhook for a commit pushed in a PR or for PR merge?

StephenKing
  • 36,187
  • 11
  • 83
  • 112
Behlül
  • 3,412
  • 2
  • 29
  • 46
  • Assuming different branches for PR and merge PR, e.g. 'feature' and 'develop', you could solve this using `when { branch '...' }`. Reference: [Pipeline Syntax](https://jenkins.io/doc/book/pipeline/syntax/#when) – groverboy Apr 18 '18 at 00:39
  • 2
    Actually, there is a `when { changeRequest() }` condition mentioned on the site referenced by @groverboy – StephenKing Apr 18 '18 at 05:58
  • @StephenKing +1 thanks - I see that `changeRequest` was added a couple of weeks ago. – groverboy Apr 20 '18 at 03:33
  • I can accept as answer if you guys post a code snippet as answer :) – Behlül Apr 20 '18 at 17:15

1 Answers1

2

This (declarative pipeline) snippet might help:

stage('do something for PRs opened against develop branch') {
    when {
        changeRequest target: 'develop'
    }
    steps {
        sh 'pr-worker.sh'
    }
}

stage('do something on merge or direct commits to the develop branch') {
    when {
        branch 'develop'
    }
    steps {
        sh 'develop-worker.sh'
    }
}

See when in the documentation.

AymDev
  • 6,626
  • 4
  • 29
  • 52
Tobi
  • 36
  • 3