diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..45977f1 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,60 @@ +name: Release + +on: push + +jobs: + binary: + if: startsWith(github.ref, 'refs/tags/v') + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-go@v5 + with: + go-version: 1.24.x + stable: true + + - run: go get -v -t -d ./... + - run: make VERSION=${GITHUB_REF/refs\/tags\/v/} clean build + + - uses: svenstaro/upload-release-action@v2 + with: + repo_token: ${{ secrets.GITHUB_TOKEN }} + file: build/* + tag: ${{ github.ref }} + overwrite: true + file_glob: true + + image: + if: ${{ startsWith(github.ref, 'refs/heads/master') || startsWith(github.ref, 'refs/tags/v') }} + runs-on: ubuntu-latest + + permissions: + contents: read + packages: write + attestations: write + id-token: write + + steps: + - uses: actions/checkout@v4 + + - if: startsWith(github.ref, 'refs/tags/v') + run: echo "VERSION=${GITHUB_REF/refs\/tags\/v/}" >> $GITHUB_ENV + - run: echo "DOCKER_BUILD_PUSH=true" >> $GITHUB_ENV + + - uses: docker/setup-buildx-action@v3 + - uses: docker/setup-qemu-action@v3 + - uses: docker/login-action@v3 + with: + registry: docker.io + username: ${{ secrets.DOCKER_USER }} + password: ${{ secrets.DOCKER_PASS }} + - uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ github.token }} + + - run: | + make REGISTRY=docker.io build-docker-multiarch + make REGISTRY=ghcr.io build-docker-multiarch diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index b8f6932..2e9327f 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -3,14 +3,11 @@ on: [push, pull_request] jobs: check: runs-on: ubuntu-latest - strategy: - matrix: - go: [1.20.x, 1.21.x, 1.22.x] steps: - name: Setup Go environment uses: actions/setup-go@v5 with: - go-version: ${{ matrix.go }} + go-version: 1.24.x stable: true - name: Check out code into the Go module directory @@ -21,3 +18,10 @@ jobs: - name: Test run: go test ./... + image: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: docker/setup-buildx-action@v3 + - run: | + make PLATFORM=linux/amd64 build-docker-multiarch diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml deleted file mode 100644 index 3970b83..0000000 --- a/.github/workflows/release.yml +++ /dev/null @@ -1,37 +0,0 @@ -name: Release - -on: - push: - tags: - - v* - -jobs: - release: - runs-on: ubuntu-latest - - steps: - - name: Checkout code - uses: actions/checkout@v4 - - - name: Setup Go environment - uses: actions/setup-go@v5 - with: - go-version: 1.22.x - stable: true - - - name: Get dependencies - run: go get -v -t -d ./... - - - name: Build - run: | - TAG=$(git describe --tags --abbrev=0) - GIT_TAG=$TAG make clean build - - - name: Create release - uses: svenstaro/upload-release-action@v2 - with: - repo_token: ${{ secrets.GITHUB_TOKEN }} - file: build/* - tag: ${{ github.ref }} - overwrite: true - file_glob: true diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 99c4b4b..0000000 --- a/Dockerfile +++ /dev/null @@ -1,28 +0,0 @@ -# build -FROM golang:1.18-stretch AS build-env - -WORKDIR /src - -ADD . . - -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o app . - -# run -FROM alpine:3.16 - -RUN apk add --no-cache \ - ca-certificates - -RUN addgroup -S app \ - && adduser -S -g app app - -WORKDIR /home/app - -COPY --from=build-env /src/app . - -RUN chown -R app:app ./ - -USER app -ENV USER=app - -ENTRYPOINT ["./app"] diff --git a/Makefile b/Makefile index a6e7bc9..fc75d05 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,46 @@ -BUILD_DIR=./build +SHELL := /bin/bash + +# Env var inputs for container image builds +# REGISTRY: The registry to which the build image should be pushed to. +# IMAGE: The name of the image to build and publish in the afore mentioned registry. +# PLATFORM: The platform for which the image should be built +REGISTRY ?= docker.io +IMAGE ?= gotify/cli +PLATFORM ?= linux/amd64,linux/arm64,linux/386,linux/arm/v7,linux/riscv64 + +# Env var inputs for all builds +# VERSION: The version for which the container image or the binary is being built. +# When it is not provided, no version will be specified in the built package. +# COMMIT: The commit of this project for which the cli is being built, for reference in the tool's "version" command. +# LD_FLAGS: Build flags, for the tool's "version" command. +COMMIT ?= $(shell git rev-parse --verify HEAD) +LD_FLAGS ?= $(if $(VERSION),-X main.Version=${VERSION}) \ + -X main.BuildDate=$(shell date "+%F-%T") \ + -X main.Commit=${COMMIT} + +ifdef GOTOOLCHAIN + GO_VERSION=$(GOTOOLCHAIN) +else + GO_VERSION=$(shell go mod edit -json | jq -r .Toolchain | sed -e 's/go//') +endif + +build-docker-multiarch: + docker buildx build \ + $(if $(DOCKER_BUILD_PUSH),--push) \ + -t ${REGISTRY}/${IMAGE}:master \ + $(if $(VERSION),-t ${REGISTRY}/${IMAGE}:latest) \ + $(if $(VERSION),-t ${REGISTRY}/${IMAGE}:${VERSION}) \ + $(if $(VERSION),-t ${REGISTRY}/${IMAGE}:$(shell echo $(VERSION) | cut -d '.' -f -2)) \ + $(if $(VERSION),-t ${REGISTRY}/${IMAGE}:$(shell echo $(VERSION) | cut -d '.' -f -1)) \ + --build-arg GO_VERSION=$(GO_VERSION) \ + --build-arg LD_FLAGS="$(LD_FLAGS)" \ + --platform $(PLATFORM) \ + -f docker/Dockerfile . clean: - rm -rf ${BUILD_DIR} + rm -rf build build: - if [ '$(shell echo "${GIT_TAG}" | cut -c 1 )' != 'v' ]; then exit 1; fi; - $(eval LD_FLAGS := -X main.Version=$(shell echo ${GIT_TAG} | cut -c 2-) -X main.BuildDate=$(shell date "+%F-%T") -X main.Commit=$(shell git rev-parse --verify HEAD)) CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags="${LD_FLAGS}" -o build/gotify-cli-windows-amd64.exe cli.go CGO_ENABLED=0 GOOS=windows GOARCH=386 go build -ldflags="${LD_FLAGS}" -o build/gotify-cli-windows-386.exe cli.go CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="${LD_FLAGS}" -o build/gotify-cli-linux-amd64 cli.go diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 0000000..ddd303b --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,26 @@ +ARG BUILDKIT_SBOM_SCAN_CONTEXT=true +ARG GO_VERSION=PLEASE_PROVIDE_GO_VERSION +ARG BUILDPLATFORM + +FROM --platform=${BUILDPLATFORM} docker.io/golang:${GO_VERSION}-alpine AS builder + +ARG TARGETPLATFORM +ARG TARGETOS +ARG TARGETARCH +ARG LD_FLAGS + +WORKDIR /src +COPY . /src + +RUN GOOS=${TARGETOS} \ + GOARCH=${TARGETARCH} \ + GOARM=$(echo $TARGETPLATFORM | sed -En 's|^linux/arm/v(.*)|\1|p') \ + go build -mod=readonly -a -installsuffix cgo -ldflags "${LD_FLAGS}" -o /target/app/gotify-cli + +FROM docker.io/library/alpine:latest + +RUN apk add --no-cache ca-certificates + +COPY --from=builder /target/app/gotify-cli /gotify-cli + +ENTRYPOINT ["/gotify-cli"] diff --git a/go.mod b/go.mod index ea7acc4..0ce0a12 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,8 @@ module github.com/gotify/cli/v2 -go 1.18 +go 1.23 + +toolchain go1.24.1 require ( github.com/adrg/xdg v0.4.0