Skip to content

Release and Versioning #31

Release and Versioning

Release and Versioning #31

name: Release and Versioning
on:
push:
branches:
- main
pull_request_review:
types: [submitted]
jobs:
create-release:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.event.pull_request.head.ref || github.ref_name }}
- name: Check if version was already bumped
id: version_check
if: github.event.review.state == 'approved'
run: |
# Find the commit where the branch diverged from main
git fetch origin main
MERGE_BASE=$(git merge-base origin/main HEAD)
# Check commits between merge-base and current HEAD
COMMITS=$(git log $MERGE_BASE..HEAD --format=%B)
if echo "$COMMITS" | grep -q "\[bot\] New pkg version:"; then
VERSION_COMMIT=$(echo "$COMMITS" | grep "\[bot\] New pkg version:" | head -n 1)
echo "::warning::Version was already bumped in this branch with commit message: $VERSION_COMMIT"
echo "skip_bump=true" >> $GITHUB_OUTPUT
else
echo "No version bump found in branch commits"
echo "skip_bump=false" >> $GITHUB_OUTPUT
fi
- name: Check PR labels
id: check_labels
if: |
steps.version_check.outputs.skip_bump != 'true' &&
github.event.review.state == 'approved'
env:
GH_TOKEN: ${{ github.token }}
run: |
if [ "${{ github.event_name }}" == "pull_request_review" ]; then
PR_NUMBER=${{ github.event.pull_request.number }}
else
PR_NUMBER=$(gh pr list --state merged --json number --jq '.[0].number')
fi
LABELS=$(gh pr view "$PR_NUMBER" --json labels --jq '.labels[].name')
echo "::notice::PR used to check version bump: #$PR_NUMBER"
echo "::notice::PR labels: "[${LABELS//$'\n'/,}]""
# Make script executable
chmod +x ./.github/ci-scripts/pr-label-check.sh
# Run the script and capture both output and exit code
RELEASE_TYPE=$(./.github/ci-scripts/pr-label-check.sh $PR_NUMBER 2>&1) || {
# If script exits with non-zero, output the error and exit
echo "::error::Script failed with output: $RELEASE_TYPE"
exit 1
}
# Only proceed if we have a valid release type
if [ -z "$RELEASE_TYPE" ]; then
echo "::error::No valid release type determined"
exit 1
fi
echo "version=$RELEASE_TYPE" >> $GITHUB_OUTPUT
echo "::notice::Release type: $RELEASE_TYPE"
if [ "$RELEASE_TYPE" == "no-release" ]; then
echo "::notice::PR is not flagged for release, skipping other steps"
fi
- name: Bump version
id: bump
if: |
steps.version_check.outputs.skip_bump != 'true' &&
steps.check_labels.outputs.version != 'no-release' &&
github.event.review.state == 'approved'
run: |
git config user.name "GitHub Actions"
git config user.email "actions@github.com"
if [ "${{ steps.check_labels.outputs.version }}" == "alpha" ]; then
npm version -m "[bot] New pkg version: %s" --preid=alpha prerelease
else
npm version -m "[bot] New pkg version: %s" ${{ steps.check_labels.outputs.version }}
fi
git push
- name: Create GitHub Release
if: |
github.event_name == 'push' &&
github.ref == 'refs/heads/main' &&
steps.check_labels.outputs.version != 'no-release'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
VERSION=$(node -p "require('./package.json').version")
gh release create "v${VERSION}" \
--title "Release v${VERSION}" \
--generate-notes \
--prerelease \
--target "${GITHUB_SHA}"