Release tag with circleci/github-cli

I want to create a GitHub release whenever a tag is pushed with the following code, using circleci/github-cli@1.0

aliases:
  - &tags_only
    tags:
      only: /^v[0-9]+\.[0-9]+\.[0-9]+(\-[0-9a-z]+)?+$/
    branches:
      ignore: /.*/
orbs:
  gh: circleci/github-cli@1.0
workflows:
  release:
    when: << pipeline.git.tag >>
    jobs:
      - gh/release:
          notes-file: CHANGELOG.md
          tag: << pipeline.git.tag >>
          token: GITHUB_TOKEN
          filters: *tags_only

However, this will cause error for pipelines that are not triggered by tags.

I figured out the following reasons:

  1. The orb does not provide a default value to the parameter (and thus passing empty string will cause error)
  2. CircleCI tries to call the workflow during config build, even the workflow will not run (where pipeline.git.tag is empty string)

Here’s a simplified demo of the problem.

Has anyone managed to create a release using this orb? May I have some suggestions?

P.S. TBH, I think point 2 above should be a defect of CircleCI.

Hello @coo-lest,

I would recommend removing when: << pipeline.git.tag >> from this workflow, the filters will be all that is needed for this.

So if you would like to execute gh/release on no branches and on a tag we should look at a config more like this:

orbs:
  gh: circleci/github-cli@1.0
workflows:
  release:
    jobs:
      - gh/release:
          notes-file: CHANGELOG.md
          tag: << pipeline.git.tag >>
          token: GITHUB_TOKEN
          filters:
          tags:
            only: /^v.*/
          branches:
            ignore: /.*/

That said, you probably could keep in your when conditional if desired, the important part will be to ensure you add the branch filter:

filters:
  branches:
    ignore: /.*/

Hi Kyle,

Thanks for your reply! My config was actually the same as your suggestion, as the filters is an alias that resolves to a map ignoring all branches.

I just updated the code, adding the aliases section (sorry for missing that in the first place).

Could you please help check the problem again?

P.S. You may checkout the demo, a minimal reproduction of the problem. You’ll see that the error appeared when CircleCI parses the config file.

In your demo try removing the when not equal check.

Circleci’s tag support is a little odd. The CIRCLE_TAG parameter is only populated if the job is known to pass a tag-based filter and it is not defined within your example. It is not clear from the docs what the status of pipeline.git.tag should be, but as it is not defined at runtime it is likely to be defined using the same logic as CIRCLE_TAG.

You may also need to switch to using CIRCLE_TAG while debugging the issue as its availability and value are at least shown in the ‘Preparing environment variables’ step - so you can debug even if the script is unable to run.

On a side note, if the filter works correctly for a tag the CIRCLE_BRANCH env var will be defined but without a value.