From ec4fdb50edfba81cc73a14ccaff5a4e061f3792f Mon Sep 17 00:00:00 2001 From: Matheus Macabu Date: Sun, 18 Aug 2024 16:37:07 +0200 Subject: [PATCH] build: use goreleaser to cross-compile binaries for all platforms and archs The main motivation behind it is because "gox" is unmaintained and archived and it does not support "linux/riscv64" as a target. Right now, goreleaser is only building the binaries, and I've tried to replicate the exact same way the old script does. In the future, if so desired, goreleaser could be used to further automate the build and release pipeline by creating the release in GitHub. --- .github/workflows/release.yml | 2 +- .goreleaser.yaml | 45 +++++++++++++++++++++++++++++++++++ scripts/xcompile.sh | 17 +++++++++---- 3 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 .goreleaser.yaml diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index c671258c..e9d76501 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -40,7 +40,7 @@ jobs: - name: Cross compile run: | sudo apt-get install rhash -y - go install github.com/mitchellh/gox@v1.0.1 + go install github.com/goreleaser/goreleaser/v2@latest mkdir -p build cp yq.1 build/yq.1 ./scripts/xcompile.sh diff --git a/.goreleaser.yaml b/.goreleaser.yaml new file mode 100644 index 00000000..757f694f --- /dev/null +++ b/.goreleaser.yaml @@ -0,0 +1,45 @@ +version: 2 + +dist: build + +builds: + - id: yq + + binary: yq_{{ .Os }}_{{ .Arch }} + + ldflags: + - -s -w + + env: + - CGO_ENABLED=0 + + targets: + - darwin_amd64 + - darwin_arm64 + - freebsd_386 + - freebsd_amd64 + - freebsd_arm + - linux_386 + - linux_amd64 + - linux_arm + - linux_arm64 + - linux_mips + - linux_mips64 + - linux_mips64le + - linux_mipsle + - linux_ppc64 + - linux_ppc64le + - linux_s390x + - netbsd_386 + - netbsd_amd64 + - netbsd_arm + - openbsd_386 + - openbsd_amd64 + - windows_386 + - windows_amd64 + + no_unique_dist_dir: true + +release: + disable: true + skip_upload: true diff --git a/scripts/xcompile.sh b/scripts/xcompile.sh index 08e8d662..41847eaf 100755 --- a/scripts/xcompile.sh +++ b/scripts/xcompile.sh @@ -1,11 +1,20 @@ #!/bin/bash -set -e -# you may need to go install github.com/mitchellh/gox@v1.0.1 first -echo $VERSION -CGO_ENABLED=0 gox -ldflags "-s -w ${LDFLAGS}" -output="build/yq_{{.OS}}_{{.Arch}}" --osarch="darwin/amd64 darwin/arm64 freebsd/386 freebsd/amd64 freebsd/arm linux/386 linux/amd64 linux/arm linux/arm64 linux/mips linux/mips64 linux/mips64le linux/mipsle linux/ppc64 linux/ppc64le linux/s390x netbsd/386 netbsd/amd64 netbsd/arm openbsd/386 openbsd/amd64 windows/386 windows/amd64" + +set -eo pipefail + +# You may need to go install github.com/goreleaser/goreleaser/v2@latest first +GORELEASER="goreleaser build --clean" +if [ -z "$CI" ]; then + GORELEASER+=" --snapshot" +fi + +$GORELEASER cd build +# Remove artifacts from goreleaser +rm artifacts.json config.yaml metadata.json + find . -executable -type f | xargs -I {} tar czvf {}.tar.gz {} yq.1 -C ../scripts install-man-page.sh tar czvf yq_man_page_only.tar.gz yq.1 -C ../scripts install-man-page.sh