Skip to content

Refactor: Simplify CI/CD workflow and improve tag handling #4

Refactor: Simplify CI/CD workflow and improve tag handling

Refactor: Simplify CI/CD workflow and improve tag handling #4

name: Build and Release
on:
push:
branches:
- main
tags:
- 'v*'
pull_request:
branches:
- main
jobs:
auto-tag:
name: Auto Tag
runs-on: ubuntu-latest
# Only run on main branch pushes, not on tag pushes or PRs
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
outputs:
new_tag: ${{ steps.tag_version.outputs.new_tag }}
tag_created: ${{ steps.tag_version.outputs.new_tag != '' }}
permissions:
contents: write
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Auto Tag
id: tag_version
uses: mathieudutour/github-tag-action@v6.1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
default_bump: patch
create_annotated_tag: true
tag_prefix: v
- name: Print Tag Info
run: |
echo "New tag: ${{ steps.tag_version.outputs.new_tag }}"
echo "Tag created: ${{ steps.tag_version.outputs.new_tag != '' }}"
build:
name: Build Go Binary
runs-on: ${{ matrix.os }}
needs: auto-tag
# Always run build, even if auto-tag was skipped (e.g., for tag pushes)
if: always() && (needs.auto-tag.result == 'success' || needs.auto-tag.result == 'skipped')
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
include:
- os: ubuntu-latest
artifact_name: commit
asset_name: commit-linux-amd64
- os: windows-latest
artifact_name: commit.exe
asset_name: commit-windows-amd64.exe
- os: macos-latest
artifact_name: commit
asset_name: commit-macos-amd64
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.22' # Use the appropriate Go version for your project
- name: Get dependencies
run: go mod download
- name: Build
run: |
if [ "${{ matrix.os }}" = "windows-latest" ]; then
go build -v -o ${{ matrix.artifact_name }} -ldflags="-s -w" ./src
else
go build -v -o ${{ matrix.artifact_name }} -ldflags="-s -w" ./src
fi
shell: bash
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: ${{ matrix.asset_name }}
path: ${{ matrix.artifact_name }}
release:
name: Create Release
needs: [auto-tag, build]
# Run if a tag was created by auto-tag or this is a tag push
if: (needs.auto-tag.outputs.new_tag != '') || (github.event_name == 'push' && contains(github.ref, 'refs/tags/'))
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Download all artifacts
uses: actions/download-artifact@v4
- name: Debug info
run: |
echo "GitHub ref: ${{ github.ref }}"
echo "Event name: ${{ github.event_name }}"
echo "New tag: ${{ needs.auto-tag.outputs.new_tag }}"
echo "Directory contents:"
find . -type f | sort
- name: Create Release
id: create_release
uses: softprops/action-gh-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ needs.auto-tag.outputs.new_tag != '' && needs.auto-tag.outputs.new_tag || github.ref_name }}
name: Release ${{ needs.auto-tag.outputs.new_tag != '' && needs.auto-tag.outputs.new_tag || github.ref_name }}
files: |
./commit-linux-amd64/commit
./commit-windows-amd64.exe/commit.exe
./commit-macos-amd64/commit
draft: false
prerelease: false
generate_release_notes: true
publish:
name: Publish to GitHub Packages
needs: [auto-tag, build]
# Run if a tag was created by auto-tag or this is a tag push
if: (needs.auto-tag.outputs.new_tag != '') || (github.event_name == 'push' && contains(github.ref, 'refs/tags/'))
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Download Linux artifact
uses: actions/download-artifact@v4
with:
name: commit-linux-amd64
path: ./dist/linux-amd64
- name: Download Windows artifact
uses: actions/download-artifact@v4
with:
name: commit-windows-amd64.exe
path: ./dist/windows-amd64
- name: Download macOS artifact
uses: actions/download-artifact@v4
with:
name: commit-macos-amd64
path: ./dist/darwin-amd64
- name: Debug info
run: |
echo "Directory contents:"
find ./dist -type f | sort
- name: Make binaries executable
run: |
chmod +x ./dist/linux-amd64/commit || echo "Linux binary not found"
chmod +x ./dist/darwin-amd64/commit || echo "macOS binary not found"
- name: Prepare release assets
run: |
mkdir -p ./release
# Linux package
tar -C ./dist/linux-amd64 -czf ./release/commit-linux-amd64.tar.gz commit || echo "Failed to create Linux package"
# Windows package
zip -j ./release/commit-windows-amd64.zip ./dist/windows-amd64/commit.exe || echo "Failed to create Windows package"
# macOS package
tar -C ./dist/darwin-amd64 -czf ./release/commit-macos-amd64.tar.gz commit || echo "Failed to create macOS package"
echo "Release packages:"
ls -la ./release/
- name: Upload release packages to GitHub Releases
uses: softprops/action-gh-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ needs.auto-tag.outputs.new_tag != '' && needs.auto-tag.outputs.new_tag || github.ref_name }}
files: |
./release/commit-linux-amd64.tar.gz
./release/commit-windows-amd64.zip
./release/commit-macos-amd64.tar.gz