How to implement an "after success" action?

I have the following action, named “Node CI”, which runs on every push:

name: Node CI

on: [push]

env:
  CI: true

jobs:
  build:

    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [8.x, 13.x]

    steps:
    - uses: actions/checkout@v1
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v1
      with:
        node-version: ${{ matrix.node-version }}
    - run: yarn install
    - run: yarn run ci

After all build matrixes are successful (as in, successful build on both node 8 and 13), if the branch is master, I’d like to run a release process, which would consist of basically

name: Release
on:
  push:
    branches:
      - master
jobs:
  release:
    name: Release
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v1
      - name: Setup Node.js
        uses: actions/setup-node@v1
        with:
          node-version: 13.x
      - name: Install dependencies
        run: yarn install
- name: Build
run: yarn run build
      - name: Release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
        run: npx semantic-release

How can I make the “Release” workflow run only after all “Node CI” are finished and successful (including all matrixes)?

2 Likes

Put them both in the same workflow, and add:

jobs:
  build:
    ...
  release:
    if: ${{ github.ref == 'master' }}
    needs: build
    ...

Release job only gets triggered if we are in master branch and “build” job is successful and complete, including the whole matrix.

6 Likes

How would one implement an “else” script, such as using if for success, and something else for failure (what I’m trying to do)

1 Like

I’m struggling to solve the same issue. I want to be able to run steps “always” after a matrix job, and some only “on failure” or “on success”. Basically like a fan out and fan in.

2 Likes

Any solution to the if/else problem?

Take a look at the Workflow Conclusion Action.

https://github.com/marketplace/actions/workflow-conclusion-action

The action creates an environment variable that you can use check the status of the other jobs.

Here’s the example from their README:

on: push

name: CI

jobs:
  lint:
    name: ESLint
    runs-on: ubuntu-latest
    ...

  test:
    name: Coverage
    needs: lint
    strategy:
      matrix:
        node: ['11', '12']
    ...

  publish:
    name: Publish Package
    needs: test
    if: startsWith(github.ref, 'refs/tags/v')
    ...

  slack:
    name: Slack
    needs: publish # set needs only last job except this job
    runs-on: ubuntu-latest
    if: always() # set always
    steps:
        # run this action to get workflow conclusion
        # You can get conclusion by env (env.WORKFLOW_CONCLUSION)
      - uses: technote-space/workflow-conclusion-action@v1
      - uses: 8398a7/action-slack@v2
        with:
          # status: ${{ env.WORKFLOW_CONCLUSION }} # neutral, success, cancelled, timed_out, failure
          status: failure
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
        if: env.WORKFLOW_CONCLUSION == 'failure' # notify only if failure

Notice that the last step only runs if the other jobs failed.