build/release action #1
Workflow file for this run
This file contains 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: Build and Release | |
on: | |
push: | |
tags: | |
- "v*" # Trigger the workflow on tags starting with "v" | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
strategy: | |
matrix: | |
project: [hashit] | |
goos: [linux, darwin, windows] | |
goarch: [386, amd64, arm, arm64] | |
exclude: | |
# Exclude certain combinations | |
- goos: darwin | |
goarch: 386 | |
- goos: darwin | |
goarch: arm | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
- name: Set up Go | |
uses: actions/setup-go@v5 | |
with: | |
go-version: "1.21" # Adjust the Go version if necessary | |
- name: Build binary | |
run: | | |
mkdir -p bin | |
GOOS=${{ matrix.goos }} GOARCH=${{ matrix.goarch }} go build -o bin/${{ matrix.project }}_${{ matrix.goos }}_${{ matrix.goarch }}$([[ ${{ matrix.goos }} == 'windows' ]] && echo .exe || echo '') ./cmd/${{ matrix.project }}/main.go | |
- name: List files for debug | |
run: ls -la bin | |
- name: Move binary to release directory | |
run: | | |
mkdir -p release | |
ls -la release | |
mv bin/${{ matrix.project }}_${{ matrix.goos }}_${{ matrix.goarch }}* release/ | |
ls -la release | |
- name: List release directory for debug | |
run: ls -la release | |
- name: Upload Release Assets | |
uses: actions/upload-artifact@v4 | |
with: | |
name: release-assets-${{ matrix.project }}-${{ matrix.goos }}-${{ matrix.goarch }} | |
path: release/ | |
release: | |
runs-on: ubuntu-latest | |
needs: build | |
steps: | |
- name: Download Release Assets | |
uses: actions/download-artifact@v4 | |
with: | |
path: release-assets | |
pattern: release-assets-* | |
merge-multiple: true | |
- name: Create directories for compression | |
run: mkdir -p release-compressed | |
- name: Compress Release Assets | |
run: | | |
version=${GITHUB_REF#refs/tags/v} | |
# Set up name mappings | |
declare -A goos_map=( ["linux"]="linux" ["darwin"]="macos" ["windows"]="windows" ) | |
declare -A goarch_map=( ["386"]="x86" ["amd64"]="x64" ["arm"]="arm" ["arm64"]="arm64" ) | |
# Compress binaries | |
for file in release-assets/*; do | |
base_name=$(basename "$file") | |
echo "Compressing $base_name" | |
# Extract project, goos, and goarch | |
parts=(${base_name//_/ }) | |
project=${parts[0]} | |
goos=${parts[1]} | |
goarch_with_ext=${parts[2]} | |
goarch=${goarch_with_ext%%.*} # Remove the file extension | |
friendly_goos=${goos_map[$goos]} | |
friendly_goarch=${goarch_map[$goarch]} | |
# Rename the file to project-specific name or project-specific name.exe | |
if [[ $goos == "windows" ]]; then | |
mv "$file" release-assets/${project}.exe | |
file="release-assets/${project}.exe" | |
else | |
mv "$file" release-assets/${project} | |
file="release-assets/${project}" | |
fi | |
# Use .zip for macOS and Windows, .tar.gz for Linux | |
if [[ $goos == "windows" || $goos == "darwin" ]]; then | |
zip_filename="${project}-${version}-${friendly_goos}-${friendly_goarch}.zip" | |
zip release-compressed/${zip_filename} -j "$file" | |
else | |
tar_filename="${project}-${version}-${friendly_goos}-${friendly_goarch}.tar.gz" | |
tar -czvf release-compressed/${tar_filename} -C release-assets ${project} | |
fi | |
done | |
- name: Create GitHub Release | |
uses: softprops/action-gh-release@v2 | |
with: | |
files: release-compressed/* | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |