name: Release on: push: tags: - 'v*' workflow_dispatch: inputs: tag: description: 'Tag to release (e.g. v1.2.3)' required: true jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: actions/setup-go@v5 with: go-version-file: go.mod - name: Test run: go test ./... - name: Lint run: go vet ./... release: needs: test runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 with: fetch-depth: 0 - uses: actions/setup-go@v5 with: go-version-file: go.mod - name: Build release binaries run: | VERSION="${{ github.event.inputs.tag || github.ref_name }}" for target in "linux/amd64" "linux/arm64" "darwin/amd64" "darwin/arm64" "windows/amd64"; do GOOS="${target%/*}" GOARCH="${target#*/}" EXT="" [ "$GOOS" = "windows" ] && EXT=".exe" NAME="unitdore-${GOOS}-${GOARCH}${EXT}" GOOS="$GOOS" GOARCH="$GOARCH" go build \ -ldflags "-X main.version=${VERSION}" \ -o "$NAME" . echo "Built $NAME" done - name: Create release and upload assets run: | TAG="${{ github.event.inputs.tag || github.ref_name }}" API="${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/releases" # Collect commits since the previous tag (or last 20 if no prior tag) PREV_TAG=$(git tag --sort=-version:refname | grep -v "^${TAG}$" | head -1) if [ -n "$PREV_TAG" ]; then RANGE="${PREV_TAG}..${TAG}" else RANGE="HEAD~20..HEAD" fi NOTES=$(git log "$RANGE" --pretty=format:"- %s" --no-merges) BODY="## What's changed"$'\n'"${NOTES}" # Escape for JSON BODY_JSON=$(printf '%s' "$BODY" | python3 -c 'import json,sys; print(json.dumps(sys.stdin.read()))') RELEASE=$(curl -s -X POST "$API" \ -H "Authorization: token ${GITHUB_TOKEN}" \ -H "Content-Type: application/json" \ -d "{\"tag_name\":\"${TAG}\",\"name\":\"${TAG}\",\"body\":${BODY_JSON}}") UPLOAD_URL=$(echo "$RELEASE" | grep -o '"upload_url":"[^"]*"' | cut -d'"' -f4) if [ -z "$UPLOAD_URL" ]; then echo "Failed to create release: $RELEASE" exit 1 fi for f in unitdore-*; do echo "Uploading $f..." curl -s -X POST "${UPLOAD_URL}?name=${f}" \ -H "Authorization: token ${GITHUB_TOKEN}" \ -H "Content-Type: application/octet-stream" \ --data-binary "@${f}" > /dev/null done env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}