From 1e5c97bd7f9c7032149c42852bb8c1b3a6798575 Mon Sep 17 00:00:00 2001 From: Aidan Delaney Date: Sat, 26 Oct 2024 18:06:09 +0100 Subject: [PATCH 1/2] Use goreleaser to build binaries/images Use goreleaser and common workflows to 1. bump tag on merges to main 2. build binaries 3. build image 4. release binaries on GH --- .github/workflows/go.yml | 23 ---------------- .github/workflows/goreleaser.yaml | 34 +++++++++++++++++++++++ .github/workflows/tag-on-main.yaml | 29 ++++++++++++++++++++ .gitignore | 3 ++- .goreleaser.yaml | 43 ++++++++++++++++++++++++++++++ Dockerfile | 8 +----- build.sh | 19 ------------- go.mod | 2 +- 8 files changed, 110 insertions(+), 51 deletions(-) delete mode 100644 .github/workflows/go.yml create mode 100644 .github/workflows/goreleaser.yaml create mode 100644 .github/workflows/tag-on-main.yaml create mode 100644 .goreleaser.yaml delete mode 100755 build.sh diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml deleted file mode 100644 index d17cbcf..0000000 --- a/.github/workflows/go.yml +++ /dev/null @@ -1,23 +0,0 @@ -name: Go -on: - push: - branches: [ main ] - pull_request: - branches: [ main ] -jobs: - build: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - name: Set up Go - uses: actions/setup-go@v2 - with: - go-version: 1.18 - - name: Fmt - run: test -z $(go fmt ./...) - - name: Vet - run: go vet ./... - - name: Build - run: go build -v ./... - - name: Test - run: go test -v ./... diff --git a/.github/workflows/goreleaser.yaml b/.github/workflows/goreleaser.yaml new file mode 100644 index 0000000..39f04eb --- /dev/null +++ b/.github/workflows/goreleaser.yaml @@ -0,0 +1,34 @@ +name: goreleaser + +on: + push: + tags: + - '*' + +permissions: + contents: write + +jobs: + goreleaser: + runs-on: ubuntu-latest + steps: + - + name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + - run: git fetch --force --tags + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - + name: Set up Go + uses: actions/setup-go@v3 + - + name: Run GoReleaser + uses: goreleaser/goreleaser-action@v4 + with: + distribution: goreleaser + version: latest + args: release --rm-dist + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/.github/workflows/tag-on-main.yaml b/.github/workflows/tag-on-main.yaml new file mode 100644 index 0000000..81ad641 --- /dev/null +++ b/.github/workflows/tag-on-main.yaml @@ -0,0 +1,29 @@ +name: Bump version +on: + push: + branches: + - main +jobs: + build: + runs-on: ubuntu-latest + steps: + - + name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + token: ${{ secrets.GITHUB_TOKEN }} + - + name: Fetch all tags + run: git fetch --force --tags + - + name: Set up Go + uses: actions/setup-go@v3 + - + name: Install svu + run: GOBIN=$(realpath tools) go install github.com/caarlos0/svu@latest + - + name: Bump version and push tag + run: | + git tag $(./tools/svu patch) + git push origin $(./tools/svu c) \ No newline at end of file diff --git a/.gitignore b/.gitignore index b238bdd..d9d7ddb 100644 --- a/.gitignore +++ b/.gitignore @@ -20,4 +20,5 @@ *.out # Dependency directories (remove the comment below to include it) -# vendor/ \ No newline at end of file +# vendor/ +dist/ diff --git a/.goreleaser.yaml b/.goreleaser.yaml new file mode 100644 index 0000000..3d27ed4 --- /dev/null +++ b/.goreleaser.yaml @@ -0,0 +1,43 @@ +# This is an example .goreleaser.yml file with some sensible defaults. +# Make sure to check the documentation at https://goreleaser.com +before: + hooks: + # You may remove this if you don't use go modules. + - go mod tidy + # you may remove this if you don't need go generate + - go generate ./... +builds: + - id: yj + env: + - CGO_ENABLED=0 + goos: + - linux + - windows + - darwin + goarch: + - amd64 + - arm64 + ldflags: "-X main.Version={{ .Version }}" +dockers: + - id: yj + goos: linux + goarch: amd64 + image_templates: + - "sclevine/yj:latest" + - "sclevine/yj:{{ .Version }}" +archives: + - id: yj +checksum: + name_template: 'checksums.txt' +snapshot: + name_template: "{{ incpatch .Version }}-next" +changelog: + sort: asc + filters: + exclude: + - '^docs:' + - '^test:' + +# modelines, feel free to remove those if you don't want/use them: +# yaml-language-server: $schema=https://goreleaser.com/static/schema.json +# vim: set ts=2 sw=2 tw=0 fo=cnqoj diff --git a/Dockerfile b/Dockerfile index dc45324..ab9c51c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,3 @@ -FROM golang:alpine AS builder -RUN apk update && apk add --no-cache git -WORKDIR /workspace -COPY . . -ARG version="0.0.0" -RUN go build -ldflags "-X main.Version=$version" . FROM scratch -COPY --from=builder /workspace/yj /bin/yj +COPY yj /bin/yj ENTRYPOINT ["/bin/yj"] diff --git a/build.sh b/build.sh deleted file mode 100755 index 604a057..0000000 --- a/build.sh +++ /dev/null @@ -1,19 +0,0 @@ -#!/bin/bash - -set -e - -version=${1:-0.0.0} -out=build-v${version} -cd "$(dirname "${BASH_SOURCE[0]}")" -mkdir -p "$out" - -GOOS=darwin GOARCH=amd64 go build -ldflags "-X main.Version=$version" -o "$out/yj-macos-amd64" . -GOOS=darwin GOARCH=arm64 go build -ldflags "-X main.Version=$version" -o "$out/yj-macos-arm64" . -GOOS=linux GOARCH=amd64 go build -ldflags "-X main.Version=$version" -o "$out/yj-linux-amd64" . -GOOS=linux GOARCH=arm64 go build -ldflags "-X main.Version=$version" -o "$out/yj-linux-arm64" . -GOOS=linux GOARCH=arm GOARM=5 go build -ldflags "-X main.Version=$version" -o "$out/yj-linux-arm-v5" . -GOOS=linux GOARCH=arm GOARM=7 go build -ldflags "-X main.Version=$version" -o "$out/yj-linux-arm-v7" . -GOOS=windows GOARCH=amd64 go build -ldflags "-X main.Version=$version" -o "$out/yj.exe" . - -docker build . --build-arg "version=$version" -t "sclevine/yj:$version" -docker tag "sclevine/yj:$version" "sclevine/yj:latest" \ No newline at end of file diff --git a/go.mod b/go.mod index c37f5e8..1b2a4d7 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/sclevine/yj/v5 -go 1.21 +go 1.22 require ( github.com/BurntSushi/toml v1.3.2 From d5f587a8641dc9e19aecd312f4687a54be330af6 Mon Sep 17 00:00:00 2001 From: Aidan Delaney Date: Mon, 28 Oct 2024 09:46:40 +0000 Subject: [PATCH 2/2] Reinstate local build.sh file Build locally using build.sh as a wrapper around goreleaser, and remove auto-tag on merge to main. --- .github/workflows/tag-on-main.yaml | 29 ----------------------------- build.sh | 2 ++ 2 files changed, 2 insertions(+), 29 deletions(-) delete mode 100644 .github/workflows/tag-on-main.yaml create mode 100755 build.sh diff --git a/.github/workflows/tag-on-main.yaml b/.github/workflows/tag-on-main.yaml deleted file mode 100644 index 81ad641..0000000 --- a/.github/workflows/tag-on-main.yaml +++ /dev/null @@ -1,29 +0,0 @@ -name: Bump version -on: - push: - branches: - - main -jobs: - build: - runs-on: ubuntu-latest - steps: - - - name: Checkout - uses: actions/checkout@v4 - with: - fetch-depth: 0 - token: ${{ secrets.GITHUB_TOKEN }} - - - name: Fetch all tags - run: git fetch --force --tags - - - name: Set up Go - uses: actions/setup-go@v3 - - - name: Install svu - run: GOBIN=$(realpath tools) go install github.com/caarlos0/svu@latest - - - name: Bump version and push tag - run: | - git tag $(./tools/svu patch) - git push origin $(./tools/svu c) \ No newline at end of file diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..162f730 --- /dev/null +++ b/build.sh @@ -0,0 +1,2 @@ +go install github.com/goreleaser/goreleaser/v2@latest +goreleaser release --auto-snapshot --rm-dist \ No newline at end of file