Skip to content

Commit

Permalink
use goreleaser docker image for cross compile in release step (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
pimg authored Jul 22, 2024
1 parent d955ee4 commit e0c24e0
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 103 deletions.
43 changes: 16 additions & 27 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,34 +1,23 @@
name: release

on:
push:
tags:
- 'v*'

permissions:
contents: write
id-token: write
packages: write

- v*
jobs:
goreleaser:
runs-on: macos-latest
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: checkout code
uses: actions/checkout@v2
with:
fetch-depth: 0 # this is important, otherwise it won't checkout the full tree (i.e. no previous tags)
- uses: actions/setup-go@v3
with:
go-version: 1.22
cache: true
- uses: goto-bus-stop/setup-zig@v2
- name: Set output
id: macos_sdk
run: echo "path=$(xcrun --show-sdk-path)" >> $GITHUB_OUTPUT
- uses: goreleaser/goreleaser-action@v4 # run goreleaser
with:
version: latest
args: release --clean
env:
SDK_PATH: ${{ steps.macos_sdk.outputs.path }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
submodules: 'true'
- run: git fetch --prune --unshallow
- name: setup dependencies
uses: actions/setup-go@v2
- name: release dry run
run: make release-dry-run
- name: setup release environment
run: |-
echo 'GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}' > .release-env
- name: release publish
run: make release
108 changes: 33 additions & 75 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,93 +7,51 @@ before:
gomod:
proxy: true

env:
- CGO_ENABLED=1
builds:
- id: cg-darwin
- id: darwin-amd64
main: ./
binary: cg
goos:
- darwin
goarch:
- amd64
- arm64
ldflags:
- -s -w
flags:
- -trimpath
- -buildmode=pie
env:
- CGO_ENABLED=1
- >-
{{- if eq .Os "darwin" }}
{{- if eq .Arch "amd64"}}CC=zig c -target x86_64-macos-none -F{{ .Env.SDK_PATH }}/System/Library/Frameworks{{- end }}
{{- if eq .Arch "arm64"}}CC=zig c -target aarch64-macos-none -F{{ .Env.SDK_PATH }}/System/Library/Frameworks{{- end }}
{{- end }}
- >-
{{- if eq .Os "darwin" }}
{{- if eq .Arch "amd64"}}CC=zig c++ -target x86_64-macos-none -F{{ .Env.SDK_PATH }}/System/Library/Frameworks{{- end }}
{{- if eq .Arch "arm64"}}CC=zig c++ -target aarch64-macos-none -F{{ .Env.SDK_PATH }}/System/Library/Frameworks{{- end }}
{{- end }}
- id: cg-linux
- PKG_CONFIG_SYSROOT_DIR=/sysroot/macos/amd64
- PKG_CONFIG_PATH=/sysroot/macos/amd64/usr/local/lib/pkgconfig
- CC=o64-clang
- CXX=o64-clang++
flags:
- -mod=readonly
ldflags:
- -s -w -X main.version={{.Version}}
- id: linux-armhf
main: ./
binary: cg
goos:
- linux
goarch:
- amd64
- arm64
ldflags:
- -s -w
flags:
- -trimpath
- arm
goarm:
- 7
env:
- CGO_ENABLED=1
- >-
{{- if eq .Os "linux" }}
{{- if eq .Arch "amd64" }}CC=zig c -target x86_64-linux-musl{{- end }}
{{- if eq .Arch "arm64"}}CC=zig c -target aarch64-linux-musl{{- end }}
{{- end }}
- >-
{{- if eq .Os "linux" }}
{{- if eq .Arch "amd64" }}CC=zig c++ -target x86_64-linux-musl{{- end }}
{{- if eq .Arch "arm64"}}CC=zig c++ -target aarch64-linux-musl{{- end }}
{{- end }}
- id: cg-windows
goos:
- windows
goarch:
- amd64
- arm64
ldflags:
- -s -w
- CC=arm-linux-gnueabihf-gcc
- CXX=arm-linux-gnueabihf-g++
- CGO_CFLAGS=--sysroot=/sysroot/linux/armhf
- CGO_LDFLAGS=--sysroot=/sysroot/linux/armhf
- PKG_CONFIG_SYSROOT_DIR=/sysroot/linux/armhf
- PKG_CONFIG_PATH=/sysroot/linux/armhf/opt/vc/lib/pkgconfig:/sysroot/linux/armhf/usr/lib/arm-linux-gnueabihf/pkgconfig:/sysroot/linux/armhf/usr/lib/pkgconfig:/sysroot/linux/armhf/usr/local/lib/pkgconfig
flags:
- -trimpath
env:
- CGO_ENABLED=1
- >-
{{- if eq .Os "windows" }}
{{- if eq .Arch "amd64" }}CC=zig c -target x86_64-windows-gnu{{- end }}
{{- if eq .Arch "arm64"}}CC=zig c -target aarch64-windows-gnu{{- end }}
{{- end }}
- >-
{{- if eq .Os "windows" }}
{{- if eq .Arch "amd64" }}CC=zig c++ -target x86_64-windows-gnu{{- end }}
{{- if eq .Arch "arm64"}}CC=zig c++ -target aarch64-windows-gnu{{- end }}
{{- end }}
- -mod=readonly
ldflags:
- -s -w -X main.version={{.Version}}
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
- id: cg-cross
builds:
- darwin-amd64
- linux-armhf
name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}"
format: zip
wrap_in_directory: true

changelog:
sort: asc
Expand Down
46 changes: 45 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,48 @@ gif: build

@PHONY: sqlc
sqlc:
sqlc generate -f internal/adapter/db/sqlc.yaml
sqlc generate -f internal/adapter/db/sqlc.yaml


GOLANG_CROSS_VERSION ?= v1.22.2
PACKAGE_NAME ?= certguard

SYSROOT_DIR ?= sysroots
SYSROOT_ARCHIVE ?= sysroots.tar.bz2

.PHONY: sysroot-pack
sysroot-pack:
@tar cf - $(SYSROOT_DIR) -P | pv -s $[$(du -sk $(SYSROOT_DIR) | awk '{print $1}') * 1024] | pbzip2 > $(SYSROOT_ARCHIVE)

.PHONY: sysroot-unpack
sysroot-unpack:
@pv $(SYSROOT_ARCHIVE) | pbzip2 -cd | tar -xf -

.PHONY: release-dry-run
release-dry-run:
@docker run \
--rm \
-e CGO_ENABLED=1 \
-v /var/run/docker.sock:/var/run/docker.sock \
-v `pwd`:/go/src/$(PACKAGE_NAME) \
-v `pwd`/sysroot:/sysroot \
-w /go/src/$(PACKAGE_NAME) \
ghcr.io/goreleaser/goreleaser-cross:${GOLANG_CROSS_VERSION} \
--clean --skip=validate --skip=publish

.PHONY: release
release:
@if [ ! -f ".release-env" ]; then \
echo "\033[91m.release-env is required for release\033[0m";\
exit 1;\
fi
docker run \
--rm \
-e CGO_ENABLED=1 \
--env-file .release-env \
-v /var/run/docker.sock:/var/run/docker.sock \
-v `pwd`:/go/src/$(PACKAGE_NAME) \
-v `pwd`/sysroot:/sysroot \
-w /go/src/$(PACKAGE_NAME) \
ghcr.io/goreleaser/goreleaser-cross:${GOLANG_CROSS_VERSION} \
release --clean

0 comments on commit e0c24e0

Please sign in to comment.