From f4b65d9144ec3dd587b284075aeb9c3a4c3c0d96 Mon Sep 17 00:00:00 2001 From: tkizm1 <5886048+tkizm1@users.noreply.github.com> Date: Fri, 17 May 2024 09:35:35 +0800 Subject: [PATCH] add docker and actions and compose (#5) * Create docker-publish.yml * Create Dockerfile * Update Dockerfile * Create docker-compose.yml --- .github/workflows/docker-publish.yml | 67 ++++++++++++++++++++++++++++ Dockerfile | 30 +++++++++++++ docker-compose.yml | 9 ++++ 3 files changed, 106 insertions(+) create mode 100644 .github/workflows/docker-publish.yml create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml new file mode 100644 index 0000000..9891f1e --- /dev/null +++ b/.github/workflows/docker-publish.yml @@ -0,0 +1,67 @@ +name: Docker + +on: + push: + branches: [ "master" ] + tags: [ 'v*.*.*' ] + pull_request: + branches: [ "master" ] + +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + + +jobs: + build: + + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + id-token: write + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Install cosign + if: github.event_name != 'pull_request' + uses: sigstore/cosign-installer@6e04d228eb30da1757ee4e1dd75a0ec73a653e06 #v3.1.1 + with: + cosign-release: 'v2.1.1' + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@f95db51fddba0c2d1ec667646a06c2ce06100226 # v3.0.0 + + - name: Log into registry ${{ env.REGISTRY }} + if: github.event_name != 'pull_request' + uses: docker/login-action@343f7c4344506bcbf9b4de18042ae17996df046d # v3.0.0 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract Docker metadata + id: meta + uses: docker/metadata-action@96383f45573cb7f253c731d3b3ab81c87ef81934 # v5.0.0 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + tags: | + type=ref,event=branch + type=ref,event=tag + type=semver,pattern={{version}} + type=sha + type=raw,value=latest + + - name: Build and push Docker image + id: build-and-push + uses: docker/build-push-action@0565240e2d4ab88bba5387d719585280857ece09 # v5.0.0 + with: + context: . + push: ${{ github.event_name != 'pull_request' }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + platforms: linux/amd64,linux/arm64 + cache-from: type=gha + cache-to: type=gha,mode=max diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..3b23d24 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,30 @@ +# 使用 Go 1.22 官方镜像作为构建环境 +FROM golang:1.22 AS builder + +# 禁用 CGO +ENV CGO_ENABLED=0 + +# 设置工作目录 +WORKDIR /app + +# 复制 go.mod 和 go.sum 并下载依赖 +COPY go.mod go.sum ./ +RUN go mod download + +# 复制源代码并构建应用 +COPY . . +RUN go build -ldflags "-s -w" -o /app/override . + +# 使用 Alpine Linux 作为最终镜像 +FROM alpine:latest + +# 设置工作目录 +WORKDIR /app + +# 从构建阶段复制编译好的应用和资源 +COPY --from=builder /app/override /app/override + +# 暴露端口 +EXPOSE 8080 + +CMD ["/app/override"] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..2271d6e --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,9 @@ +version: '3.4' + +services: + groq2api: + image: ghcr.io/linux-do/override:latest + container_name: override + restart: always + ports: + - "8181:8181"