Merge pull request #30 from BerryBytes/feat/sync-precommit #46
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Auto-Release CLI on Push to Main | |
| on: | |
| push: | |
| branches: | |
| - main | |
| workflow_dispatch: | |
| concurrency: | |
| group: release-${{ github.ref }} | |
| cancel-in-progress: true | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| jobs: | |
| release: | |
| if: "github.event_name == 'workflow_dispatch' || contains(github.event.head_commit.message, '[release]')" | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code with full history | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| fetch-tags: true | |
| - name: Install GitHub CLI | |
| run: | | |
| sudo apt-get update | |
| sudo apt-get install -y gh | |
| - name: Install dependencies | |
| run: | | |
| npm install --global prettier@3.5.3 | |
| npm install --global semver | |
| - name: Get current and previous tags | |
| id: tags | |
| run: | | |
| git fetch -a | |
| # CURRENT_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "") | |
| #this is test | |
| CURRENT_TAG=$(git tag --sort=-creatordate | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | head -n 1 || echo "") | |
| if [ -n "$CURRENT_TAG" ]; then | |
| # PREV_TAG=$(git describe --tags --abbrev=0 "$CURRENT_TAG"^ 2>/dev/null || echo "") | |
| PREV_TAG=$(git tag --sort=-creatordate | grep -E '^v[0-9]+\.[0-9]+\.[0-9]+$' | sed -n 2p || echo "") | |
| else | |
| PREV_TAG="" | |
| fi | |
| echo "Current tag: $CURRENT_TAG" | |
| echo "Previous tag: $PREV_TAG" | |
| echo "current_tag=$CURRENT_TAG" >> $GITHUB_OUTPUT | |
| echo "previous_tag=$PREV_TAG" >> $GITHUB_OUTPUT | |
| - name: Check for new commits | |
| id: check_commits | |
| run: | | |
| CURRENT_TAG=${{ steps.tags.outputs.current_tag }} | |
| if [ -z "$CURRENT_TAG" ]; then | |
| echo "No existing tags found - this is the first release" | |
| echo "skip=false" >> $GITHUB_OUTPUT | |
| exit 0 | |
| fi | |
| git fetch origin main --tags | |
| COMMIT_COUNT=$(git rev-list --count "$CURRENT_TAG..origin/main" --no-merges) | |
| if [ "$COMMIT_COUNT" -eq 0 ]; then | |
| echo "No new commits since $CURRENT_TAG. Skipping release." | |
| echo "skip=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "Found $COMMIT_COUNT new commits since $CURRENT_TAG" | |
| echo "skip=false" >> $GITHUB_OUTPUT | |
| fi | |
| - name: Determine new version | |
| if: steps.check_commits.outputs.skip != 'true' | |
| id: new_version | |
| run: | | |
| CURRENT_TAG=${{ steps.tags.outputs.current_tag }} | |
| if [ -z "$CURRENT_TAG" ]; then | |
| echo "First release - creating v0.1.0" | |
| echo "new_tag=v0.1.0" >> $GITHUB_OUTPUT | |
| exit 0 | |
| fi | |
| VERSION=$(echo "$CURRENT_TAG" | sed 's/^v//') | |
| COMMIT_MSGS=$(git log "$CURRENT_TAG..HEAD" --no-merges --pretty=%B) | |
| if echo "$COMMIT_MSGS" | grep -qi "BREAKING CHANGE"; then | |
| NEW_VERSION=$(semver "$VERSION" -i major) | |
| elif echo "$COMMIT_MSGS" | grep -qi "^feat:"; then | |
| NEW_VERSION=$(semver "$VERSION" -i minor) | |
| else | |
| NEW_VERSION=$(semver "$VERSION" -i patch) | |
| fi | |
| NEW_TAG="v$NEW_VERSION" | |
| while git rev-parse "$NEW_TAG" >/dev/null 2>&1; do | |
| NEW_VERSION=$(semver "$NEW_VERSION" -i patch) | |
| NEW_TAG="v$NEW_VERSION" | |
| done | |
| echo "new_tag=$NEW_TAG" >> $GITHUB_OUTPUT | |
| - name: Create changelog branch | |
| if: steps.check_commits.outputs.skip != 'true' | |
| id: create_branch | |
| run: | | |
| git config user.name "GitHub Actions Bot" | |
| git config user.email "actions@github.com" | |
| BRANCH_NAME="changelog-update-/${{ steps.new_version.outputs.new_tag }}" | |
| git checkout -b "$BRANCH_NAME" | |
| echo "branch_name=$BRANCH_NAME" >> $GITHUB_OUTPUT | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Generate Changelog | |
| if: steps.check_commits.outputs.skip != 'true' | |
| env: | |
| RELEASE_TAG: ${{ steps.new_version.outputs.new_tag }} | |
| PREVIOUS_TAG: ${{ steps.tags.outputs.current_tag }} | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| git tag -f "temp-${{ steps.new_version.outputs.new_tag }}" | |
| chmod +x .github/scripts/generate-changelog.sh | |
| .github/scripts/generate-changelog.sh | |
| git tag -d "temp-${{ steps.new_version.outputs.new_tag }}" | |
| git diff --quiet CHANGELOG.md || echo "Changelog updated" | |
| - name: Commit changelog updates | |
| if: steps.check_commits.outputs.skip != 'true' | |
| id: commit_changelog | |
| run: | | |
| git add CHANGELOG.md | |
| if git diff --staged --quiet; then | |
| echo "No changes to CHANGELOG.md. Skipping commit." | |
| echo "skip_pr=true" >> $GITHUB_OUTPUT | |
| exit 0 | |
| fi | |
| git commit -m "Update CHANGELOG.md for ${{ steps.new_version.outputs.new_tag }}" | |
| echo "skip_pr=false" >> $GITHUB_OUTPUT | |
| - name: Create and push release tag | |
| if: steps.check_commits.outputs.skip != 'true' && steps.commit_changelog.outputs.skip_pr != 'true' | |
| run: | | |
| RELEASE_COMMIT=$(git rev-parse HEAD) | |
| # Fetch full history and tags to avoid non-fast-forward errors | |
| git fetch --unshallow || true | |
| git fetch --tags | |
| git fetch origin "+refs/heads/*:refs/remotes/origin/*" | |
| # Rebase if the branch exists remotely | |
| CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD) | |
| git pull --rebase origin "$CURRENT_BRANCH" || true | |
| # Create and push tag forcefully | |
| git tag -f ${{ steps.new_version.outputs.new_tag }} -m "Release ${{ steps.new_version.outputs.new_tag }}" $RELEASE_COMMIT | |
| git push origin ${{ steps.new_version.outputs.new_tag }} --force | |
| - name: Push release branch | |
| if: steps.check_commits.outputs.skip != 'true' && steps.commit_changelog.outputs.skip_pr != 'true' | |
| run: | | |
| git push origin ${{ steps.create_branch.outputs.branch_name }} | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Create Pull Request | |
| if: steps.check_commits.outputs.skip != 'true' && steps.commit_changelog.outputs.skip_pr != 'true' | |
| run: | | |
| gh pr create \ | |
| --base main \ | |
| --head ${{ steps.create_branch.outputs.branch_name }} \ | |
| --title "Release ${{ steps.new_version.outputs.new_tag }}" \ | |
| --body "Automated release PR for ${{ steps.new_version.outputs.new_tag }}" \ | |
| --reviewer enzokamal || echo "PR creation failed, possibly no changes or reviewer issue" | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |