Release #3
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: Release | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| release_type: | |
| description: "Type of release" | |
| required: true | |
| default: "patch" | |
| type: choice | |
| options: | |
| - major | |
| - minor | |
| - patch | |
| prerelease: | |
| description: "Is this a pre-release?" | |
| required: false | |
| default: false | |
| type: boolean | |
| permissions: | |
| contents: write | |
| packages: write | |
| jobs: | |
| release: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| with: | |
| # Fetch full history for changelog generation | |
| fetch-depth: 0 | |
| - name: Set up Go | |
| uses: actions/setup-go@v5 | |
| with: | |
| go-version: "1.25.1" | |
| - name: Cache Go modules | |
| uses: actions/cache@v4 | |
| with: | |
| path: ~/go/pkg/mod | |
| key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} | |
| restore-keys: | | |
| ${{ runner.os }}-go- | |
| - name: Download dependencies | |
| run: go mod download | |
| - name: Run tests | |
| run: go test -v ./... | |
| - name: Calculate next version | |
| id: version | |
| run: | | |
| # Get the latest tag, default to v0.0.0 if no tags exist | |
| LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0") | |
| echo "Latest tag: $LATEST_TAG" | |
| # Remove 'v' prefix and split version parts | |
| VERSION=${LATEST_TAG#v} | |
| IFS='.' read -ra VERSION_PARTS <<< "$VERSION" | |
| MAJOR=${VERSION_PARTS[0]:-0} | |
| MINOR=${VERSION_PARTS[1]:-0} | |
| PATCH=${VERSION_PARTS[2]:-0} | |
| # Remove any pre-release suffix from patch version | |
| PATCH=$(echo $PATCH | cut -d'-' -f1) | |
| echo "Current version: $MAJOR.$MINOR.$PATCH" | |
| # Calculate next version based on release type | |
| case "${{ github.event.inputs.release_type }}" in | |
| "major") | |
| MAJOR=$((MAJOR + 1)) | |
| MINOR=0 | |
| PATCH=0 | |
| ;; | |
| "minor") | |
| MINOR=$((MINOR + 1)) | |
| PATCH=0 | |
| ;; | |
| "patch") | |
| PATCH=$((PATCH + 1)) | |
| ;; | |
| esac | |
| NEXT_VERSION="v$MAJOR.$MINOR.$PATCH" | |
| echo "Next version: $NEXT_VERSION" | |
| echo "tag=$NEXT_VERSION" >> $GITHUB_OUTPUT | |
| - name: Check if calculated tag already exists | |
| run: | | |
| if git rev-parse "refs/tags/${{ steps.version.outputs.tag }}" >/dev/null 2>&1; then | |
| echo "Error: Tag ${{ steps.version.outputs.tag }} already exists" | |
| exit 1 | |
| fi | |
| - name: Create and push tag | |
| run: | | |
| git config user.name "github-actions[bot]" | |
| git config user.email "github-actions[bot]@users.noreply.github.com" | |
| git tag -a "${{ steps.version.outputs.tag }}" -m "Release ${{ steps.version.outputs.tag }}" | |
| git push origin "${{ steps.version.outputs.tag }}" | |
| - name: Run GoReleaser | |
| uses: goreleaser/goreleaser-action@v6 | |
| with: | |
| distribution: goreleaser | |
| version: "~> v2" | |
| args: release --clean | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| GORELEASER_CURRENT_TAG: ${{ steps.version.outputs.tag }} | |
| - name: Mark as pre-release if specified | |
| if: ${{ github.event.inputs.prerelease == 'true' }} | |
| run: | | |
| # Get the release ID and mark it as pre-release | |
| release_id=$(gh api repos/${{ github.repository }}/releases/tags/${{ steps.version.outputs.tag }} --jq '.id') | |
| gh api repos/${{ github.repository }}/releases/$release_id \ | |
| --method PATCH \ | |
| --field prerelease=true | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Release Summary | |
| run: | | |
| echo "🎉 Release ${{ steps.version.outputs.tag }} has been successfully created!" | |
| echo "📦 View the release: https://github.com/${{ github.repository }}/releases/tag/${{ steps.version.outputs.tag }}" | |
| echo "🔄 Release type: ${{ github.event.inputs.release_type }}" | |
| if [[ "${{ github.event.inputs.prerelease }}" == "true" ]]; then | |
| echo "⚠️ This is marked as a pre-release" | |
| fi |