Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GitHub workflow : one job for each different git actions (push on master, push tags...)

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
like image 652
nowox Avatar asked Aug 02 '20 19:08

nowox


People also ask

How do I trigger one workflow from another action in GitHub?

If you do want to trigger a workflow from within a workflow run, you can use a personal access token instead of GITHUB_TOKEN to trigger events that require a token. You'll need to create a personal access token and store it as a secret.

Can you have multiple workflows in GitHub Actions?

Workflows are defined in the . github/workflows directory in a repository, and a repository can have multiple workflows, each of which can perform a different set of tasks.

Do GitHub Actions jobs run sequentially?

A workflow run is made up of one or more jobs , which run in parallel by default. To run jobs sequentially, you can define dependencies on other jobs using the jobs.


1 Answers

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
like image 154
Samira Avatar answered Nov 13 '22 16:11

Samira