github actions: 如何检查当前推送是否有新标签(是新版本)?

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

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

我应该在 jobs.publish.if 中输入什么以检查此提交是否是新版本?

这样可以吗:contains(github.ref, '/tags/')

同时推送代码和标签会怎样?

您可以这样做来检查当前推送事件是否针对以 v 开头的标签。

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

正如您所指出的,我认为您不能保证这是一个新版本。我的建议是使用 on: release 而不是 on: push。这只会在新标记的版本上触发。

请在此处查看 on: release 的文档: https://docs.github.com/en/actions/reference/events-that-trigger-workflows#release

另一种使用 GitHub 发布作为触发器的方法(如果您想自由使用标签并仅发布特定版本):

on:
  release:
    types: [created]

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