CircleCI-Public / github-cli-orb

Bring all of the power and flexibility of the GitHub CLI to your CI/CD pipeline.
https://circleci.com/developer/orbs/orb/circleci/github-cli
MIT License
8 stars 27 forks source link

feat: add `when` parameter to all commands #50

Closed jasonarewhy closed 2 months ago

jasonarewhy commented 6 months ago

In order to use CircleCI's common when command parameter in orbs, orb commands must have their own when parameter.

Here I'm adding when to each command so that on_success, on_fail or always can be plumbed through when needed.

This is useful in circumstances where you may need to, say, install the Github CLI as part of the steps taken after a CI job failure.

[!NOTE] No tests have been added because AFAIK there's no way in Circle to validate failure flows for orbs. I've checked their docs, forums and the tests of several major public orbs that have popular fail features, and have found nothing.

Example

Let's say you're trying to comment on a PR when there's a build failure:

commands:
  build-failure-pr-comment:
    steps:
      - gh/setup
      - run: gh pr comment ...
          when: on_fail

jobs:
  build:
    steps:
      - run:
          name: example of failure in a build
          command: exit 1
      - build-failure-pr-comment 

The above build job will not work as expected on failure because gh/setup will be skipped. The only way around this currently is to add a separate gh/setup step to the build job before you expect any failures, which is very awkward.

With this PR change, we can instead do this:

commands:
  build-failure-pr-comment:
    steps:
      - gh/setup:
          when: on_fail
      - run: gh pr comment ...
          when: on_fail

jobs:
  build:
    steps:
      - run:
          name: example of failure in a build
          command: exit 1
      - build-failure-pr-comment 

The above will work because gh/setup will still run even if the job itself fails.

david-montano-circleci commented 2 months ago

Hello @jasonarewhy , please let me know if you can update your remote branch with the suggested changes this week. If not I will just create a new PR based on this one. Thank you!

david-montano-circleci commented 2 months ago

I created this PR to merge these changes ASAP. So I will close this PR.