Skip to content

Commit

Permalink
ci: use GoReleaser for build & release binary (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
heronimus authored Mar 28, 2024
1 parent 03d6f65 commit 07b0545
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 92 deletions.
57 changes: 8 additions & 49 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,61 +7,20 @@ on:
push:
branches:
- main
workflow_call:
inputs:
create_release:
description: 'Check if workflows called from Github Release event.'
default: false
required: false
type: boolean

env:
GO_VERSION: 1.21.1

jobs:
build-go:
name: Go
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [1.21.x]
goos: [linux, windows, darwin]
goarch: [amd64]
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up Docker Build
uses: docker/setup-buildx-action@v2
uses: actions/checkout@v4
- name: Install Go
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}
cache-dependency-path: "go.sum"
- name: Build ${{ matrix.goos }}/${{ matrix.goarch }}
id: build
run: |
output_name=world_${{ matrix.goos }}_${{ matrix.goarch }}
[ ${{ matrix.goos }} = "windows" ] && output_name+=".exe"
## Get version from tag name
bin_version=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
## Get sentry dsn from secret
sentry_dsn=${{ secrets.SENTRY_DSN }}
## Get posthog api key from secret
posthog_api_key=${{ secrets.POSTHOG_API_KEY }}
## Handle if event coming from PR, not release/tag
if [ "${{ github.event_name }}" == "pull_request" ]; then bin_version=${{ github.event.pull_request.head.sha }}; fi
env GOOS=${{ matrix.goos }} GOARCH=${{ matrix.goarch }} go build -ldflags "-X main.AppVersion=$bin_version -X main.SentryDsn=$sentry_dsn -X main.PosthogApiKey=$posthog_api_key" -o $output_name ./cmd/world
echo "output_name=$output_name" >> $GITHUB_OUTPUT
- name: Compress build binary
uses: a7ul/[email protected]
id: compress
with:
command: c
files: |
./${{ steps.build.outputs.output_name }}
outPath: ${{ steps.build.outputs.output_name }}.tar.gz
- name: Upload binary to Github artifact
if: ${{ inputs.create_release }}
uses: actions/upload-artifact@v3
uses: actions/setup-go@v5
with:
name: world-${{ matrix.goos }}-${{ matrix.goarch }}
path: ./world*.tar.gz
go-version: ${{ env.GO_VERSION }}
- name: Build
run: make build
4 changes: 2 additions & 2 deletions .github/workflows/lint-go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
name: Go
runs-on: namespace-profile-linux-4vcpu-8gb-cached
env:
GO_VERSION: 1.22.1
GO_VERSION: 1.21.1
steps:
- name: Checkout
uses: actions/checkout@v4
Expand Down Expand Up @@ -43,4 +43,4 @@ jobs:
version: v1.56.2
args: --timeout=10m --concurrency 8 -v ${{ steps.go-dir.outputs.path }}
## skip cache, use Namespace volume cache
skip-cache: true
skip-cache: true
46 changes: 25 additions & 21 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,38 @@ on:
release:
types: [created]

env:
GORELEASER_VERSION: v1.24.0
GO_VERSION: 1.21.1

permissions:
contents: write

jobs:
go-test:
name: Test
uses: ./.github/workflows/test.yml

go-build:
name: Build
uses: ./.github/workflows/build.yml
with:
create_release: true
secrets: inherit

release:
goreleaser:
name: Release World CLI binary
runs-on: ubuntu-latest
needs: [go-test, go-build]
strategy:
matrix:
go-version: [1.21.x]
needs: go-test
steps:
- name: Download World CLI build binary
uses: actions/download-artifact@v3
- name: Display downloaded files
run: |
ls -R
- name: Publish World CLI binary to corresponding Github Release
uses: skx/github-action-publish-binaries@master
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Golang
uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}
- name: Run World CLI goreleaser
uses: goreleaser/goreleaser-action@v5
with:
distribution: goreleaser
version: ${{ env.GORELEASER_VERSION }}
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
args: 'world*/*.tar.gz'
SENTRY_DSN: ${{ secrets.SENTRY_DSN }}
POSTHOG_API_KEY: ${{ secrets.POSTHOG_API_KEY }}
9 changes: 4 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ on:
push:
branches:
- main
workflow_call:

env:
GO_VERSION: 1.21.0
GO_VERSION: 1.21.1

jobs:
test-unit-coverage:
Expand All @@ -17,9 +18,6 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v4
# Setup Docker build to use Namespace workspace builder
- name: Configure Namespace powered Buildx
uses: namespacelabs/nscloud-setup-buildx-action@v0
- name: Setup Golang
uses: actions/setup-go@v5
with:
Expand All @@ -38,9 +36,10 @@ jobs:
shell: 'script -q -e -c "bash {0}"'
run: make test-coverage
- name: Upload coverage to Codecov
if: github.ref == 'refs/heads/main' || github.event_name == 'pull_request'
uses: codecov/codecov-action@v4
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
with:
fail_ci_if_error: true
directory: "./"
directory: "./"
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ go.work

# Build
world-cli
dist/

