3

I couldn't find an answer for my question on stack, sorry if it is a duplicate.

Let's say I have repo A on Github, where my Jenkinsfile is, and repo B on Github, where all my source code is.

For my job, I've set 'Pipeline script from SCM' from repo 'A', added 'Polling ignores commit in certain paths' with included region containing only this Jenkinsfile.

In Jenkinsfile, I'm using checkout plugin to pull repo 'B' with added PathRestriction extension. Also I've added pollSCM to 'triggers' with value 'H/5 * * * *'.

But whenever I commit something to repo 'A' without any changes to this Jenkinsfile, build is triggered anyway.

My question is: How can I setup my build to trigger only, when Jenkinsfile in repo 'A' or my source code is changed?

  • Unless unavoidable, it is always a good practice to keep the Jenkinsfile together with the source code that it builds, especially when handling a large number of repositories. Just to add, the option "Polling ignores commits in certain paths" is known not work too well with pipeline jobs. – Dibakar Aditya Nov 25 '19 at 19:14

1 Answers1

0

There are two ways to trigger a build only when specific file changes.

 1. script hook: On git server side, create a hook that will check if the last commit changed the Jenkinsfile, if true, send a post to jenkins job configured with 'Trigger build remotely'.

 2. Commit: Create a repo only with the Jenkinsfile, so every commit on this repo will be a change on Jenkinsfile.

I strong advise you to change your approach and use Jenkins shared library, this is a much better way to share and manager pipeline across multiple projects / jobs

Joao Vitorino
  • 2,976
  • 3
  • 26
  • 55