Auto Release #2
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 | |
| on: | |
| schedule: | |
| # Run at 01:00 UTC every day | |
| - cron: "0 1 * * *" | |
| workflow_dispatch: # Allow manual trigger | |
| permissions: | |
| contents: write | |
| jobs: | |
| check-and-release: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout repository | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 # Fetch all history for proper comparison | |
| - name: Check for new commits | |
| id: check_commits | |
| run: | | |
| # Get the latest PUBLISHED release (exclude drafts) | |
| LATEST_RELEASE=$(gh release list --exclude-drafts --limit 1 --json createdAt,tagName --jq '.[0]' 2>/dev/null || echo "") | |
| if [ -z "$LATEST_RELEASE" ]; then | |
| echo "No previous releases found" | |
| echo "has_commits=true" >> $GITHUB_OUTPUT | |
| echo "last_tag=" >> $GITHUB_OUTPUT | |
| else | |
| RELEASE_DATE=$(echo "$LATEST_RELEASE" | jq -r '.createdAt') | |
| LAST_TAG=$(echo "$LATEST_RELEASE" | jq -r '.tagName') | |
| echo "Latest release: $LAST_TAG at $RELEASE_DATE" | |
| echo "last_tag=$LAST_TAG" >> $GITHUB_OUTPUT | |
| # Check if there are commits since the latest release | |
| COMMITS_SINCE=$(git log --since="$RELEASE_DATE" --oneline | wc -l) | |
| echo "Commits since last release: $COMMITS_SINCE" | |
| # Require at least 2 commits for auto-release | |
| if [ "$COMMITS_SINCE" -ge 2 ]; then | |
| echo "has_commits=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "has_commits=false" >> $GITHUB_OUTPUT | |
| echo "Only $COMMITS_SINCE commit(s) found. Need at least 2 commits for auto-release." | |
| fi | |
| fi | |
| env: | |
| GH_TOKEN: ${{ github.token }} | |
| - name: Calculate next version | |
| id: next_version | |
| if: steps.check_commits.outputs.has_commits == 'true' | |
| run: | | |
| LAST_TAG="${{ steps.check_commits.outputs.last_tag }}" | |
| if [ -z "$LAST_TAG" ]; then | |
| # No previous tag, start with 0.0.1 | |
| NEW_VERSION="0.0.1" | |
| else | |
| # Extract version number (handles tags like "v1.2.3" or "1.2.3") | |
| VERSION=$(echo "$LAST_TAG" | sed 's/^v//') | |
| # Split version into major.minor.patch | |
| MAJOR=$(echo "$VERSION" | cut -d. -f1) | |
| MINOR=$(echo "$VERSION" | cut -d. -f2) | |
| PATCH=$(echo "$VERSION" | cut -d. -f3) | |
| # Increment patch version | |
| PATCH=$((PATCH + 1)) | |
| NEW_VERSION="$MAJOR.$MINOR.$PATCH" | |
| fi | |
| echo "version=$NEW_VERSION" >> $GITHUB_OUTPUT | |
| echo "New version: $NEW_VERSION" | |
| - name: Get release drafter output | |
| id: release_drafter | |
| if: steps.check_commits.outputs.has_commits == 'true' | |
| uses: release-drafter/[email protected] | |
| with: | |
| config-name: release-drafter.yml | |
| version: ${{ steps.next_version.outputs.version }} | |
| publish: true | |
| env: | |
| GITHUB_TOKEN: ${{ github.token }} | |
| - name: Create GitHub release | |
| if: steps.check_commits.outputs.has_commits == 'true' | |
| uses: softprops/[email protected] | |
| with: | |
| tag_name: ${{ steps.next_version.outputs.version }} | |
| name: ${{ steps.release_drafter.outputs.name }} | |
| body: ${{ steps.release_drafter.outputs.body }} | |
| draft: false | |
| prerelease: false | |
| - name: No commits to release | |
| if: steps.check_commits.outputs.has_commits == 'false' | |
| run: | | |
| echo "No new commits since last release. Skipping release creation." |