Skip to content

fix: create-release action #43

fix: create-release action

fix: create-release action #43

Workflow file for this run

name: Release and Versioning
on:
push:
branches:
- main
pull_request:
types: [opened, reopened, labeled, synchronize]
jobs:
version-bump:
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: write
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.event.pull_request.head.ref || github.ref_name }}
- name: Check PR labels
id: check_labels
env:
GH_TOKEN: ${{ github.token }}
run: |
PR_NUMBER=${{ github.event.pull_request.number }}
echo "::notice::PR used to check version bump: #$PR_NUMBER"
LABELS=$(gh pr view "$PR_NUMBER" --json labels --jq '.labels[].name')
# 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
}
echo "version=$RELEASE_TYPE" >> $GITHUB_OUTPUT
echo "::notice::Release label detected: $RELEASE_TYPE"
if [ "$RELEASE_TYPE" == "no-release" ]; then
echo "::notice::PR is not flagged for release, skipping bump"
fi
- name: Bump version
if: steps.check_labels.outputs.version != 'no-release'
env:
GH_TOKEN: ${{ github.token }}
run: |
chmod +x ./.github/ci-scripts/bump-check.sh
# Capture only the last line of output as BUMP_CHECK
BUMP_CHECK=$(./.github/ci-scripts/bump-check.sh | tail -n 1)
if [ "$BUMP_CHECK" == "true" ]; then
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
gh pr comment ${{ github.event.pull_request.number }} --body "Auto-bumped version for release, this cannot be the last commit as bots can't trigger the workflows to pass the PR checks"
fi
- name: Revert version bump
if: steps.check_labels.outputs.version == 'no-release'
env:
GH_TOKEN: ${{ github.token }}
run: |
MERGE_BASE=$(git merge-base origin/main HEAD)
COMMITS=$(git log $MERGE_BASE..HEAD --format=%H:%B)
# Find the latest version bump commit
VERSION_COMMIT_SHA=$(echo "$COMMITS" | grep ":\[bot\] New pkg version:" | head -n 1 | cut -d':' -f1)
if [ -n "$VERSION_COMMIT_SHA" ]; then
# Count version bumps and reverts
BUMP_COUNT=$(echo "$COMMITS" | grep -c ":\[bot\] New pkg version:" || true)
REVERT_COUNT=$(echo "$COMMITS" | grep -c ":Revert.*\[bot\] New pkg version:" || true)
if [ $REVERT_COUNT -lt $BUMP_COUNT ]; then
git config user.name "GitHub Actions"
git config user.email "actions@github.com"
git revert --no-edit $VERSION_COMMIT_SHA
git push
gh pr comment ${{ github.event.pull_request.number }} --body "Reverted version bump as no-release label was detected"
else
echo "::notice::Version bump has already been reverted, skipping"
fi
fi
create-release:
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v4
- name: Create GitHub Release
# if: 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}"