-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ci: use GoReleaser for build & release binary (#49)
- Loading branch information
Showing
8 changed files
with
150 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters