fix ci releases #46
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: | |
| push: | |
| branches: [ "main" ] | |
| tags: | |
| - 'v*.*.*' # Trigger on tag pushes | |
| workflow_dispatch: | |
| inputs: | |
| tag: | |
| description: 'Optional tag to release (e.g., v2.0.0)' | |
| required: false | |
| permissions: | |
| contents: write | |
| actions: write | |
| jobs: | |
| test: | |
| name: Run Tests & Generate Coverage Badge | |
| runs-on: ubuntu-latest | |
| outputs: | |
| coverage: ${{ steps.coverage.outputs.coverage }} | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Setup Go | |
| uses: actions/setup-go@v4 | |
| with: | |
| go-version-file: 'go.mod' | |
| - name: Run Tests and Generate Coverage | |
| id: coverage | |
| run: | | |
| go test -v ./... -covermode=count -coverprofile=coverage.out | |
| COVERAGE=$(go tool cover -func=coverage.out | grep total | awk '{print $3}' | sed 's/%//') | |
| echo "Coverage: $COVERAGE%" | |
| echo "coverage=$COVERAGE" >> $GITHUB_ENV | |
| echo "coverage=$COVERAGE" >> $GITHUB_OUTPUT | |
| if (( $(echo "$COVERAGE < 80" | bc -l) )); then | |
| echo "Test coverage is below 80%, failing the build." | |
| exit 1 | |
| fi | |
| go tool cover -func=coverage.out -o=coverage.out | |
| - name: Go Coverage Badge | |
| uses: tj-actions/coverage-badge-go@v3 | |
| with: | |
| filename: coverage.out | |
| coverage: ${{ env.coverage }} | |
| - name: Verify Changed Files | |
| uses: tj-actions/verify-changed-files@v16 | |
| id: verify-changed-files | |
| with: | |
| files: README.md | |
| - name: Commit Changes | |
| if: steps.verify-changed-files.outputs.files_changed == 'true' | |
| run: | | |
| git config --local user.email "[email protected]" | |
| git config --local user.name "GitHub Action" | |
| git add README.md | |
| git commit -m "chore: Updated coverage badge." | |
| - name: Push Changes | |
| if: steps.verify-changed-files.outputs.files_changed == 'true' | |
| uses: ad-m/github-push-action@master | |
| with: | |
| github_token: ${{ secrets.GITHUB_TOKEN }} | |
| branch: ${{ github.head_ref }} | |
| release: | |
| name: Create Release | |
| needs: test | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 | |
| - name: Fetch All Tags | |
| run: git fetch --tags | |
| - name: Determine Tag | |
| id: version | |
| run: | | |
| if [ -n "${{ github.event.inputs.tag }}" ]; then | |
| # Use manual tag if provided | |
| new_tag="${{ github.event.inputs.tag }}" | |
| elif [[ "${GITHUB_REF}" == refs/heads/main ]]; then | |
| # Auto-increment patch version for main branch | |
| latest_tag=$(git describe --tags --abbrev=0 || echo "v0.0.0") | |
| IFS='.' read -r major minor patch <<<"${latest_tag#v}" | |
| if [ "$patch" -lt 9 ]; then | |
| patch=$((patch + 1)) | |
| else | |
| patch=0 | |
| if [ "$minor" -lt 9 ]; then | |
| minor=$((minor + 1)) | |
| else | |
| minor=0 | |
| major=$((major + 1)) | |
| fi | |
| fi | |
| new_tag="v${major}.${minor}.${patch}" | |
| else | |
| # Use pushed tag for tag-triggered workflow | |
| new_tag="${GITHUB_REF##*/}" | |
| fi | |
| # Ensure tag exists | |
| if ! git rev-parse "$new_tag" >/dev/null 2>&1; then | |
| git tag "$new_tag" | |
| git push origin "$new_tag" | |
| fi | |
| echo "tag=$new_tag" >> $GITHUB_OUTPUT | |
| - name: Create GitHub Release with Notes | |
| uses: softprops/action-gh-release@v2 | |
| with: | |
| tag_name: ${{ steps.version.outputs.tag }} | |
| name: ${{ steps.version.outputs.tag }} | |
| generate_release_notes: true | |
| body: | | |
| 🚀 **Automated Release** | |
| - **Triggered by:** [@${{ github.actor }}](https://github.com/${{ github.actor }}) | |
| env: | |
| GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |