|
| 1 | +name: Release |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_run: |
| 5 | + workflows: ["CI"] |
| 6 | + branches: [main] |
| 7 | + types: |
| 8 | + - completed |
| 9 | + |
| 10 | +permissions: |
| 11 | + contents: write # Required for creating GitHub releases |
| 12 | + packages: write # Required for pushing to GitHub Container Registry |
| 13 | + |
| 14 | +jobs: |
| 15 | + release: |
| 16 | + # Only run if the CI workflow was successful |
| 17 | + if: ${{ github.event.workflow_run.conclusion == 'success' }} |
| 18 | + runs-on: ubuntu-latest |
| 19 | + steps: |
| 20 | + - name: Checkout |
| 21 | + uses: actions/checkout@v3 |
| 22 | + with: |
| 23 | + fetch-depth: 0 # Required for version calculation |
| 24 | + |
| 25 | + - name: Set up Go |
| 26 | + uses: actions/setup-go@v4 |
| 27 | + with: |
| 28 | + go-version: ">=1.19" |
| 29 | + cache: true |
| 30 | + |
| 31 | + - name: Calculate Version |
| 32 | + id: calculate_version |
| 33 | + run: | |
| 34 | + # Start at 1.0.0 if no tags exist |
| 35 | + LATEST_TAG=$(git tag -l 'v*' | sort -V | tail -n 1) |
| 36 | + if [ -z "$LATEST_TAG" ]; then |
| 37 | + NEW_VERSION="1.0.0" |
| 38 | + else |
| 39 | + # Strip the 'v' prefix |
| 40 | + CURRENT_VERSION=${LATEST_TAG#v} |
| 41 | + # Split into major, minor, patch |
| 42 | + IFS='.' read -r MAJOR MINOR PATCH <<< "$CURRENT_VERSION" |
| 43 | + # Increment patch version |
| 44 | + PATCH=$((PATCH + 1)) |
| 45 | + NEW_VERSION="$MAJOR.$MINOR.$PATCH" |
| 46 | + fi |
| 47 | + echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_ENV |
| 48 | + echo "version=$NEW_VERSION" >> $GITHUB_OUTPUT |
| 49 | + echo "Calculated new version: v$NEW_VERSION" |
| 50 | +
|
| 51 | + - name: Create Tag |
| 52 | + run: | |
| 53 | + git config --local user.email "[email protected]" |
| 54 | + git config --local user.name "GitHub Action" |
| 55 | + git tag -a v${{ steps.calculate_version.outputs.version }} -m "Release v${{ steps.calculate_version.outputs.version }}" |
| 56 | + git push origin v${{ steps.calculate_version.outputs.version }} |
| 57 | +
|
| 58 | + - name: Login to GitHub Container Registry |
| 59 | + uses: docker/login-action@v2 |
| 60 | + with: |
| 61 | + registry: ghcr.io |
| 62 | + username: ${{ github.actor }} |
| 63 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 64 | + |
| 65 | + - name: Run GoReleaser |
| 66 | + uses: goreleaser/goreleaser-action@v4 |
| 67 | + with: |
| 68 | + distribution: goreleaser |
| 69 | + version: latest |
| 70 | + args: release --clean |
| 71 | + env: |
| 72 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 73 | + GITHUB_REPOSITORY_OWNER: ${{ github.repository_owner }} |
0 commit comments