Make custom command run always with `when: always`

I have a circle config which includes the following custom command:

  remove-circle-ip:
    description: "remove current Circle CI box IP from inbound security group rules for DB"
    steps:
      - aws-white-list-circleci-ip/remove:
          tag-key: circleci
          tag-value: whitelistmeplease
          port: 5432

which I use in my job as follows:

jobs:
  test:
    docker:
      - image: nikolaik/python-nodejs:python3.8-nodejs12
        environment:
          AWS_DEFAULT_REGION: us-east-2
    steps:
      - setup
      - install-python-deps
      - add-circle-ip
      - run:
          name: run tests
          command: |
            poetry run coverage run --source='.' manage.py test 
      - run:
          name: remove circle IP
          command:  remove-circle-ip
          when: always

I’d like the remove circle IP to run even if the tests which run before it fail. I can’t seem to figure out the syntax for this. Previously, I had just used - remove-circle-ip to run the command rather than putting a run block, i.e.:

jobs:
  test:
    docker:
      ...
    steps:
      - setup
      - ...
      - add-circle-ip
      - ...
      - remove-circle-ip

but couldn’t figure out how to specify when: always if I did it that way.

But now, when switching to run, it fails with “remove-circle-ip: command not found”

So how can I make this command always run even if steps before fail?

Hi @levinotik – welcome to Discuss!

Is the remove-circle-ip command invoking an Orb? Or is the command using another already specified command?

The when: always will essentially need to get added at the base level of the run step. If you are using an Orb that may require changing the Orb. If you can provide a link to the failed build you mentioned I can look at your config file and provide a bit more information. If you don’t feel comfortable posting the build link here you can submit a ticket and mention this post and I will pick up that ticket.

Thanks!

hey, thanks for the reply. I got this to work by switching the run to a when block as follows:

  - when:
      condition:
        equal: [ 42, 42 ] # a value that is always true
      steps:
        - remove-circle-ip

Awesome, thanks for the reply on how you ended up solving this!

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