Skip to content

Github Action checking if a workspace changed using Turborepo

License

Notifications You must be signed in to change notification settings

Trampoline-CX/action-turbo-changed

Use this GitHub action with your project
Add this Action to an existing workflow or create a new one
View on Marketplace

Repository files navigation

typescript-action status

turbo-changed Github Action

A Github Action making it easy to check if a local workspace changed using Turborepo.

Prerequisites

Since this Github Action relies on Turborepo, you'll need to have Turborepo set up in your repository before using this. Also, this action relies on the fact that you have a build pipeline configured as it makes use of turbo run build --dry-run behind the scenes.

⚠️ This was tested against a working monorepo using Yarn v3 (with Yarn Workspaces), but as it uses a standard Turborepo command behind the scenes, it should work with any monorepo setup compatible with Turborepo. See the example/yarn-workspaces folder for a working setup.

How to use

Here is a minimal example of how to use the action to check if a workspace changed:

name: 'example-push'
on: push

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
        with:
          fetch-depth: 0 # Necessary so we have commit history to compare to

      - name: package-a changed in last commit?
        id: changedAction
        uses: Trampoline-CX/action-turbo-changed@v1
        with:
          workspace: package-a
          from: HEAD^1 # Check for changes since previous commit (feel free to put a branch name instead in the form of origin/<branchName>)

      # Do something more meaningful here, like push to NPM, do heavy computing, etc.
      - name: Validate Action Output
        if: steps.changedAction.outputs.changed == 'true' # Check output if it changed or not (returns a boolean)
        run: echo 'package-a changed!'

ℹ️ Feel free to check the example_push.yml and example-pull_request.yml files as well for more examples.

Options

The following options can be passed to customize the behavior of the action:

Option Name Description Default Value
workspace (Required) The workspace name we are interested in. NA
from (Required) Start of the commit range to check (can be a commit hash, a branch name or HEAD^1). NA
to End of the commit range to check (can be a commit hash or branch). HEAD
working-directory Path to the root of the monorepo. ./
turbo-task-name Name of the turborepo task to run build

ℹ️ If using branch names, be sure to specify them as origin/<branchName>, as otherwise you'll be comparing to a local branch, which in most cases won't exist.

Outputs

This Github Action emits the following outputs that are useful to automate subsequent actions of your CI/CD pipelines:

Output Name Description
changed Boolean that will be true if the workspace option was changed. This is useful to conditionally run follow-up steps only if the workspace in question did change.
affectedWorkspaces Name of the workspaces for which the turbo-task-name had to run on because they were not present in the Turborepo cache. Useful for running matrix jobs on these workspaces for advanced use cases that might not be achievable through Turborepo tasks.

How it works?

Behind the scenes, this will run a Turborepo command looking like this to get the changed repositories:

yarn turbo run <turbo-task-name> --filter="<workspace>...[<from>...<to>]" --dry-run=json

The action then parses the returned JSON to check if the repository was changed or not.

Disclaimer

This project was bootstraped using typescript-action.