Run Github-Actions Step On Single Version

Viewed 373

I have a GA workflow to deploy my React app here. It runs the checkout/install/build/test steps on a matrix of three separate NodeJS versions, then runs deployment. My concern is that, given the matrix, this will try to run deployment three times, which can cause issues. Is there a good way to filter the last step so it only runs once?

My current best guess is changing lines 30-32 to the below, but I am not certain it would work correctly.

- name: Deploy
        if: matrix.node-version == '10.x' && (github.event_name == 'push' || github.event_name == 'workflow_dispatch')
        run: | ...
1 Answers

Add a separate deployment job that only runs on 1 node version and needs the previous matrix job (containing checkout/install/test) to succeed. I've also renamed your first job to test since that's more accurate about what it's doing.

jobs:
  test:
    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [10.x, 12.x, 14.x]

    steps:
      - uses: actions/checkout@v2
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node-version }}
      - run: npm ci
      - run: npm run build --if-present
      - run: npm test

  deploy:
    runs-on: ubuntu-latest
    needs: test
    steps:
      - uses: actions/checkout@v2
      # might need build steps here, I'm not 100% certain on your npm config
      - name: Deploy
        if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'
        run: |
          git config --global user.name $user_name
          git config --global user.email $user_email
          git remote set-url origin https://${github_token}@github.com/${repository}
          npm run deploy
        env:
          user_name: "github-actions[bot]"
          user_email: "github-actions[bot]@users.noreply.github.com"
          github_token: ${{ secrets.ACTIONS_DEPLOY_ACCESS_TOKEN }}
          repository: ${{ github.repository }}
Related