Avoid repeating test configuration in workflows

php
workflow

#1

Hello there,

I’m trying to write a Circle 2.0 config file for testing a repo on multiple PHP versions. I’ve seen an example of Workflows used for this, in Ruby: https://github.com/CircleCI-Public/circleci-demo-workflows/blob/parallel-jobs/.circleci/config.yml

The problem is, all the configuration steps are repeated for all the versions. This is fine here, as there are just 3 Ruby versions and only a few brief steps, but in my case the steps might be more, commands might be longer, and PHP versions will be more than 3. This means that if I chose this approach, my config file would become huge and hardly maintainable.

My question is: is there a way to define a set of shared steps which is then executed for every build? Or honestly, any other way of avoiding the repetition.

Thanks!


#2

It is possible to use YAML built-in anchor functionality to inject common context:

version: 2.0

job-template: &job-template
    working_directory: ~/circleci-demo-workflows
    steps:
      - checkout
      - run: bundle install --path vendor/bundle
      - run: bundle exec rake db:create db:schema:load
      - run:
          name: Run tests
          command: rake

jobs:
  "ruby-2.2":
    <<: *job-template
    docker:
      - image: circleci/ruby:2.2-node
      - image: circleci/postgres:9.4.12-alpine

  "ruby-2.3":
    <<: *job-template
    docker:
      - image: circleci/ruby:2.3-node
      - image: circleci/postgres:9.4.12-alpine

  "ruby-2.4":
    <<: *job-template
    docker:
      - image: circleci/ruby:2.4-node
      - image: circleci/postgres:9.4.12-alpine

workflows:
  version: 2
  build:
    jobs:
      - "ruby-2.2"
      - "ruby-2.3"
      - "ruby-2.4"

#3

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