Stasis

Go Binary

Cross-compile Go applications for multiple platforms

Go Binary Compilation

Cross-compile Go applications for multiple platforms with version injection.

Overview

This pipeline builds Go binaries for Linux, macOS, and Windows with:

  • Code quality checks (vet, fmt)
  • Unit tests with coverage
  • Multi-platform builds
  • Version injection via ldflags
  • SHA256 checksums
  • Artifact collection

Use Case

  • Go CLI tools
  • Go microservices
  • Cross-platform utilities
  • Release builds

Configuration

# runner.yaml
image:
  name: "golang"
  tag: "1.21-alpine"
  pull_policy: "IfNotPresent"

on:
  push:
    - "main"
    - "develop"
  tag:
    - "v*"

global_env:
  CGO_ENABLED: "0"
  GOOS: "linux"
  GOARCH: "amd64"

timeout: 1800  # 30 minutes

steps:
  setup:
    type: "pre"
    scripts:
      - go version
      - go env
      - go mod download
      - go mod verify

  vet:
    type: "exec"
    scripts:
      - go vet ./...
    continue_on_error: false

  fmt_check:
    type: "exec"
    scripts:
      - |
        if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then
          echo "Code is not formatted. Run 'go fmt ./...'"
          gofmt -s -d .
          exit 1
        fi
    continue_on_error: false

  test:
    type: "exec"
    scripts:
      - go test -v -race -coverprofile=coverage.out ./...
      - go tool cover -html=coverage.out -o coverage.html
    continue_on_error: false
    timeout: 600

  build_linux_amd64:
    type: "exec"
    scripts:
      - |
        VERSION=$(git describe --tags --always --dirty 2>/dev/null || echo "dev")
        BUILD_TIME=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
        COMMIT=$(git rev-parse --short HEAD 2>/dev/null || echo "unknown")
        
        go build -ldflags "-X main.Version=$VERSION -X main.BuildTime=$BUILD_TIME -X main.Commit=$COMMIT" \
          -o bin/app-linux-amd64 \
          ./cmd/app
    envs:
      GOOS: "linux"
      GOARCH: "amd64"
    continue_on_error: false

  checksum:
    type: "exec"
    scripts:
      - |
        cd bin
        sha256sum app-* > checksums.txt || shasum -a 256 app-* > checksums.txt
        cat checksums.txt
    when: "on_success"

Key Features

Version Injection

The pipeline injects build metadata via ldflags:

package main

var (
    Version   = "dev"
    BuildTime = "unknown"
    Commit    = "unknown"
)

Multi-Platform Builds

Build for multiple platforms in parallel:

build_linux_arm64:
  type: "exec"
  scripts:
    - go build -o bin/app-linux-arm64 ./cmd/app
  envs:
    GOOS: "linux"
    GOARCH: "arm64"

build_darwin_amd64:
  type: "exec"
  scripts:
    - go build -o bin/app-darwin-amd64 ./cmd/app
  envs:
    GOOS: "darwin"
    GOARCH: "amd64"

Checksums

Generate SHA256 checksums for verification:

checksum:
  type: "exec"
  scripts:
    - cd bin && sha256sum app-* > checksums.txt

Environment Variables

VariableDescriptionDefault
CGO_ENABLEDEnable CGO0
GOOSTarget OSlinux
GOARCHTarget architectureamd64

Artifacts

FileDescription
bin/app-linux-amd64Linux AMD64 binary
bin/app-linux-arm64Linux ARM64 binary
bin/app-darwin-amd64macOS AMD64 binary
bin/app-windows-amd64.exeWindows AMD64 binary
bin/checksums.txtSHA256 checksums
coverage.htmlTest coverage report

Next Steps

On this page