|
| 1 | +name: Release |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + release_type: |
| 7 | + description: "Type of release" |
| 8 | + required: true |
| 9 | + default: "patch" |
| 10 | + type: choice |
| 11 | + options: |
| 12 | + - major |
| 13 | + - minor |
| 14 | + - patch |
| 15 | + prerelease: |
| 16 | + description: "Is this a pre-release?" |
| 17 | + required: false |
| 18 | + default: false |
| 19 | + type: boolean |
| 20 | + |
| 21 | +permissions: |
| 22 | + contents: write |
| 23 | + packages: write |
| 24 | + |
| 25 | +jobs: |
| 26 | + release: |
| 27 | + runs-on: ubuntu-latest |
| 28 | + steps: |
| 29 | + - name: Checkout |
| 30 | + uses: actions/checkout@v4 |
| 31 | + with: |
| 32 | + # Fetch full history for changelog generation |
| 33 | + fetch-depth: 0 |
| 34 | + |
| 35 | + - name: Set up Go |
| 36 | + uses: actions/setup-go@v5 |
| 37 | + with: |
| 38 | + go-version: "1.25.1" |
| 39 | + |
| 40 | + - name: Cache Go modules |
| 41 | + uses: actions/cache@v4 |
| 42 | + with: |
| 43 | + path: ~/go/pkg/mod |
| 44 | + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} |
| 45 | + restore-keys: | |
| 46 | + ${{ runner.os }}-go- |
| 47 | +
|
| 48 | + - name: Download dependencies |
| 49 | + run: go mod download |
| 50 | + |
| 51 | + - name: Run tests |
| 52 | + run: go test -v ./... |
| 53 | + |
| 54 | + - name: Calculate next version |
| 55 | + id: version |
| 56 | + run: | |
| 57 | + # Get the latest tag, default to v0.0.0 if no tags exist |
| 58 | + LATEST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "v0.0.0") |
| 59 | + echo "Latest tag: $LATEST_TAG" |
| 60 | +
|
| 61 | + # Remove 'v' prefix and split version parts |
| 62 | + VERSION=${LATEST_TAG#v} |
| 63 | + IFS='.' read -ra VERSION_PARTS <<< "$VERSION" |
| 64 | + MAJOR=${VERSION_PARTS[0]:-0} |
| 65 | + MINOR=${VERSION_PARTS[1]:-0} |
| 66 | + PATCH=${VERSION_PARTS[2]:-0} |
| 67 | +
|
| 68 | + # Remove any pre-release suffix from patch version |
| 69 | + PATCH=$(echo $PATCH | cut -d'-' -f1) |
| 70 | +
|
| 71 | + echo "Current version: $MAJOR.$MINOR.$PATCH" |
| 72 | +
|
| 73 | + # Calculate next version based on release type |
| 74 | + case "${{ github.event.inputs.release_type }}" in |
| 75 | + "major") |
| 76 | + MAJOR=$((MAJOR + 1)) |
| 77 | + MINOR=0 |
| 78 | + PATCH=0 |
| 79 | + ;; |
| 80 | + "minor") |
| 81 | + MINOR=$((MINOR + 1)) |
| 82 | + PATCH=0 |
| 83 | + ;; |
| 84 | + "patch") |
| 85 | + PATCH=$((PATCH + 1)) |
| 86 | + ;; |
| 87 | + esac |
| 88 | +
|
| 89 | + NEXT_VERSION="v$MAJOR.$MINOR.$PATCH" |
| 90 | + echo "Next version: $NEXT_VERSION" |
| 91 | + echo "tag=$NEXT_VERSION" >> $GITHUB_OUTPUT |
| 92 | +
|
| 93 | + - name: Check if calculated tag already exists |
| 94 | + run: | |
| 95 | + if git rev-parse "refs/tags/${{ steps.version.outputs.tag }}" >/dev/null 2>&1; then |
| 96 | + echo "Error: Tag ${{ steps.version.outputs.tag }} already exists" |
| 97 | + exit 1 |
| 98 | + fi |
| 99 | +
|
| 100 | + - name: Create and push tag |
| 101 | + run: | |
| 102 | + git config user.name "github-actions[bot]" |
| 103 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 104 | + git tag -a "${{ steps.version.outputs.tag }}" -m "Release ${{ steps.version.outputs.tag }}" |
| 105 | + git push origin "${{ steps.version.outputs.tag }}" |
| 106 | +
|
| 107 | + - name: Run GoReleaser |
| 108 | + uses: goreleaser/goreleaser-action@v6 |
| 109 | + with: |
| 110 | + distribution: goreleaser |
| 111 | + version: "~> v2" |
| 112 | + args: release --clean |
| 113 | + env: |
| 114 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 115 | + GORELEASER_CURRENT_TAG: ${{ steps.version.outputs.tag }} |
| 116 | + |
| 117 | + - name: Mark as pre-release if specified |
| 118 | + if: ${{ github.event.inputs.prerelease == 'true' }} |
| 119 | + run: | |
| 120 | + # Get the release ID and mark it as pre-release |
| 121 | + release_id=$(gh api repos/${{ github.repository }}/releases/tags/${{ steps.version.outputs.tag }} --jq '.id') |
| 122 | + gh api repos/${{ github.repository }}/releases/$release_id \ |
| 123 | + --method PATCH \ |
| 124 | + --field prerelease=true |
| 125 | + env: |
| 126 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 127 | + |
| 128 | + - name: Release Summary |
| 129 | + run: | |
| 130 | + echo "🎉 Release ${{ steps.version.outputs.tag }} has been successfully created!" |
| 131 | + echo "📦 View the release: https://github.com/${{ github.repository }}/releases/tag/${{ steps.version.outputs.tag }}" |
| 132 | + echo "🔄 Release type: ${{ github.event.inputs.release_type }}" |
| 133 | + if [[ "${{ github.event.inputs.prerelease }}" == "true" ]]; then |
| 134 | + echo "⚠️ This is marked as a pre-release" |
| 135 | + fi |
0 commit comments