7
votes

I would like to setup my workflow to do the following:

  • On any event (pull-request, push on any branches)
    • Checkout code
    • Build project
    • Run tests
    • Upload artifacts for other jobs
  • Only when master is pushed
    • Download artifacts from previous job
    • Push GH-pages
  • Only when a tag is pushed
    • Download artifacts from previous job
    • Create a release
    • Upload artifacts to the release

In my .github/workflows the on directives applies to all jobs so it won't work in my case. On the other hand, the action/upload-artifact only works within the same workflow.

What is the proper way to achieve the described workflow?

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/[email protected]
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN  }}
          BRANCH: gh-pages
          FOLDER: dist/html
1

1 Answers

8
votes

You could add conditions to your steps and simply skip parts which aren't needed, see jobs.<id>.steps.if. As for what to check, github context is a goldmine of various data related to currently running workflow. For example

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.

and so on.

Just a note that parts mentioned in documentation are just a tip of an iceberg; github.event contains wall of useful stuff. It's best to take a look in some test workflow and see what each event provides.


Something like that should work:

- name: On any event (pull-request, push on any branches) 
  uses: some/action
- name: Only when master is pushed
  if:   github.event_name == 'push' && github.ref == 'refs/heads/master'
  uses: some/action
- name: Only when a tag is pushed 
  if:   github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
  uses: some/action