text: Q:
  
  Template workflows in Jenkins
  
  Every jenkins pipeline does pretty much the same thing - atleast in a small team with multiple projects.
  Build (from the same sourcecode repo) --> run tests --> publish artifacts (to the same artifact repo)
  We are creating many new projects and they all have very similar lifecycle. Is it possible to create a template pipeline from which I can create concrete pipleines and make necessary changes to the jobs?
  
  A:
  
  There are a couple of approaches that I use that work well for me and my team.
  part 1) is to identify which orchestration plugins suits you best in jenkins.
  Plugins and approaches that worked well for me were:
  a) Use http://ci.openstack.org/jenkins-job-builder/
  It abstract the jobs definitions and flows using a higher level library. It allows you to define jobs in YAML which is fairly simple and it supports most of the common usage cases (jobs, templates, flows). 
  These yaml files can then be consumed by the jenkins-jobs-builder python cli tool through an orchestration tool such as ansible, puppet,chef.
  You can use YAML anchors to replace blocks that are common to multiple jobs, or ever template them from a template engine (erb,jinja2)
  b) Use the workflow-plugin, https://github.com/jenkinsci/workflow-plugin
  The workflow plugin allows you to have a single workflow in groovy, instead of a set of jobs that chain together.
  "For example, to check out and build several repositories in parallel, each on its own slave:
  parallel repos.collectEntries {repo -> [/* thread label */repo, {
      node {
          dir('sources') { // switch to subdir
              git url: "https://github.com/user/${repo}"
              sh 'make all -Dtarget=../build'
          }
      }
  }]}
  
  "
  If you build these workflow definitions from a template engine (ERB, jinja2), and integrate them with a configuration management tool (again ansible,chef,puppet).
  It becomes a lot easier to make small and larger changes that affect one or all the jobs.
  For example, you can template that some jenkins boxes compile, publish and deploy the artifacts into a development environment, while others simply deploy the artifacts into a QA environment.
  This can all be achieved from the same template, using if/then statements and macros in jinja2/erb.
  Ex (an abstraction): 
  if ($environment == dev=) then compile, publish, deploy($environment)
  elif ($environment== qa) then deploy($environment)
  
  part2) is to make sure all the jenkins configuration for all the jobs and flows is kept in source control, and make sure a change of a job definition in source control will be automatically propagated to the jenkins server(s) (again ansible, puppet, chef).
  Or even have a jenkins jobs that monitors its own repo of jobs definitions and automatically updates itself
  When you achieve #1 and #2 you should be at a position where you can with some confidence allow all your team members to make changes to their jobs/projects, giving you information of who changed what and when, and be able to rollback changes easily from change control when things go wrong.
  its pretty much about getting jenkins to deploy code from a series of templated jobs that were themselves defined in code.
  
  