DEV Community

Justin Poehnelt
Justin Poehnelt

Posted on • Originally published at justin.poehnelt.com on

Optimizing Parallel Jobs in a Github Workflow

This weekend I was trying to optimize a GitHub Actions workflow composed of three primary steps: build, preview, and test. I really wanted the build step to be followed by the in preview and test steps in parallel. My first thought was to use the actions/cache action to persist the workspace between jobs, but there was a little more boilerplate than I wanted because I didn’t need to cache between different workflow runs, only jobs for a particular commit.

So I created a new action with two child actions jpoehnelt/reusable-workspace/save and jpoehnelt/reusable-workspace/restore to simplify the process.

Performance

The usage looks like the following.

name: CI
on:
  pull_request:
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      # Build steps (save should be after this)
      - uses: jpoehnelt/reusable-workspace/save@v1
  preview:
    needs: [build]
    runs-on: ubuntu-latest
    steps:
      - uses: jpoehnelt/reusable-workspace/restore@v1
      # Additional steps (restore should be before this)
  test:
    needs: [build]
    runs-on: ubuntu-latest
    steps:
      - uses: jpoehnelt/reusable-workspace/restore@v1
        with:
          fail-on-miss: true
      # Additional steps (restore should be before this)
Enter fullscreen mode Exit fullscreen mode

My workflow now runs the build and then the preview and test steps in parallel. The preview job can also use a matrix without needing to rebuild the workspace after this change.

Optimized parallel jobs in a GitHub workflow

This new action is a great way to optimize your GitHub Actions workflows and reduce the time it takes to run your CI/CD pipeline. I hope you find it useful, just include the following:

- uses: jpoehnelt/reusable-workspace/restore@v1
- uses: jpoehnelt/reusable-workspace/save@v1
Enter fullscreen mode Exit fullscreen mode

Top comments (0)