How to reuse steps in CircleCI?

Circleci - PDA

There are more than 10 development projects that require CircleCI at Prevent Direct Access. The problem is as we’re using the same configuration for different jobs, we have to copy all the steps codes over and over again. As a consequence, it’s very hard to modify and maintain them. In this article, we will show you how to improve CircleCI configurations.

Let’s have a look at this example.

# .circleci/config.yml
version: 2
jobs:
  build:
    docker:
      - image: preventdirectaccess/pda-ci:0.0.3
    working_directory: ~/project
    steps:
      - checkout
      - run:
          name: "Install dependencies"
          command: |
              git submodule sync
              git submodule update --init
              git submodule update --remote
  unit-testing:
    docker:
      - image: preventdirectaccess/pda-ci:0.0.3
    working_directory: ~/project
    steps:
      - checkout
      - run:
          name: "Install dependencies"
          command: |
              git submodule sync
              git submodule update --init
              git submodule update --remote

Do you find it lengthy? If you want to add npm install command to Install dependencies step, you have to add it into two places, which apparently doesn’t look good. We should reuse YAML in CircleCI, instead.

Don’t repeat yourself with Anchors and Aliases

Anchors and Aliases let you identify an item with an anchor in a YAML document. In other words, you can replace all loop nodes by only one node. In the snippet above, we use “Install dependencies” command in 2 jobs. The duplication can be removed by the following code:

# .circleci/config.yml
version: 2

install_depencies: &install_depencies
  - run:
     name: "Install dependencies"
     command: |
        git submodule sync
        git submodule update --init
        git submodule update --remote

jobs:
  build:
    docker:
      - image: preventdirectaccess/pda-ci:0.0.3
    working_directory: ~/project
    steps:
      - checkout
      <<: *install_depencies
  unit-testing:
    docker:
      - image: preventdirectaccess/pda-ci:0.0.3
    working_directory: ~/project
    steps:
      - checkout
      <<: *install_depencies

As a result, it reduced 5 lines of code. If we want to add a command to 2 jobs, we only need to put it in the anchor. Alternatively, we can apply commands in version 2.1.

Don’t repeat yourself with command

Rules for a reusable command:

  • Description (optional): Describes the purpose of the command
  • Parameters (optional): A map of parameter keys
  • Steps (required): A sequence of steps running inside the calling job of the command

We are going to write a command “Hello PDA” with the parameter content.

version: 2.1

commands:
  helloPDA:
    description: "Simple to reuse it"
    parameters:
      content:
        type: string
        default: "Hello PDA !!!1"
    steps:
      - run: echo << parameters.name >>

With commands in CircleCI, it’s very easy to separate steps to reuse it. What’s more, commands may work as a part of the sequence under steps in a job.

version: 2.1

jobs:
  Pdajob:
    docker:
      - image: "preventdirectaccess/pda-ci:0.0.3"
    steps:
      - helloPda:
          content: "Hello PDA !!!"

To sum up, by following the above tips to reuse the commands, we can cut short more than 100 lines of code per project. In the next article, we will discover CircleCI orbs to improve our config.

References: