From 77f30efde86431b1dc5df97e0969e717b449b102 Mon Sep 17 00:00:00 2001 From: wood chen Date: Wed, 9 Oct 2024 13:06:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20Docker=20=E5=B7=A5?= =?UTF-8?q?=E4=BD=9C=E6=B5=81=E4=BB=A5=E6=94=AF=E6=8C=81=E5=A4=9A=E5=B9=B3?= =?UTF-8?q?=E5=8F=B0=E5=92=8C=20Go=20=E7=89=88=E6=9C=AC=E6=9E=84=E5=BB=BA?= =?UTF-8?q?=E4=B8=8E=E6=8E=A8=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/docker.yml | 61 +++++++++++++++++++++++++----------- Dockerfile | 14 ++------- 2 files changed, 44 insertions(+), 31 deletions(-) diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index df8721e..bb1dacb 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -1,4 +1,4 @@ -name: Docker +name: Build and Push Docker Image on: push: @@ -13,30 +13,53 @@ env: jobs: build: runs-on: ubuntu-latest + strategy: + matrix: + go-version: [1.23.x] + platform: [linux/amd64, linux/arm64] steps: - - name: 检出代码库 - uses: actions/checkout@v3 + - name: Checkout code + uses: actions/checkout@v3 - - name: 构建镜像 - run: docker build . --file Dockerfile --tag $IMAGE_NAME + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: ${{ matrix.go-version }} - - name: 登录到镜像仓库 - run: echo "${{ secrets.ACCESS_TOKEN }}" | docker login -u woodchen --password-stdin + - name: Set up QEMU + uses: docker/setup-qemu-action@v2 - - name: 推送镜像 - run: | - IMAGE_ID=woodchen/$IMAGE_NAME - IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]') + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 - # 从 GitHub 事件负载中获取分支名 - BRANCH_NAME=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') + - name: Login to DockerHub + uses: docker/login-action@v2 + with: + username: woodchen + password: ${{ secrets.ACCESS_TOKEN }} - # 对于除了 "main" 分支和标签以外的分支,使用 "latest" 版本号 - VERSION=$(if [ "$BRANCH_NAME" == "main" ]; then echo "latest"; else echo $BRANCH_NAME; fi) + - name: Build Go binary + run: | + CGO_ENABLED=0 GOOS=linux GOARCH=${{ matrix.platform == 'linux/amd64' && 'amd64' || 'arm64' }} go build -ldflags '-w -s' -o feishu_chatgpt - echo IMAGE_ID=$IMAGE_ID - echo VERSION=$VERSION + - name: Prepare Docker metadata + id: meta + uses: docker/metadata-action@v4 + with: + images: woodchen/${{ env.IMAGE_NAME }} + tags: | + type=ref,event=branch + type=ref,event=pr + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + + - name: Build and push Docker image + uses: docker/build-push-action@v4 + with: + context: . + platforms: ${{ matrix.platform }} + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} - docker tag $IMAGE_NAME $IMAGE_ID:$VERSION - docker push $IMAGE_ID:$VERSION \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index f09eac2..944912d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,19 +1,9 @@ -FROM golang:1.23 as golang - -ENV GO111MODULE=on \ - CGO_ENABLED=1 - -WORKDIR /build -ADD /code /build - -RUN CGO_ENABLED=0 GOOS=linux go build -ldflags '-w -s' -o feishu_chatgpt - FROM alpine:latest WORKDIR /app RUN apk add --no-cache bash -COPY --from=golang /build/feishu_chatgpt /app -COPY --from=golang /build/role_list.yaml /app +COPY feishu_chatgpt /app +COPY role_list.yaml /app EXPOSE 9000 ENTRYPOINT ["/app/feishu_chatgpt"]