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'
您可以使用github.ref_type
来检查触发工作流运行的引用类型。
触发工作流运行的引用类型。有效值为
branch
或tag
。
publish:
needs: test
if: github.ref_type == 'tag'