github actions: how to check if current push has new tag (is new release)?

Viewed 19163
name: test-publish

on: [push]

jobs:
  test:
    strategy:
      ...
    steps:
      ...

  publish:
    needs: test
    if: github.event_name == 'push' && github.ref???
    steps:
      ...  # eg: publish package to PyPI

What should I put in jobs.publish.if in order to check that this commit is new release?

Is this okay: contains(github.ref, '/tags/')?

What will happen if I push code and tag at the same time?

2 Answers

You could do this to check if the current push event is for a tag starting with v.

  publish:
    needs: test
    if: startsWith(github.ref, 'refs/tags/v')

As you pointed out though, I don't think you can guarantee that this is a new release. My suggestion would be to use on: release instead of on: push. This will only trigger on a newly tagged release.

See the docs for on: release here: https://docs.github.com/en/actions/reference/events-that-trigger-workflows#release

An alternative way to use a GitHub Release as a trigger (in case if you want to use tags freely and release specific versions only):

on:
  release:
    types: [created]

jobs:
  release-job:
    name: Releasing
    if: github.event_name == 'release' && github.event.action == 'created'
Related