Skip to content

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

Update build-and-upload-binaries.yml

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

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
# Install necessary build tools for cgo
- name: Install build tools for cgo
run: |
sudo apt-get update
sudo apt-get install -y gcc g++ libc6-dev
sudo apt-get install gcc-multilib
# 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 github.com/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 only if tag is new
- name: Build binaries
if: env.skip != 'true'
run: |
# Define a specific list of GOOS and GOARCH combinations
GOOS_ARCH_LIST=(
"windows/386"
"windows/amd64"
"windows/arm"
"windows/arm64"
"linux/386"
"linux/amd64"
"linux/arm"
"linux/arm64"
"darwin/amd64"
"darwin/arm64"
)
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)
# Disable cgo for windows builds (or other platforms that don't need cgo)
if [[ "$GOOS" == "windows" ]]; then
export CGO_ENABLED=0
else
export CGO_ENABLED=1
fi
# Skip problematic platforms if necessary (e.g., AIX or others)
if [[ "$GOOS" == "aix" && "$GOARCH" == "ppc64" ]]; then
echo "Skipping aix/ppc64 due to unsupported platform"
continue
fi
# Set environment variables
export GOOS
export GOARCH
# 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 }}