Skip to content

Update build-and-upload-binaries.yml #2

Update build-and-upload-binaries.yml

Update build-and-upload-binaries.yml #2

name: Build and Upload Binaries on Tag
on:
push:
tags:
- 'v*' # Trigger the workflow for tags like v1.0, v2.1, etc.
jobs:
build:
runs-on: ubuntu-latest
steps:
# Checkout the code
- name: Checkout code
uses: actions/checkout@v2
# Set up Go environment
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: '1.21' # Set the Go version you're using
# Get the version from the tag
- name: Get version from tag
id: get_version
run: |
echo "VERSION=$(echo ${GITHUB_REF} | sed 's/refs\/tags\///')" >> $GITHUB_ENV
# Get the repository name from the GitHub context (e.g., steam-utils from bearaujus/steam-utils)
- name: Get repo name
id: get_repo_name
run: |
REPO_NAME=$(echo "${GITHUB_REPOSITORY}" | cut -d'/' -f2)
echo "REPO_NAME=${REPO_NAME}" >> $GITHUB_ENV
# Build binaries for all GOOS and GOARCH combinations
- name: Build binaries
run: |
# List of GOOS and GOARCH combinations
GOOS_ARCH_LIST=$(go tool dist list)
VERSION=${{ env.VERSION }}
REPO_NAME=${{ env.REPO_NAME }}
# Create a directory for the build outputs
mkdir -p binaries
# Loop through the GOOS and GOARCH combinations and build binaries
for GOOS_ARCH in $GOOS_ARCH_LIST; do
GOOS=$(echo $GOOS_ARCH | cut -d'/' -f1)
GOARCH=$(echo $GOOS_ARCH | cut -d'/' -f2)
# Set environment variables
export GOOS
export GOARCH
export CGO_ENABLED=1
# Build the binary with the required format and ldflags for version, name, arch, and goos
FILENAME="${REPO_NAME}-${VERSION}-${GOOS}-${GOARCH}"
go build -ldflags "-X main.name=${REPO_NAME} -X main.version=${VERSION} -X main.arch=${GOARCH} -X main.goos=${GOOS}" -o "binaries/${FILENAME}"
echo "Built binary: ${FILENAME}"
done
# Upload binaries to GitHub Releases
- name: Upload binaries to release
uses: softprops/action-gh-release@v1
with:
files: binaries/*
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}