How can steps in YAML config be reused?


#1

Hey guys, just converting to circle 2.0 and the question is …

How can we reuse steps… I have a set of steps that need to happen before each job… (configuring apache, php, installing scripts etc ) … this needs to happen for multiple jobs in each instance…

Do I have to copy all the steps over and over again to each job… it seems very non DRY to me… and a bit of nightmare…


#2

If the section of the YAML tree is exactly the same, then references may be the way to go.


#3

YAML anchors and aliases. https://circleci.com/blog/circleci-hacks-reuse-yaml-in-your-circleci-config-with-yaml/


#4

For an example: https://github.com/retorquere/zotero-better-bibtex/blob/master/.circleci/config.yml#L16


#5

My advice above was not correct. You can define a YAML block and then replace selected key values within it on a case-by-case basis. See the entry for YAML at Wikipedia, it is pretty useful.


#6

The main limiting factor with trying to work with sharing steps is as noted above. It only works if the exact set of steps is used. While you can merge what I think of as hashes (key/value pairs in YAML), you cannot merge what I usually think of as arrays (i.e. what’s under the steps keyword).

The solution I’ve ended up with is having references for my steps and adding them one at a time in whatever sequence is needed. For example:

aliases:
  - &step_attach_workspace
    attach_workspace:
      at: /home/circleci

# partial tree
jobs:
  build_something:
    steps:
      - *step_attach_workspace
      - run: custom_command.sh
      - *save_results  # would also be defined in aliases

#7

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.