Skip to content

Commit

Permalink
ci(chromatic): allow to run on both master and other branches (#2006)
Browse files Browse the repository at this point in the history
  • Loading branch information
YossiSaadi authored Mar 11, 2024
1 parent db2e085 commit fd6500e
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 14 deletions.
38 changes: 38 additions & 0 deletions .github/actions/check-changed-packages/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Check Changed Packages
description: Check if there are any changed packages in the monorepo

inputs:
exit_on_no_changes:
description: Whether to exit with a non-zero code if there are no changes.
required: false
default: "false"
outputs:
has_changes:
description: Whether there are any changes in the monorepo.
value: ${{ steps.check-changed-packages.outputs.has_changes }}

runs:
using: "composite"
steps:
- id: determine-since-flag
uses: ./.github/actions/determine-lerna-since-flag
- id: check-changed-packages
shell: bash
env:
SINCE_FLAG: ${{ steps.determine-since-flag.outputs.since_flag }}
run: |
changed_packages=$(yarn -s lerna ls $SINCE_FLAG --json --loglevel=error)
if [[ $changed_packages = "[]" ]]; then
echo "has_changes=false" >> $GITHUB_OUTPUT
echo "No packages to process as Lerna didn't detect any changes."
if [[ "${{ inputs.exit_on_no_changes }}" == "true" ]]; then
exit 1
else
echo "Continuing because exit_on_no_changes is not used."
fi
else
echo "has_changes=true" >> $GITHUB_OUTPUT
echo "Change detected:"
echo "$changed_packages"
fi
23 changes: 23 additions & 0 deletions .github/actions/determine-lerna-since-flag/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Determine Lerna since flag
description: Determine the --since flag value for Lerna commands based on the branch. Should be used for workflows/actions that can be used on both default branch and other branches.

outputs:
since_flag:
description: The --since flag value to use with Lerna commands.
value: ${{ steps.get-since-flag.outputs.since_flag }}

runs:
using: "composite"
steps:
- id: get-since-flag
shell: bash
run: |
if [[ "$GITHUB_REF" == "refs/heads/master" ]]; then
since_flag="--since"
echo "Running on master branch, checking for all changes."
else
since_flag="--since=origin/master"
echo "Not running on master branch, checking for changes since origin/master."
fi
echo "since_flag=$since_flag" >> $GITHUB_OUTPUT
31 changes: 17 additions & 14 deletions .github/workflows/chromatic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,27 @@ jobs:
chromatic-deployment:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Install dependencies
run: yarn
- name: Check if there are changed packages
id: changed-packages
continue-on-error: true
run: |
changed_packages=$(yarn -s lerna ls --since=origin/master --json --loglevel=error)
[[ $changed_packages = "[]" ]] && echo "HAS_CHANGES=false" >> $GITHUB_ENV || echo "HAS_CHANGES=true" >> $GITHUB_ENV
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Run Setup
uses: ./.github/actions/setup
with:
npm_token: ${{ secrets.npm_token }}
- id: check-changed-packages
uses: ./.github/actions/check-changed-packages
- id: determine-since-flag
uses: ./.github/actions/determine-lerna-since-flag
- name: Run Build
if: ${{ env.HAS_CHANGES == 'true' }}
if: ${{ steps.check-changed-packages.outputs.has_changes == 'true' }}
shell: bash
run: yarn lerna run build --since=origin/master --include-filtered-dependencies
env:
SINCE_FLAG: ${{ steps.determine-since-flag.outputs.since_flag }}
run: yarn lerna run build $SINCE_FLAG --include-filtered-dependencies
- name: Publish to Chromatic
uses: chromaui/action@v1
if: ${{ env.HAS_CHANGES == 'true' }}
if: ${{ steps.check-changed-packages.outputs.has_changes == 'true' }}
uses: chromaui/action@v11
with:
token: ${{ secrets.GITHUB_TOKEN }}
# 👇 Chromatic projectToken, if you are project maintainer refer to the manage page to obtain it.
projectToken: ${{ secrets.CHROMATIC_PROJECT_TOKEN }}
workingDir: packages/core

0 comments on commit fd6500e

Please sign in to comment.