4
votes

Switching from Travis CI to GitHub Actions, I was wondering if there is a way to share common steps between jobs. For a project, I need each job to start with 3 actions: check out repository code, install Node.js v12, restore node_modules from the cache (if available). Actually I have added these 3 actions for each job, which works but is a bit verbose. Is there a way to say: "Each job must run these actions first" or something like that?

name: ci
on: [push, workflow_dispatch]

jobs:
  setup:
    runs-on: ubuntu-latest
    steps:
      - name: Check out repository code
        uses: actions/checkout@v2

      - name: Setup node
        uses: actions/setup-node@v2
        with:
          node-version: '12'

      - name: Cache node_modules
        id: cache-node-modules
        uses: actions/cache@v2
        with:
          path: node_modules
          key: node-modules-${{ hashFiles('package-lock.json') }}

      - name: Install dependencies
        run: npm install

  test_mysql:
    runs-on: ubuntu-latest
    needs: setup
    steps:
      - name: Check out repository code
        uses: actions/checkout@v2

      - name: Setup node
        uses: actions/setup-node@v2
        with:
          node-version: '12'

      - name: Restore node_modules
        id: cache-node-modules
        uses: actions/cache@v2
        with:
          path: node_modules
          key: node-modules-${{ hashFiles('package-lock.json') }}

      - name: Test MySQL 5
        run: npm run test-mysql
        env:
          DOCKER_MYSQL_TAG: 5

      - name: Test MySQL 8
        run: npm run test-mysql
        env:
          DOCKER_MYSQL_TAG: 8

  test_postgres:
    runs-on: ubuntu-latest
    needs: setup
    steps:
      - name: Check out repository code
        uses: actions/checkout@v2

      - name: Setup node
        uses: actions/setup-node@v2
        with:
          node-version: '12'

      - name: Restore node_modules
        id: cache-node-modules
        uses: actions/cache@v2
        with:
          path: node_modules
          key: node-modules-${{ hashFiles('package-lock.json') }}

      - name: Test Postgres 10
        run: npm run test-postgres
        env:
          DOCKER_POSTGRES_TAG: 10

      - name: Test Postgres 11
        run: npm run test-postgres
        env:
          DOCKER_POSTGRES_TAG: 11

      - name: Test Postgres 12
        run: npm run test-postgres
        env:
          DOCKER_POSTGRES_TAG: 12

  test_mariadb:
    runs-on: ubuntu-latest
    needs: setup
    steps:
      - name: Check out repository code
        uses: actions/checkout@v2

      - name: Setup node
        uses: actions/setup-node@v2
        with:
          node-version: '12'

      - name: Restore node_modules
        id: cache-node-modules
        uses: actions/cache@v2
        with:
          path: node_modules
          key: node-modules-${{ hashFiles('package-lock.json') }}

      - name: Test MariaDB 10.4
        run: npm run test-mariadb
        env:
          DOCKER_MARIADB_TAG: 10.4.12

  test_mssql:
    runs-on: ubuntu-latest
    needs: setup
    steps:
      - name: Check out repository code
        uses: actions/checkout@v2

      - name: Setup node
        uses: actions/setup-node@v2
        with:
          node-version: '12'

      - name: Restore node_modules
        id: cache-node-modules
        uses: actions/cache@v2
        with:
          path: node_modules
          key: node-modules-${{ hashFiles('package-lock.json') }}

      - name: Test MSSQL 2017
        run: npm run test-mssql
        env:
          DOCKER_MSSQL_TAG: 2017-CU17-ubuntu

      - name: Test MSSQL 2019
        run: npm run test-mssql
        env:
          DOCKER_MSSQL_TAG: 2019-latest

  test_sqlite:
    runs-on: ubuntu-latest
    needs: setup
    steps:
      - name: Check out repository code
        uses: actions/checkout@v2

      - name: Setup node
        uses: actions/setup-node@v2
        with:
          node-version: '12'

      - name: Restore node_modules
        id: cache-node-modules
        uses: actions/cache@v2
        with:
          path: node_modules
          key: node-modules-${{ hashFiles('package-lock.json') }}

      - name: Test SQLite
        run: npm run test-sqlite

  publish:
    runs-on: ubuntu-latest
    needs: [test_mysql, test_postgres, test_mariadb, test_mssql, test_sqlite]
    steps:
      - name: Check out repository code
        uses: actions/checkout@v2

      - name: Setup node
        uses: actions/setup-node@v2
        with:
          node-version: '12'

      - name: Restore node_modules
        id: cache-node-modules
        uses: actions/cache@v2
        with:
          path: node_modules
          key: node-modules-${{ hashFiles('package-lock.json') }}

      - name: Build
        run: npm run build

      - name: Check version changes
        uses: EndBug/version-check@v1
        id: check

      - name: Publish
        if: steps.check.outputs.changed == 'true' && github.ref == 'refs/heads/master'
        run: |
          npm set registry "https://registry.npmjs.org"
          npm set //registry.npmjs.org/:_authToken ${{ secrets.NPM_PUBLISH_TOKEN }}
          npm publish
2

2 Answers

1
votes

It looks that at the moment is not possible if among steps you want to share are uses.

It should be handled by composite actions but

What does composite run steps currently support?

For each run step in a composite action, we support:

  • name
  • id
  • run
  • env
  • shell
  • working-directory

In addition, we support mapping input and outputs throughout the action.

See docs for more info.

What does Composite Run Steps Not Support

We don't support setting conditionals, continue-on-error, timeout-minutes, "uses", and secrets on individual steps within a composite action right now.

0
votes

I took a look at your jobs. Each of them has needs: setup. Isn't that running the steps within setup, for each of those jobs?