circlecicircleci-workflows

CircleCI Hold step


I'm trying to add a hold job into a workflow in CircleCI's config.yml file but I cannot make it work and I'm pretty sure it's a really simple error on my part (I just can't see it!).

When validating it with the CircleCI CLI locally running the following command

circleci config validate:

I get the following error

Error: Job 'hold' requires 'build-and-test-service', which is the name of 0 other jobs in workflow 'build-deploy'

This is the config.yml (note it's for a Serverless Framework application - not that that should make any difference)

version: 2.1
jobs:
  build-and-test-service:
    docker:
      - image: timbru31/java-node
    parameters:
      service_path:
        type: string
    steps:
      - checkout
      - serverless/setup:
          app-name: serverless-framework-orb
          org-name: circleci
      - restore_cache:
          keys:
            - dependencies-cache-{{ checksum "v2/shared/package-lock.json" }}-{{ checksum "package-lock.json" }}-{{ checksum "<< parameters.service_path >>/package-lock.json" }}
            - dependencies-cache
      - run:
          name: Install dependencies
          command: |
            npm install
            cd v2/shared
            npm install
            cd ../../<< parameters.service_path >>
            npm install
      - run:
          name: Test service
          command: |
            cd << parameters.service_path >>
            npm run test:ci
      - store_artifacts:
          path: << parameters.service_path >>/test-results/jest
          prefix: tests
      - store_artifacts:
          path: << parameters.service_path >>/coverage
          prefix: coverage
      - store_test_results:
          path: << parameters.service_path >>/test-results
  deploy:
    docker:
      - image: circleci/node:lts
    parameters:
      service_path:
        type: string
      stage_name:
        type: string
      region:
        type: string
    steps:
      - run:
          name: Deploy application
          command: |
            cd << parameters.service_path >>
            serverless deploy --verbose --stage << parameters.stage_name >> --region << parameters.region >>
      - save_cache:
          paths:
            - node_modules
            - << parameters.service_path >>/node_modules
          key: dependencies-cache-{{ checksum "package-lock.json" }}-{{ checksum "<< parameters.service_path >>/package-lock.json" }}
orbs:
  serverless: circleci/serverless-framework@1.0.1
workflows:
  version: 2
  build-deploy:
    jobs:
      # non-master branches deploys to stage named by the branch
      - build-and-test-service:
          name: Build and test campaign
          service_path: v2/campaign
          filters:
            branches:
              only: develop
      - hold:
          name: hold
          type: approval
          requires:
            - build-and-test-service
      - deploy:
          service_path: v2/campaign
          stage_name: dev
          region: eu-west-2
          requires:
            - hold

It's obvious the error relates to the hold step (near the bottom of the config) not being able to find the build-and-test-service just above it but build-and-test-service does exist so am stumped at this point.


Solution

  • For anyone reading I figured out why it wasn't working.

    Essentially I was using the incorrect property reference under the requires key:

    workflows:
      version: 2
      build-deploy:
        jobs:
          # non-master branches deploys to stage named by the branch
          - build-and-test-service:
              name: Build and test campaign
              service_path: v2/campaign
              filters:
                branches:
                  only: develop
          - hold:
              name: hold
              type: approval
              requires:
                - build-and-test-service
    
    

    The correct property reference in this case should have been the name of the previous step, i.e. Build and test campaign so I just changed that name to build-and-test-service.

    I found the CircleCI docs were not very clear on this but perhaps it was because their examples surrounding manual approvals stated the requires property should be pointing to the root key of the job such as build-and-test-service.

    I suppose I should have been more vigilant in my error reading too, it did mention name there as well.