From 643168b2bd19a6a62191f318e48bf028caae5070 Mon Sep 17 00:00:00 2001 From: iyear Date: Fri, 8 Nov 2024 14:14:06 +0800 Subject: [PATCH] feat(prj): support docker image --- .github/workflows/docker.yml | 67 ++++++++++++++++++++++++++++++++++++ Dockerfile | 24 +++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 .github/workflows/docker.yml create mode 100644 Dockerfile diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml new file mode 100644 index 0000000000..a4d7bd93de --- /dev/null +++ b/.github/workflows/docker.yml @@ -0,0 +1,67 @@ +name: docker + +on: + workflow_dispatch: + inputs: + ref: + description: 'Ref to checkout' + required: true + default: 'master' + push: + tags: + - 'v*' + +jobs: + docker: + runs-on: ubuntu-22.04 + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: 0 + ref: ${{ github.event_name == 'workflow_dispatch' && inputs.ref || github.ref }} + - name: Docker meta + id: meta + uses: docker/metadata-action@v5 + with: + context: git + images: | + ${{ github.repository }} + tags: | + type=match,pattern=\d+.\d+.\d+ + type=ref,event=branch + type=ref,event=pr + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + - name: Login to DockerHub + uses: docker/login-action@v3 + with: + username: ${{ github.repository_owner }} + password: ${{ secrets.DOCKERHUB_TOKEN }} +# - name: Login to GitHub Container Registry +# uses: docker/login-action@v3 +# with: +# registry: ghcr.io +# username: ${{ github.repository_owner }} +# password: ${{ secrets.RELEASE_TOKEN }} + - name: Use latest Dockerfile if workflow_dispatch + if: ${{ github.event_name == 'workflow_dispatch' }} + run: | + curl -s https://raw.githubusercontent.com/iyear/tdl/master/Dockerfile > Dockerfile + - name: Build and push + uses: docker/build-push-action@v6 + with: + context: . + cache-from: type=gha + cache-to: type=gha,mode=max + build_args: | + VERSION=${{ github.ref_name }} + COMMIT=${{ github.sha }} + COMMIT_DATE=${{ github.event.head_commit.timestamp }} +# platforms: linux/amd64,linux/386,linux/arm64,linux/arm/v7,linux/arm/v6,linux/riscv64 + platforms: linux/amd64 + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000000..e5f93b7007 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,24 @@ +FROM golang:1.21-alpine AS builder + +ARG VERSION="dev" +ARG COMMIT="unknown" +ARG COMMIT_DATE="unknown" + +WORKDIR / + +COPY . . + +RUN go build -trimpath \ + -ldflags "-s -w \ + -X github.com/iyear/tdl/pkg/consts.Version=${VERSION} \ + -X github.com/iyear/tdl/pkg/consts.Commit=${COMMIT} \ + -X github.com/iyear/tdl/pkg/consts.CommitDate=${COMMIT_DATE}" \ + -o tdl + +FROM alpine:latest + +RUN apk add --no-cache ca-certificates + +COPY --from=builder /tdl /tdl + +ENTRYPOINT ["/tdl"]