Files
dsfree2api/.github/workflows/build.yml
T
nyoungo 3a21c53c0a ci: attach binaries to a GitHub Release on tag push
- release job (contents: write, gh CLI) creates the release with generated notes and uploads both binaries + sha256, --clobber for re-runs
- on tag builds the ldflags version comes from the tag itself
2026-10-07 17:45:58 +08:00

95 lines
2.5 KiB
YAML

name: build
on:
push:
branches: [ main ]
tags: [ 'v*' ]
pull_request:
branches: [ main ]
workflow_dispatch:
permissions:
contents: read
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
- run: go vet ./...
- run: go test ./...
build:
needs: test
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
- goos: linux
goarch: amd64
artifact: linux-amd64
- goos: windows
goarch: amd64
artifact: windows-amd64
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
- name: Build ${{ matrix.artifact }}
env:
GOOS: ${{ matrix.goos }}
GOARCH: ${{ matrix.goarch }}
CGO_ENABLED: '0'
run: |
set -euo pipefail
version="$(awk '/^VERSION/{print $3; exit}' Makefile)"
case "${GITHUB_REF:-}" in
refs/tags/*) version="${GITHUB_REF_NAME#v}" ;;
esac
ext=""
if [ "$GOOS" = "windows" ]; then ext=".exe"; fi
out="dsfree2api-${{ matrix.artifact }}${ext}"
go build -trimpath \
-ldflags "-s -w -X github.com/nyoungo/dsfree2api/internal/admin.Version=${version}" \
-o "$out" ./cmd/dsfree2api
sha256sum "$out" > "${out}.sha256"
echo "built: $out version: $version size: $(du -h "$out" | cut -f1)"
- uses: actions/upload-artifact@v4
with:
name: dsfree2api-${{ matrix.artifact }}
path: dsfree2api-${{ matrix.artifact }}*
if-no-files-found: error
release:
if: startsWith(github.ref, 'refs/tags/')
needs: build
runs-on: ubuntu-latest
permissions:
contents: write
actions: read
steps:
- name: Download binaries
uses: actions/download-artifact@v4
with:
path: dist
merge-multiple: true
- name: Attach assets to release
env:
GH_TOKEN: ${{ github.token }}
GH_REPO: ${{ github.repository }}
run: |
set -euo pipefail
tag="$GITHUB_REF_NAME"
ls -l dist/
if gh release view "$tag" >/dev/null 2>&1; then
gh release upload "$tag" dist/* --clobber
else
gh release create "$tag" --verify-tag --generate-notes --title "dsfree2api $tag" dist/*
fi
gh release view "$tag"