Declarative pipeline - Call another Jenkinsfile from pipeline script

33 views
Skip to first unread message

Ashish Sharma

unread,
Sep 7, 2020, 9:57:50 PM9/7/20
to Jenkins Users
Dear All,

Below is our usecase (using declarative script):
  • We have a small pipeline script (Jenkinsfile) which will do sanity, tests & lint check on every feature branch and pull requests
  • Then we have another script (Jenkinsfile) which is superset which will additionally build and deploy as well. We will use this for develop / master branches
  • We are planning to use Org folders (BitBucket teams) to scan repo and create pipeline automatically.

How can we reuse our first script file to be included in second script, so that we don't have to duplicate declarative script?

Any other suggestions on how you generally tackle such cases?

Thanks,
Ashish

Saravanan Manickam

unread,
Sep 8, 2020, 11:09:11 AM9/8/20
to Jenkins Users
I am not sure whether I understood completely..  you could have used "when" condition to match the branch and do all the additional steps you want.  It'll be clean that way.

Regards,
Saravanan

Gian Maxera

unread,
Sep 8, 2020, 11:19:39 AM9/8/20
to jenkins...@googlegroups.com

I agree with Saravanan ... and I would also add that there are other two Jenkins features we use for your case.
The approach we follow is:
- first we try with "when" to run stages conditionally depending on branches/parameters/tags/directory and so on
- when we see many times the same piece of code and we want to DRY, then we use the readTrusted to load a snippet of functionality:
def notifier = evaluate readTrusted('./scripts/jenkins/notifier.groovy')
- when we see many times the same piece of code used across different repositories, then we use the Library approach:
@Library('osirium-...@2.0.1')_



--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/1ff7b13b-75b4-440b-abbb-e106ed56ec16n%40googlegroups.com.

Ashish Sharma

unread,
Sep 8, 2020, 3:22:16 PM9/8/20
to jenkins...@googlegroups.com
Thanks both, let us explore the suggested options. 
Stay safe. Cheers.

--
Thanks,
Ashish
Reply all
Reply to author
Forward
0 new messages