GitHub 工作流程:针对每个不同的 git 操作(推送主节点、推送标签......)

now*_*wox 6 github github-actions

我想设置我的工作流程以执行以下操作:

  • 在任何事件上(拉请求,在任何分支上推送)
    • 结帐代码
    • 构建项目
    • 运行测试
    • 上传其他作业的工件
  • 仅当 master 被推送时
    • 从以前的工作下载工件
    • 推送 GH 页面
  • 仅当标签被推送时
    • 从以前的工作下载工件
    • 创建发布
    • 将工件上传到发布

在我.github/workflowson指令中适用于所有工作,所以它在我的情况下不起作用。另一方面,action/upload-artifact仅在同一工作流程中有效。

实现所描述的工作流程的正确方法是什么?

on: push
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v1
        with:
          submodules: true
      - name: Build
        run: make all
      - uses: actions/upload-artifact@v2
        with:
          name: build
          path: dist/
      - name: Deploy to GitHub Pages
        filters: # <-----<<<< What I would like to do
          branch: master                
        uses: JamesIves/github-pages-deploy-action@3.5.9
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN  }}
          BRANCH: gh-pages
          FOLDER: dist/html
Run Code Online (Sandbox Code Playgroud)

Sam*_*ira 6

您可以向您的步骤添加条件,只需跳过不需要的部分,请参阅jobs.<id>.steps.if。至于检查什么github上下文是与当前运行的工作流相关的各种数据的金矿。例如

github.event_name  string  The name of the event that triggered the workflow run.
github.ref         string  The branch or tag ref that triggered the workflow run.
github.head_ref    string  The head_ref or source branch of the pull request in a workflow run.
Run Code Online (Sandbox Code Playgroud)

等等。

请注意,文档提到的部分只是冰山一角;github.event包含有用的东西墙。最好查看一些测试工作流程,看看每个事件提供了什么。


这样的事情应该工作:

github.event_name  string  The name of the event that triggered the workflow run.
github.ref         string  The branch or tag ref that triggered the workflow run.
github.head_ref    string  The head_ref or source branch of the pull request in a workflow run.
Run Code Online (Sandbox Code Playgroud)
- name: On any event (pull-request, push on any branches) 
  uses: some/action
Run Code Online (Sandbox Code Playgroud)
- name: Only when master is pushed
  if:   github.event_name == 'push' && github.ref == 'refs/heads/master'
  uses: some/action
Run Code Online (Sandbox Code Playgroud)