Disclaimer: The statements and opinions expressed in this article are those of the author(s) and do not necessarily reflect the positions of Thoughtworks.
Pipeline templates are useful when you need two pipelines to do the same things except that they build against different repositories or different branches of the same repository. This latter case is common. You might have a short lived branch and a long lived mainline. Using templates here is better than cloning the mainline pipeline because any future change to the template will automatically get reflected in both mainline and branch. On the other hand, you do want to clone if you want to simply start off as same and then diverge. e.g. perhaps your branch pipeline needs an extra stage.
Pipeline parameters allow for quite an impressive degree of customization. Say the mainline works against oracle-jdk and we create a branch to support open-jdk. We want the artifact names to reflect this. Just define a pipeline parameter called target_jdk in both pipelines with values oracle-jdk and open-jdk respectively.
Then, in the template, at the appropriate job, specify the name of the artifact as: myproduct_#{target-jdk}_$GO_PIPELINE_LABEL
Next, we want to make sure that the branch pipeline only executes on agents where open-jdk is available. We could do this by tagging all jobs in the template with parameterised resources.
There is a flip side of partitioning agents using resources. Now all jobs in all pipelines need to say via resources what kind of agents they should execute on. Otherwise , a job without any resources specified is free to execute on an open-jdk agent or an oracle-jdk agent or any other agent. To achieve true ring-fencing, we need to use the environments feature in Go.
We could also parameterize the name of batch or shell script in a template job so that you can have a windows pipeline and a linux pipeline based off the same template.
What if you have two (or more) templated pipelines that have different upstream pipelines? Each downstream pipeline would want to specify a different artifact to be fetched from parent. Parameters to the rescue again. Simple define a parameter called my-upstream in each of the templated downstream pipelines. Then use the parameter in the fetch artifact specification inside the template as shown above.
In our final Part 6 of "How do I do CD with Go?", I detail ways to model higher-order workflows with Go
"How do I do CD with Go?" blog series:
Disclaimer: The statements and opinions expressed in this article are those of the author(s) and do not necessarily reflect the positions of Thoughtworks.
Thoughtworks acknowledges the Traditional Owners of the land where we work and live, and their continued connection to Country. We pay our respects to Elders past and present. Aboriginal and Torres Strait Islander peoples were the world's first scientists, technologists, engineers and mathematicians. We celebrate the stories, culture and traditions of Aboriginal and Torres Strait Islander Elders of all communities who also work and live on this land.
As a company, we invite Thoughtworkers to be actively engaged in advancing reconciliation and strengthen their solidarity with the First Peoples of Australia. Since 2019, we have been working with Reconciliation Australia to formalize our commitment and take meaningful action to advance reconciliation. We invite you to review our Reconciliation Action Plan.