From e0c24e0f1453c9fb417cfac561eace8a45eb132b Mon Sep 17 00:00:00 2001 From: Pim Gaemers Date: Mon, 22 Jul 2024 20:33:16 +0200 Subject: [PATCH] use goreleaser docker image for cross compile in release step (#68) --- .github/workflows/release.yml | 43 +++++--------- .goreleaser.yml | 108 +++++++++++----------------------- Makefile | 46 ++++++++++++++- 3 files changed, 94 insertions(+), 103 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 640cbad..75a492c 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -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 }} \ No newline at end of file + 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 \ No newline at end of file diff --git a/.goreleaser.yml b/.goreleaser.yml index 0d30a3a..3da9956 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -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 diff --git a/Makefile b/Makefile index 12c9d90..569a202 100644 --- a/Makefile +++ b/Makefile @@ -20,4 +20,48 @@ gif: build @PHONY: sqlc sqlc: - sqlc generate -f internal/adapter/db/sqlc.yaml \ No newline at end of file + 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 \ No newline at end of file