revy
revy

Reputation: 4727

GitHub Actions: share common actions between jobs

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

Upvotes: 10

Views: 5807

Answers (1)

Krzysztof Madej
Krzysztof Madej

Reputation: 40849

Now is possible to use uses in composite actions - please check this link

Composite action:

name: "Publish to Docker"
description: "Pushes built artifacts to Docker"

inputs:
  registry_username:
    description: “Username for image registry”
    required: true
  registry_password:
    description: “Password for image registry”
    required: true

runs:
  using: "composite"
  steps:
      - uses: docker/setup-buildx-action@v1

      - uses: docker/login-action@v1
        with:
          username: ${{inputs.registry_username}}
          password: ${{inputs.registry_password}}

      - uses: docker/build-push-action@v2
        with:
          context: .
          push: true
          tags: user/app:latest

And then:

on: [push]

jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: my-org/publish-docker@v1
        with:
          registry_username: ${{secrets.REGISTRY_USERNAME}}
          registry_password: ${{secrets.REGISTRY_PASSWORD}}

Original reply:

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.

Upvotes: 2

Related Questions