# Mac
.DS_Store
Expand All @@ -30,4 +31,4 @@ world-cli
.idea

# Test
/starter-game
/starter-game
60 changes: 60 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Make sure to check the documentation at https://goreleaser.com

# yaml-language-server: $schema=https://goreleaser.com/static/schema.json
# vim: set ts=2 sw=2 tw=0 fo=cnqoj

version: 1

before:
hooks:
- go mod tidy
- go generate ./...

builds:
- main: ./cmd/world
binary: world
env:
- CGO_ENABLED=0
- SENTRY_DSN={{ if index .Env "SENTRY_DSN" }}{{ .Env.SENTRY_DSN }}{{ else }}default_null{{ end }}
- POSTHOG_API_KEY={{ if index .Env "POSTHOG_API_KEY" }}{{ .Env.POSTHOG_API_KEY }}{{ else }}default_null{{ end }}
ldflags:
- -s -w
- -X main.AppVersion={{.Version}}
- -X main.SentryDsn={{.Env.SENTRY_DSN}}
- -X main.PosthogAPIKey={{.Env.POSTHOG_API_KEY}}
goos:
- linux
- windows
- darwin
goarch:
- amd64
- arm64
- "386"

release:
make_latest: false
mode: append
# uncomment until v1.25.0 stable released
# replace_existing_artifacts: true

archives:
- format: tar.gz
# this name template makes the OS and Arch compatible with the results of `uname`.
name_template: >-
{{ .ProjectName }}_
{{- title .Os }}_
{{- if eq .Arch "amd64" }}x86_64
{{- else if eq .Arch "386" }}i386
{{- else }}{{ .Arch }}{{ end }}
{{- if .Arm }}v{{ .Arm }}{{ end }}
# use zip for windows archives
format_overrides:
- goos: windows
format: zip

changelog:
sort: asc
filters:
exclude:
- "^docs:"
- "^test:"
46 changes: 43 additions & 3 deletions makefiles/build.mk
Original file line number Diff line number Diff line change
@@ -1,4 +1,44 @@
SHELL := /bin/bash
goreleaser_version=v1.24.0

goreleaser-install:
@echo "--> Checking if goreleaser $(goreleaser_version) is installed"
@if [ "$$(goreleaser --version 2> /dev/null | grep GitVersion | awk '{ print $$2 }')" != "$(goreleaser_version)" ]; then \
echo "--> Installing goreleaser $(goreleaser_version)"; \
go install github.com/goreleaser/goreleaser@$(goreleaser_version); \
else \
echo "--> goreleaser $(goreleaser_version) is already installed"; \
fi

build:
go build -o $(PKGNAME) -v ./cmd/$(PKGNAME)

.PHONY: build
@$(MAKE) goreleaser-install
@goreleaser build --clean --snapshot
@echo "--> Build binary is available in the ./dist directory"

release:
$(eval args_count := $(words $(MAKECMDGOALS)))
$(eval args_release_tag := $(word 2, $(MAKECMDGOALS)))
@if [ "$(args_count)" != "2" ]; then \
echo " [Error] Wrong argument!";\
echo -e " --> usage: make release <tag-version>\n"; \
exit 1; \
fi
@if [ -z "${GITHUB_TOKEN}" ]; then\
echo " [Error] GITHUB_TOKEN env variable not found!"; \
echo " --> GoReleaser requires an API token with the repo scope to deploy the artifacts to GitHub."; \
echo -e " (https://github.com/settings/tokens/new)\n"; \
exit 1; \
fi
@echo "--> Release Tag: $(args_release_tag)"
@echo "--> git: tags current commit"
@git tag -a $(args_release_tag) -m "goreleaser: $(args_release_tag)"
@echo "--> git: push tag $(args_release_tag)"
@git push origin $(args_release_tag)
@echo "--> goreleaser release"
goreleaser release --clean

## do-nothing targets for extra unused args passed into @release
%:
@:

.PHONY: build goreleaser-install release
17 changes: 6 additions & 11 deletions makefiles/dev.mk
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,17 @@ clean:
@echo "--> Cleaning up"
@echo "--> Running go clean"
@go clean
@echo "--> Removing binary"
@rm -f $(PKGNAME)
@echo "--> Removing build './dist' directory"
@rm -rf ./dist
@echo "--> Removing coverage files"
@find . -type f -name "*.out" -exec rm -f {} \;


install:
@echo "--> Installing World CLI"
@echo "--> Building binary"
@go build -o $(PKGNAME) -v ./cmd/$(PKGNAME)
@echo "--> Copying binary to /usr/local/bin"
@mkdir -p /usr/local/bin
@cp $(PKGNAME) /usr/local/bin
@chmod +x /usr/local/bin/$(PKGNAME)
@echo "--> Building binary, install to /usr/local/bin"
@goreleaser build --clean --single-target --snapshot -o /usr/local/bin/$(PKGNAME)
@echo "--> Installed $(PKGNAME) to /usr/local/bin"
@echo "--> Cleaning up"
@rm -f $(PKGNAME)

.PHONY: clean install

.PHONY: clean install

0 comments on commit 07b0545

Please sign in to comment.