Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: use GoReleaser for build & release binary #49

Merged
merged 32 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
7555f06
chore: init goreleaser
heronimus Mar 21, 2024
2e19769
chore: goreleaser - fix ldflags
heronimus Mar 21, 2024
6764339
chore: goreleaser - update env var
heronimus Mar 22, 2024
d5cfb24
chore: update make build, add goreleaser-install function
heronimus Mar 22, 2024
d8366c3
ci: update build & release workflow using goreleaser
heronimus Mar 22, 2024
f1459a3
fix build workflow
heronimus Mar 22, 2024
2cbdbe9
fix build workflow
heronimus Mar 22, 2024
fc8844b
fix build workflow
heronimus Mar 22, 2024
dda2f2b
fix workflow test.yml call
heronimus Mar 22, 2024
ea284c2
ci: update ci action & dependency
heronimus Mar 22, 2024
3fbcf95
ci: fix go version
heronimus Mar 22, 2024
91c4bf6
chore: fix goreleaser ldflags
heronimus Mar 22, 2024
ce1f4d6
chore: nits
heronimus Mar 22, 2024
61f3a67
chore: goreleaser - disable auto latest
heronimus Mar 22, 2024
6369d74
chore: goreleaser - test ldflags secrets
heronimus Mar 22, 2024
1b8ecd3
chore: nits comments
heronimus Mar 22, 2024
491e5d0
chore: goreleaser try telemetry ldflags
heronimus Mar 22, 2024
23d23d9
chore: nits make build echo
heronimus Mar 22, 2024
1959d08
fix Posthog API key variable changes
heronimus Mar 25, 2024
1dd3c3b
ci: use github runner, nits
heronimus Mar 26, 2024
ae63e92
fix make clean & make install, nits
heronimus Mar 26, 2024
30df5dd
goreleaser: explicitly add goarch default value
heronimus Mar 26, 2024
60d2b59
gorelease: release notes change to append mode (default: keep-existing)
heronimus Mar 26, 2024
fec4062
init make release <wip>
heronimus Mar 26, 2024
3761f19
make release - handle wrong argument and GITHUB_TOKEN
heronimus Mar 27, 2024
c62c715
make release - nit comment
heronimus Mar 27, 2024
6f44411
make release - use annotated tag
heronimus Mar 27, 2024
478eb65
make release - nits comment
heronimus Mar 27, 2024
a30dafa
make release - perform clean release
heronimus Mar 27, 2024
6ecc48b
goreleaser - replace_existing_artifacts: true
heronimus Mar 27, 2024
d3cbed1
goreleaser - replace_existing_artifacts commented
heronimus Mar 27, 2024
4f8b50b
goreleaser - nits comment
heronimus Mar 27, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
heronimus marked this conversation as resolved.
Show resolved Hide resolved
- 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
heronimus marked this conversation as resolved.
Show resolved Hide resolved
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