From 543faed36e68c462031a623d312219ec0e466066 Mon Sep 17 00:00:00 2001 From: Jian Zeng Date: Sun, 7 Jan 2024 19:50:00 +0800 Subject: [PATCH] chore: update config files Signed-off-by: Jian Zeng --- .dockerignore | 2 ++ .github/workflows/release.yml | 28 +++++++++++++++++++++------- .goreleaser.yaml | 21 +++------------------ Dockerfile | 13 +++++++++++++ Makefile | 16 ++++++++++++++-- pkg/info/info.go | 3 +++ 6 files changed, 56 insertions(+), 27 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..e55047b --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +/yukid +/yukictl diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 16f45b2..c68860e 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -12,25 +12,39 @@ concurrency: permissions: contents: write - # packages: write - # issues: write + packages: write jobs: - goreleaser: + releaser: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - with: - fetch-depth: 0 - - run: git fetch --force --tags + - uses: actions/setup-go@v5 with: go-version: stable + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Log in to the Container registry + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Build and Push Image + uses: docker/build-push-action@v3 + with: + context: . + push: true + tags: ghcr.io/ustclug/yukid:${{ github.ref_name }} + # More assembly might be required: Docker logins, GPG, etc. # It all depends on your needs. - uses: goreleaser/goreleaser-action@v5 with: - distribution: goreleaser version: latest args: release --clean env: diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 33c21d2..140091b 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -1,9 +1,9 @@ before: hooks: - go mod tidy -# TODO: publish docker images -# https://goreleaser.com/customization/docker/#how-it-works builds: + # NOTE: Currently, we cannot distribute yukid as a binary, because it requires CGO. + # Instead, we distribute it as a Docker image. Please refer to the release workflow for more details. - id: yukictl binary: yukictl @@ -17,22 +17,7 @@ builds: flags: - -trimpath ldflags: - - -s -w -X github.com/ustclug/Yuki/pkg/info.Version={{.Version}} -X github.com/ustclug/Yuki/pkg/info.BuildDate={{.Date}} - - - id: yukid - binary: yukid - main: ./cmd/yukid - env: - # required by sqlite - - CGO_ENABLED=1 - goos: - - linux - goarch: - - amd64 - flags: - - -trimpath - ldflags: - - -s -w -X github.com/ustclug/Yuki/pkg/info.Version={{.Version}} -X github.com/ustclug/Yuki/pkg/info.BuildDate={{.Date}} + - -s -w -X github.com/ustclug/Yuki/pkg/info.Version={{.Version}} -X github.com/ustclug/Yuki/pkg/info.BuildDate={{.Date}} -X github.com/ustclug/Yuki/pkg/info.GitCommit={{.Commit}} archives: - format: binary checksum: diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a5b65f9 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +# syntax=docker/dockerfile:1 + +FROM golang:1.21-bookworm AS build +WORKDIR /app +RUN --mount=type=cache,target=/root/.cache/go-build \ + --mount=type=cache,target=/go/pkg/mod \ + --mount=type=bind,target=/app \ + OUT_DIR=/tmp make yukid + +FROM debian:bookworm-slim +RUN apt update && apt install -y sqlite3 && rm -rf /var/lib/apt/lists/* +COPY --link --from=build /tmp/yukid /yukid +CMD ["/yukid"] diff --git a/Makefile b/Makefile index e5672bb..4d67226 100644 --- a/Makefile +++ b/Makefile @@ -10,13 +10,25 @@ unit-test: integration-test: go test -v ./test/integration/... +git_commit := $(shell git rev-parse HEAD) +build_date := $(shell date -u +'%Y-%m-%dT%H:%M:%SZ') + +VERSION ?= $(shell git describe --tags) +OUT_DIR ?= $(PWD) + .PHONY: yukid yukid: - go build -trimpath ./cmd/yukid + go build -ldflags "-X github.com/ustclug/Yuki/pkg/info.BuildDate=$(build_date) \ + -X github.com/ustclug/Yuki/pkg/info.GitCommit=$(git_commit) \ + -X github.com/ustclug/Yuki/pkg/info.Version=$(VERSION)" \ + -trimpath -o $(OUT_DIR)/yukid ./cmd/yukid .PHONY: yukictl yukictl: - go build -trimpath ./cmd/yukictl + go build -ldflags "-X github.com/ustclug/Yuki/pkg/info.BuildDate=$(build_date) \ + -X github.com/ustclug/Yuki/pkg/info.GitCommit=$(git_commit) \ + -X github.com/ustclug/Yuki/pkg/info.Version=$(version)" \ + -trimpath ./cmd/yukictl BUILD_IMAGE ?= golang:1.21-bookworm diff --git a/pkg/info/info.go b/pkg/info/info.go index 7894ead..abb2044 100644 --- a/pkg/info/info.go +++ b/pkg/info/info.go @@ -7,14 +7,17 @@ import ( var ( Version string BuildDate string + GitCommit string ) var VersionInfo = struct { Version string GoVersion string BuildDate string + GitCommit string }{ Version: Version, BuildDate: BuildDate, + GitCommit: GitCommit, GoVersion: runtime.Version(), }