Skip to content

Commit

Permalink
ci: fix version bump CI process (#34)
Browse files Browse the repository at this point in the history
* Fix yarn version bump

* Test release process using npm instead of yarn

* missing GH token

* new script for version bumps

* small fixes and missing vars

* capture errors

* fix output variable for version

* simplify naming conventions

* remove debug trigger

* Fix npm not pushing to repo

* make sure we're pushing to the right branch

* make sure release only creates when labels are right

* bump version manually

* [bot] New pkg version: 0.0.0-alpha.2

* prevent bumping version twice

* test script to check if ran

* tell the user it already happened

* better bump check scripr

* check the correct PR depending on the trigger

---------

Co-authored-by: GitHub Actions <[email protected]>
  • Loading branch information
gpmayorga and actions-user authored Dec 20, 2024
1 parent 6ae2d36 commit 098da8b
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 195 deletions.
40 changes: 0 additions & 40 deletions .github/ci-scripts/check-version-bump.sh

This file was deleted.

45 changes: 45 additions & 0 deletions .github/ci-scripts/pr-label-check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/bash

# Function to check if a specific label exists in a list
contains_label() {
local label="$1"
shift
for item in "$@"; do
if [[ "$item" == "$label" ]]; then
return 0
fi
done
return 1
}

# Fetch the most recent merged PR number
LAST_PR_NUMBER=$(gh pr list --state merged --json number --jq '.[0].number')

# Fetch the labels of the PR
labels=$(gh pr view "$LAST_PR_NUMBER" --json labels --jq '.labels[].name')

# Convert labels to an array
IFS=$'\n' read -rd '' -a label_array <<<"$labels"

# List of labels to check
LABELS_TO_CHECK=("major" "minor" "patch" "no-release" "alpha")

# Check for the specified labels
found_labels=0
for label in "${LABELS_TO_CHECK[@]}"; do
if contains_label "$label" "${label_array[@]}"; then
echo "$label"
((found_labels++))
fi
done

# If more than one label is found, print an error and exit with failure
if (( found_labels > 1 )); then
echo "Error: More than one release label found."
exit 1
fi

# If no specified label is found, return 'no-release'
if (( found_labels == 0 )); then
echo "no-release"
fi
30 changes: 0 additions & 30 deletions .github/ci-scripts/toggle-alpha.sh

This file was deleted.

76 changes: 69 additions & 7 deletions .github/workflows/create-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,87 @@ on:
push:
branches:
- main
pull_request_review:
types: [submitted]

jobs:
create-release:
runs-on: ubuntu-latest
permissions:
contents: write # Highly security sensitive. Do NOT add third party actions in this job
contents: write
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 2 # Need at least 2 commits to compare versions
fetch-depth: 0
ref: ${{ github.event.pull_request.head.ref || github.ref_name }}

- name: Check if version was bumped
- name: Check if version was already bumped
id: version_check
if: github.event.review.state == 'approved'
run: |
OUTPUT=$(./.github/ci-scripts/check-version-bump.sh)
echo "::warning::$OUTPUT"
# 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 last PR labels
id: check_labels
if: |
steps.version_check.outputs.skip_bump != 'true' &&
github.event.review.state == 'approved'
env:
GH_TOKEN: ${{ github.token }}
run: |
LAST_PR_NUMBER=$(gh pr list --state merged --json number --jq '.[0].number')
LABELS=$(gh pr view "$LAST_PR_NUMBER" --json labels --jq '.labels[].name')
echo "::notice::PR used to check version bump: #$LAST_PR_NUMBER"
echo "::notice::PR labels: "[${LABELS//$'\n'/,}]""
RELEASE_TYPE=$(./.github/ci-scripts/pr-label-check.sh) || exit_code=$?
if [ "$exit_code" -ne 0 ]; then
echo "::error::PR #"$LAST_PR_NUMBER" has more than one release label"
exit $exit_code
fi
echo "version=$RELEASE_TYPE" >> $GITHUB_OUTPUT
echo "::group::Output version"
echo "::notice::Release type: $RELEASE_TYPE"
if [ "$RELEASE_TYPE" == "no-release" ]; then
echo "::notice::PR is not flagged for release, skipping other steps"
fi
echo "::endgroup::"
- 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 "[email protected]"
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: steps.version_check.outcome == 'success'
if: |
github.event_name == 'push' &&
github.ref == 'refs/heads/main' &&
steps.check_labels.outputs.version != 'no-release'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
Expand All @@ -30,4 +92,4 @@ jobs:
--title "Release v${VERSION}" \
--generate-notes \
--prerelease \
--target "${COMMIT_SHA}"
--target "${GITHUB_SHA}"
28 changes: 6 additions & 22 deletions .github/workflows/naming-conventions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,24 @@ jobs:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: read
pull-requests: write
steps:
- name: Check PR Title follows conventional commit naming
id: check-pr-naming
uses: amannn/action-semantic-pull-request@v5
# continue-on-error: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
# https://www.conventionalcommits.org/en/v1.0.0/
requireScope: false

- uses: docker://agilepathway/pull-request-label-checker:v1.6.56
# continue-on-error: true
# https://github.com/agilepathway/label-checker
id: check-pr-labels
with:
one_of: major,minor,patch,no-release,alpha
repo_token: ${{ secrets.GITHUB_TOKEN }}

outputs:
labels_valid: ${{ steps.check-pr-labels.outputs.label_check }} # 'success' or 'failure'
title_error: ${{ steps.check-pr-naming.outputs.error_message }} # null or error message


handle-comments:
runs-on: ubuntu-latest
if: always()
needs: title-n-label-check
permissions:
pull-requests: write
contents: read
steps:

- name: Find existing PR title error comment
uses: peter-evans/find-comment@v2
id: find-title-comment
Expand Down Expand Up @@ -73,9 +57,9 @@ jobs:
Details:
```
${{ needs.title-n-label-check.outputs.title_error }}
${{ steps.check-pr-naming.outputs.error_message }}
edit-mode: replace
if: needs.title-n-label-check.outputs.title_error != null
if: steps.check-pr-naming.outputs.error_message != null

- name: Remove title error comment
env:
Expand All @@ -86,7 +70,7 @@ jobs:
-H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/${{ github.repository }}/issues/comments/$COMMENT_ID
if: needs.title-n-label-check.outputs.title_error == null
if: steps.check-pr-naming.outputs.error_message == null

- name: Add PR label error comment
id: pr-label-error
Expand All @@ -100,7 +84,7 @@ jobs:
PR must have ONLY one of the following labels: major, minor, patch, no-release, alpha.
Check the logs for more details: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
edit-mode: replace
if: needs.title-n-label-check.outputs.labels_valid != 'success'
if: steps.check-pr-labels.outputs.label_check != 'success'

- name: Remove label error comment
env:
Expand All @@ -111,4 +95,4 @@ jobs:
-H "Authorization: token $GITHUB_TOKEN" \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/${{ github.repository }}/issues/comments/$COMMENT_ID
if: needs.title-n-label-check.outputs.labels_valid == 'success'
if: steps.check-pr-labels.outputs.label_check == 'success'
95 changes: 0 additions & 95 deletions .github/workflows/prepare-release.yml

This file was deleted.

Loading

0 comments on commit 098da8b

Please sign in to comment.