0

I have setup a pipeline project similar to https://jenkins.io/doc/book/pipeline/syntax/#parallel-stages-example and it works fine.

But I have created the same project for different jobs, so rather than creating new jobs can I run for loop for all jobs e.g. jobs = [job1, job2, job3] and run above pipeline code for different jobs.

Something similar to https://jenkins.io/doc/book/pipeline/syntax/#script-example

Can I use a for loop or similar logic to run a declarative pipeline?

I found similar question but not sure how to modify fo this Is it possible to create parallel Jenkins Declarative Pipeline stages in a loop?

David
  • 3,392
  • 3
  • 36
  • 47
user2661518
  • 2,677
  • 9
  • 42
  • 79
  • I am trying to add into declarative syntax so I can add this to Shared Library – user2661518 Aug 30 '18 at 01:03
  • `Is it possible to create parallel Jenkins Declarative Pipeline stages in a loop?` Max's answer does it exactly how you want it ...Just copy the same thing and replace ["JobA", "JobB", "JobC"] to [job1, job2, job3] ...it will work – rohit thomas Aug 30 '18 at 03:05
  • You can check https://github.com/jenkins-infra/pipeline-library/blob/master/vars/buildPlugin.groovy and get the idea of how it's done in Jenkins itself by using a Map, I tried it my self and I came up with a weird error popping up, you might succeed though. Let me know :). – Shlomi Bazel Aug 30 '18 at 09:15
  • @rohitthomas If I tried as Max's answer won't it be `Scripted Pipeline` syntax? If I'm using `def` method, and I will have to add all declarative methods in `def` method such as `agent`, `environment` etc. I thought you can't use `def` method with 1.3 syntax – user2661518 Aug 30 '18 at 14:42
  • this is the format I am looking at https://jenkins.io/blog/2018/07/02/whats-new-declarative-piepline-13x-sequential-stages/#about-the-author, not finding any example how to incorporate for loop with this, any pointers are appreciated – user2661518 Aug 30 '18 at 14:48
  • Where exactly are you facing the issue ... I tried it based on the format you wanted with Max's answer it is working fine and Declarative is built on top of Scripted pipeline so def will work :) – rohit thomas Sep 03 '18 at 03:06
  • hmm I thought if I have to move code to Shared library i.e. under `def call { pipeline {}}` I can't have other `def` method, – user2661518 Sep 03 '18 at 03:15

0 Answers0