circleciorb

How to use a circleci orb


I am new to circleci so not sure how to use an orb. Couldn't find a good example documetation. The orb in question is this https://circleci.com/developer/orbs/orb/ganezasan/auto-cancel-workflow

The idea is that circleci should be able to cancel other josb whose tests are failed. Instead of re-inventing the wheel, i found this orb but couldn't find a sample config.yml file.

How can i use this orb in my existing config.yml file?


Solution

  • The orb page you linked provides a usage example:

    jobs:
      failed-job:
        docker:
          - image: 'cimg/base:stable'
        steps:
          - run: sleep 30 && exit 1
      success-job-1:
        docker:
          - image: 'cimg/base:stable'
        steps:
          - run: sleep 60
      success-job-2:
        docker:
          - image: 'cimg/base:stable'
        steps:
          - run: sleep 60
    orbs:
      auto-cancel: ganezasan/auto-cancel-workflow@0.0.7
    version: 2.1
    workflows:
      cancel-workflow:
        jobs:
          - success-job-1
          - success-job-2
          - failed-job
          - auto-cancel/auto-cancel:
              api_token: CIRCLE_TOKEN
              interval_seconds: 10
    

    But basically. You need to add the orb to your config.yml using this code block

    orbs:
      auto-cancel: ganezasan/auto-cancel-workflow@0.0.7
    

    and then you need to add this job to your workflow

          - auto-cancel/auto-cancel:
              api_token: CIRCLE_TOKEN
              interval_seconds: 10
    

    and then you need to make sure you have an environment variable added to your project called CIRCLE_TOKEN with the value set to a CircleCI API token that you can generate under User Settings > Personal API Tokens.

    But rather than using this orb you may want to look at running your jobs in series using requires. In the workflow below if test1 fails both test2 and deploy will be skipped. If test 1 is successful, test2 will run. If test2 fails then deploy will be skipped.

    workflows:
      version: 2
      build-test-and-deploy:
        jobs:
          - build
          - test1:
              requires:
                - build
          - test2:
              requires:
                - test1
          - deploy:
              requires:
                - test2