GitHub动作重用工作流定义



我有一个项目,我有两个GitHub动作yml文件,其中第一个文件被称为构建。它包含编译、构建和测试项目的指令。就像这样简单:

name: build my-project
on:
push:
paths-ignore:
- 'images/**'
- README.md
branches:
- master
pull_request:
branches:
- master
release:
types: [ created ]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: cache ivy2
uses: actions/cache@v1
with:
path: ~/.ivy2/cache
key: ${{ runner.os }}-sbt-ivy-cache-${{ hashFiles('**/*.sbt') }}-${{ hashFiles('project/build.properties') }}
- name: sbt Test
run: sbt clean test

我现在有了另一个yml文件,其中包含基于带注释的标记进行发布的指令。是这样的:

name: release my-project
on:
push:
# Sequence of patterns matched against refs/tags
tags:
- 'v[0-9]+.[0-9]+.[0-9]+-[a-zA-Z]*'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
jobs:
build:
uses: ./.github/workflows/build.yml
publish:
runs-on: ubuntu-latest
needs: test # See build.yml file where the test job is defined
# If there is a tag and if that tag comes from master branch
if: startsWith(github.ref, 'refs/tags/v')
steps:
- name: checkout
uses: actions/checkout@v3
- name: capture changelog
id: changelog
uses: metcalfc/changelog-generator@v4.0.1
with:
myToken: ${{ secrets.GITHUB_TOKEN }}
- name: sbt ci-publish-github
run: sbt publish
- name: ci-release-github
id: create-release
uses: actions/create-release@latest
with:
allowUpdates: true
tag_name: ${{ github.ref }}
release_name: Release ${{ github.ref }}
body: |
## What's Changed
${{ steps.changelog.outputs.changelog }}
draft: false
prerelease: false

我刚刚创建了一个带注释的标签,然后导致如下错误:

Invalid workflow file: .github/workflows/publish.yml#L14
error parsing called workflow "./.github/workflows/build.yml": workflow is not reusable as it is missing a `on.workflow_call` trigger

所以基本上我想要的是,当我推送一个带注释的标签时,我想要首先从构建中运行测试作业。然后,一旦成功,我想运行发布作业。有什么好建议吗?

所以基本上我想要的是,当我推一个带注释的标记时,我想首先从构建中运行测试作业。然后,一旦成功,我想运行发布作业。有什么好建议吗?

你的实现几乎是正确的。您只需要做一些修改:

  • build作业需要依赖于publish作业:
name: release my-project
on:
push:
# Sequence of patterns matched against refs/tags
tags:
- 'v[0-9]+.[0-9]+.[0-9]+-[a-zA-Z]*'
jobs:
publish:
[ ... ]
build:
needs:
- publish
uses: ./.github/workflows/build.yml
  • build需要workflow_call触发器(如错误信息所述-参考):
on:
workflow_call:
push:
[ ... ]

注意:您甚至可以共享tag值从以前的工作流程,发送它作为输入到第二个使用:

on:
workflow_call:
inputs:
tag:
required: true
type: string

从主工作流中调用可重用工作流:

build:
needs:
- publish
uses: ./.github/workflows/build.yml
with:
tag: 'MY TAG'

我可以通过在我的publish.yml中添加以下内容来修复它:

jobs:
tests:
uses: ./.github/workflows/build.yml
publish:
runs-on: ubuntu-latest
needs: [tests] # See build.yml file where the test job is defined

在我的构建。我必须添加以下内容:

on:
push:
paths-ignore:
- 'images/**'
- README.md
branches:
- master
pull_request:
branches:
- master
release:
types: [ created ]
workflow_call:

注意需要显式添加的workflow_call:条目。

最新更新