我对GitHub Actions相对较新,我有2个工作-一个运行我的测试,一个将我的项目部署到服务器上。
显然,我希望测试在每个分支上运行,但是部署应该只在某些东西被推到master时进行。
我正在努力寻找一种在特定分支上运行工作的方法。我知道在一个特定的分支上只运行整个工作流是可能的,但是这意味着我将有一个“测试”工作流和一个“部署”工作流。
这听起来像是一个解决方案,但它们是并行的。在理想的情况下,测试将首先运行,并且只有当测试成功时,才会启动部署作业。当使用两个单独的工作流时,情况就不是这样了。
我怎样才能做到这一点呢?是否可以在特定的分支上运行作业?
在最近的更新中,您现在可以将if条件放在工作级别。请在这里查看文档。https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idif
我测试了这个工作流,它在每次推送时运行作业测试,但只在主分支上运行部署。
name: my workflow
on: push
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Execute tests
run: exit 0
deploy:
runs-on: ubuntu-latest
needs: test
if: github.ref == 'refs/heads/master'
steps:
- name: Deploy app
run: exit 0
以下是我最初的答案,如果您喜欢有单独的工作流,则可以选择另一种解决方案。
第一个工作流运行于除master之外的每个分支。在此工作流中只运行测试。
on:
push:
branches:
- '*'
- '!master'
第二个工作流仅为master运行,如果测试成功通过,则运行您的测试和部署。
on:
push:
branches:
- master
大多数答案都为单个分支提供了解决方案。要限制作业在任何特定的分支集上运行,可以使用if条件和多个分离操作符(||);但这太啰嗦了,不尊重DRY原则。
使用contains函数可以较少重复地归档相同的内容。
使用包含:
contains('
refs/heads/dev
refs/heads/staging
refs/heads/production
', github.ref)
相比使用多个||:
github.ref == 'refs/heads/dev' || github.ref == 'refs/heads/staging' || github.ref == 'refs/heads/production' || …
完整的例子:
---
on: push
jobs:
test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Run tests
run: …
deployment:
name: Deployment
runs-on: ubuntu-latest
needs: [test]
if:
contains('
refs/heads/dev
refs/heads/staging
refs/heads/production
', github.ref)
steps:
- uses: actions/checkout@v2
- name: Deploy
run: …