fix: release.yml workflow #8
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: Automated Release | |
on: | |
push: | |
branches: | |
- main | |
- development | |
permissions: | |
contents: write | |
actions: read | |
jobs: | |
check-commit: | |
runs-on: ubuntu-latest | |
outputs: | |
should_release: ${{ steps.check.outputs.should_release }} | |
commit_message: ${{ steps.check.outputs.commit_message }} | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 1 | |
- name: Check commit message | |
id: check | |
run: | | |
COMMIT_MESSAGE=$(git log -1 --pretty=%B) | |
echo "commit_message=$COMMIT_MESSAGE" >> $GITHUB_OUTPUT | |
if [[ $COMMIT_MESSAGE =~ ^feat!.* ]] || [[ $COMMIT_MESSAGE =~ ^feat:.* ]] || [[ $COMMIT_MESSAGE =~ ^fix:.* ]]; then | |
echo "should_release=true" >> $GITHUB_OUTPUT | |
else | |
echo "should_release=false" >> $GITHUB_OUTPUT | |
echo "No version bump needed (commit: $COMMIT_MESSAGE)" | |
fi | |
release: | |
needs: check-commit | |
if: needs.check-commit.outputs.should_release == 'true' | |
runs-on: macos-latest | |
steps: | |
# Checkout the repository with full commit history | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
# Set up Swift environment | |
- name: Setup Swift | |
uses: swift-actions/setup-swift@v2 | |
with: | |
swift-version: "6.1" | |
# Cache SwiftPM dependencies | |
- name: Cache SwiftPM dependencies | |
uses: actions/cache@v3 | |
with: | |
path: .build | |
key: ${{ runner.os }}-swiftpm-${{ hashFiles('**/Package.resolved') }} | |
restore-keys: | | |
${{ runner.os }}-swiftpm- | |
# Resolve Swift package dependencies | |
- name: Resolve dependencies | |
run: swift package resolve | |
# Run tests to validate the package | |
- name: Run tests | |
run: swift test | |
continue-on-error: false | |
# Install GitHub CLI for release creation | |
- name: Setup GitHub CLI | |
run: | | |
brew install gh | |
echo "${{ secrets.GITHUB_TOKEN }}" | gh auth login --with-token | |
# Bump version, create tag, and release | |
- name: Bump version and create release | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: | | |
#!/bin/bash | |
set -e | |
# Get the current version from VERSION file (default to 0.0.0 if not exists) | |
CURRENT_VERSION=$(cat VERSION 2>/dev/null || echo "0.0.0") | |
IFS='.' read -r MAJOR MINOR PATCH <<< "$CURRENT_VERSION" | |
# Get the latest commit message | |
COMMIT_MESSAGE="${{ needs.check-commit.outputs.commit_message }}" | |
# Determine version bump based on conventional commits | |
if [[ $COMMIT_MESSAGE =~ ^feat!.* ]]; then | |
# Major bump for breaking changes | |
MAJOR=$((MAJOR + 1)) | |
MINOR=0 | |
PATCH=0 | |
elif [[ $COMMIT_MESSAGE =~ ^feat:.* ]]; then | |
# Minor bump for features | |
MINOR=$((MINOR + 1)) | |
PATCH=0 | |
elif [[ $COMMIT_MESSAGE =~ ^fix:.* ]]; then | |
# Patch bump for fixes | |
PATCH=$((PATCH + 1)) | |
else | |
echo "No version bump needed (commit: $COMMIT_MESSAGE)" | |
exit 0 | |
fi | |
# Form the new version | |
NEW_VERSION="$MAJOR.$MINOR.$PATCH" | |
echo "Bumping version from $CURRENT_VERSION to $NEW_VERSION" | |
# Update VERSION file | |
echo "$NEW_VERSION" > VERSION | |
# Commit version changes | |
git config user.name "GitHub Actions" | |
git config user.email "actions@github.com" | |
git add VERSION | |
git commit -m "chore(release): bump version to $NEW_VERSION [skip ci]" || echo "No changes to commit" | |
# Configure git to use GitHub token | |
git remote set-url origin https://x-access-token:${GITHUB_TOKEN}@github.com/maclong9/web-ui.git | |
# Determine if this is a pre-release (development branch) | |
IS_PRERELEASE="false" | |
BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD) | |
if [ "$BRANCH_NAME" = "development" ]; then | |
IS_PRERELEASE="true" | |
git push origin development | |
else | |
git push origin main | |
fi | |
# Create and push tag | |
git tag -a "v$NEW_VERSION" -m "Release v$NEW_VERSION" | |
git push origin "v$NEW_VERSION" | |
# Create and push tag | |
git tag -a "v$NEW_VERSION" -m "Release v$NEW_VERSION" | |
git push origin "v$NEW_VERSION" | |
# Generate release notes from commit message | |
RELEASE_NOTES="Automated release for v$NEW_VERSION\n\nChanges:\n- $COMMIT_MESSAGE" | |
# Create GitHub release | |
if [ "$IS_PRERELEASE" = "true" ]; then | |
# Create pre-release for development branch | |
gh release create "v$NEW_VERSION" \ | |
--title "Pre-release v$NEW_VERSION" \ | |
--notes "$RELEASE_NOTES" \ | |
--prerelease | |
else | |
# Create regular release for main branch | |
gh release create "v$NEW_VERSION" \ | |
--title "Release v$NEW_VERSION" \ | |
--notes "$RELEASE_NOTES" \ | |
--latest | |
fi |