Skip to content

Commit

Permalink
Introduce version increment validation | main (#51)
Browse files Browse the repository at this point in the history
* Verify the version bump against the base branch (#45)

* Add patch (#49)

* Add second patch

* πŸš€ Release `observability@0.2.1 β†’ 0.2.2` (#50)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* Add patch

* πŸš€ Release `observability@0.2.29 β†’ 0.2.30` (#51)

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>

* Use publihsedPackages for tagged_release

* Reset source changes

* Add comments about fetch-depth: 0

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 8, 2024
1 parent 0cd6f20 commit 2a5ba95
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 39 deletions.
64 changes: 64 additions & 0 deletions .github/actions/changeset-status/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: Read changeset status

inputs:
args:
description: Additional arguments to pass to 'yarn changeset status'
required: false

outputs:
base_branch:
description: The base branch of the changeset
value: ${{ steps.status.outputs.base_branch }}
name:
description: The name of the package
value: ${{ steps.status.outputs.name }}
type:
description: "The type of version bump: 'patch' | 'minor' | 'major'"
value: ${{ steps.status.outputs.type }}
old_version:
description: The old version of the package
value: ${{ steps.status.outputs.old_version }}
new_version:
description: The new version of the package
value: ${{ steps.status.outputs.new_version }}

runs:
using: composite
steps:
- name: Read Changeset Status
shell: bash
id: status
run: |
OUTPUT=$(mktemp "${{runner.temp}}/changeset-output.XXXXXX.json")
if [ ! -f "$OUTPUT" ]; then
echo "🚨 Error: Failed to create temp file at: $OUTPUT"
exit 1
fi
echo "βœ… Temporary file created at: $OUTPUT"
# Run the changeset status command with relative path output
yarn changeset status ${{ inputs.args }} --output $(realpath --relative-to . "$OUTPUT")
if [ ! -s "$OUTPUT" ]; then
echo "🚨 Error: Temporary file is empty after running 'yarn changeset status'"
exit 1
fi
# if the .releases length is greater than 0, then the changeset is ready to be released
if jq -e '.releases | length > 0' "$OUTPUT" > /dev/null; then
NAME=$(jq -r '.releases[0].name' "$OUTPUT")
TYPE=$(jq -r '.releases[0].type' "$OUTPUT")
OLD_VERSION=$(jq -r '.releases[0].oldVersion' "$OUTPUT")
NEW_VERSION=$(jq -r '.releases[0].newVersion' "$OUTPUT")
echo "name=$NAME" >> $GITHUB_OUTPUT
echo "type=$TYPE" >> $GITHUB_OUTPUT
echo "old_version=$OLD_VERSION" >> $GITHUB_OUTPUT
echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
else
echo "⏩ No changeset found"
fi
BASE_BRANCH=$(jq -r '.baseBranch' .changeset/config.json)
echo "base_branch=$BASE_BRANCH" >> $GITHUB_OUTPUT
87 changes: 48 additions & 39 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
pull_request:
paths-ignore:
- "**.md"
- "!.changeset/**"

push:
branches:
Expand All @@ -25,6 +26,9 @@ jobs:
steps:
- name: Checkout Repository
uses: actions/checkout@v4
with:
# changesets status requires deeper history
fetch-depth: 0

- name: Setup environment
uses: ./.github/actions/setup-env
Expand All @@ -33,6 +37,34 @@ jobs:
run: |
git diff --name-only --exit-code --relative || (echo "\nUncommitted changes found πŸ‘†" && exit 1)
- name: Read Changeset Status
id: changeset-status
if: github.event_name == 'pull_request'
uses: ./.github/actions/changeset-status
with:
args: --since origin/${{ github.base_ref }}

- name: Check Version Changes
if: steps.changeset-status.outputs.type
run: |
BASE_BRANCH="${{ steps.changeset-status.outputs.base_branch }}"
VERSION_TYPE="${{ steps.changeset-status.outputs.type }}"
# Check for invalid version bumps based on the branch type
if [[ "$BASE_BRANCH" =~ ^v[0-9]+\.[0-9]+\.x$ && "$VERSION_TYPE" != "patch" ]]; then
echo "🚨 Detected '$VERSION_TYPE' bump."
echo "🚨 Only 'patch' versions are allowed for the maintenance branch $BASE_BRANCH."
exit 1
fi
if [[ "$BASE_BRANCH" =~ ^v[0-9]+\.x\.x$ && "$VERSION_TYPE" == "major" ]]; then
echo "🚨 Detected '$VERSION_TYPE' bump."
echo "🚨 Only 'patch' or 'minor' versions are allowed for the maintenance branch $BASE_BRANCH."
exit 1
fi
echo "βœ… Detected '$VERSION_TYPE' bump."
- name: Detect File Changes
id: changed-files
uses: tj-actions/changed-files@v41
Expand Down Expand Up @@ -61,77 +93,54 @@ jobs:
# runs only on pushes so it does not run on PRs
if: github.event_name == 'push'
outputs:
tagged_release: ${{ steps.changesets-output.outputs.tagged_release }}
tagged_release: ${{ steps.release-info.outputs.tag }}
steps:
- name: Checkout Repository
uses: actions/checkout@v3
with:
# changesets status requires deeper history
fetch-depth: 0

- name: Setup environment
uses: ./.github/actions/setup-env

- name: Read Changesets Config
id: changeset-config
run: |
BASE_BRANCH=$(jq -r '.baseBranch' .changeset/config.json)
echo "base_branch=$BASE_BRANCH" >> $GITHUB_OUTPUT
- name: Determine Release Details
id: release-details
# runs only when the .changeset.baseBranch is the target branch
if: steps.changeset-config.outputs.base_branch == '${{ github.ref_name }}'
run: |
OUTPUT=$(mktemp ${{runner.temp}}/changeset-output.XXXXXX.json)
if [ ! -f "$OUTPUT" ]; then
echo "Failed to create temp file at: $OUTPUT"
exit 1
fi
echo "Temporary file created at: $OUTPUT"
# the `changeset status` command uses path.join so the path should be relative
yarn changeset status --output $(realpath --relative-to . $OUTPUT)
if [ ! -s "$OUTPUT" ]; then
echo "Error: Temporary file is empty after running 'yarn changeset status'"
exit 1
fi
MESSAGE=$(jq -r 'if .releases | length > 0 then .releases[0] | "πŸš€ Release `\(.name)@\(.oldVersion) β†’ \(.newVersion)`" else "Publishing release..." end' "$OUTPUT")
echo "message=$MESSAGE" >> $GITHUB_OUTPUT
- name: Read Changeset Status
id: changeset-status
uses: ./.github/actions/changeset-status

# creates a PR if there are changesets or assign the new tag if the PR is merged
- name: Create Release Pull Request or Publish
id: changesets
if: steps.release-details.outputs.message
# runs only when the changeset's base_branch is the target branch
if: steps.changeset-status.outputs.base_branch == '${{ github.ref_name }}'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
MESSAGE: "πŸš€ Release `${{ steps.changeset-status.outputs.name }}@${{ steps.changeset-status.outputs.old_version }} β†’ ${{ steps.changeset-status.outputs.new_version }}`"
uses: changesets/action@v1
with:
title: ${{ steps.release-details.outputs.message }}
commit: ${{ steps.release-details.outputs.message }}
title: ${{ env.MESSAGE }}
commit: ${{ env.MESSAGE }}
# define the publish command, so changeset creates a release in GitHub
publish: yarn release-tag

- name: Extract Published Version
id: changesets-output
- name: Extract Release Info
id: release-info
if: steps.changesets.outputs.published == 'true'
# extract the first package name and version and format as `name-version`
# the publish-pkgs script expects `name-version` but not `name@version`
# so `release-tag` creates both but outputs only the default changeset format
run: |
TAGGED_RELEASE=$(echo '${{ steps.changesets.outputs.publishedPackages }}' | jq -r '.[0] | "\(.name)-\(.version)"')
echo "tagged_release=$TAGGED_RELEASE" >> $GITHUB_OUTPUT
TAG=$(echo '${{ steps.changesets.outputs.publishedPackages }}' | jq -r '.[0] | "\(.name)-\(.version)"')
echo "tag=$TAG" >> $GITHUB_OUTPUT
- name: Preare Build Artifact
id: prepare-artifact
if: steps.changesets-output.outputs.tagged_release
if: steps.release-info.outputs.tag
run: |
yarn publish-pkgs \
-s "${{ github.repository }}" \
-b "${{ env.HELM_CHART_BRANCH }}" \
-t "${{ steps.changesets-output.outputs.tagged_release }}"
-t "${{ steps.release-info.outputs.tag }}"
- name: Upload Charts Artifact
if: steps.prepare-artifact.conclusion == 'success'
Expand Down

0 comments on commit 2a5ba95

Please sign in to comment.