From 073c174126dba6c519d08adcb54437376c9a33b5 Mon Sep 17 00:00:00 2001 From: GCSLaoLi Date: Tue, 9 Apr 2024 08:55:09 -0400 Subject: [PATCH] =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/build.yml | 143 ++++++++---------------------------- Dockerfile | 38 ++++++++++ 2 files changed, 70 insertions(+), 111 deletions(-) create mode 100644 Dockerfile diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 95998eb..07702cc 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,127 +1,48 @@ -name: Build +# +name: Create and publish a Docker image + +# Configures this workflow to run every time a change is pushed to the branch called `release`. on: push: - branches: - - 'dev' - tags: - - '*' - pull_request: + branches: ['release'] +# Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds. env: - APP_NAME: cockroachai-v2 - DOCKERHUB_REPO: xyhepler/cockroachai-v2 - GHCR_REPO: ghcr.io/cockroachai/cockroachai-v2 - PLATFORMS: linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64,linux/riscv64 + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} +# There is a single job in this workflow. It's configured to run on the latest available version of Ubuntu. jobs: - build: - name: Build + build-and-push-image: runs-on: ubuntu-latest + # Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job. + permissions: + contents: read + packages: write + # steps: - - name: Checkout + - name: Checkout repository uses: actions/checkout@v4 + # Uses the `docker/login-action` action to log in to the Container registry registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here. + - name: Log in to the Container registry + uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 with: - fetch-depth: 0 - - - name: Set Vars - run: | - echo "SHA_SHORT=$(git rev-parse --short HEAD)" >> $GITHUB_ENV - - - name: Set up Go - uses: actions/setup-go@v5 - with: - check-latest: true - go-version-file: 'go.mod' - cache: true - - - name: Test - run: go test -v . - - - name: Build - uses: goreleaser/goreleaser-action@v5 - if: "!startsWith(github.ref, 'refs/tags/')" - with: - version: latest - args: build --snapshot --clean - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - - name: Upload Artifact - Linux amd64 - uses: actions/upload-artifact@v4 - if: "!startsWith(github.ref, 'refs/tags/')" - with: - name: ${{ env.APP_NAME }}-dev-${{ env.SHA_SHORT }}-linux-amd64 - path: | - ./dist/default_linux_amd64_v1/${{ env.APP_NAME }} - - - name: Upload Artifact - Linux arm64 - uses: actions/upload-artifact@v4 - if: "!startsWith(github.ref, 'refs/tags/')" - with: - name: ${{ env.APP_NAME }}-dev-${{ env.SHA_SHORT }}-linux-arm64 - path: | - ./dist/default_linux_arm64/${{ env.APP_NAME }} - - - name: Upload Artifact - Darwin arm64 - uses: actions/upload-artifact@v4 - if: "!startsWith(github.ref, 'refs/tags/')" - with: - name: ${{ env.APP_NAME }}-dev-${{ env.SHA_SHORT }}-darwin-arm64 - path: | - ./dist/default_darwin_arm64/${{ env.APP_NAME }} - - - name: Upload Artifact - Windows amd64 - uses: actions/upload-artifact@v4 - if: "!startsWith(github.ref, 'refs/tags/')" - with: - name: ${{ env.APP_NAME }}-dev-${{ env.SHA_SHORT }}-windows-amd64 - path: | - ./dist/default_windows_amd64_v1/${{ env.APP_NAME }}.exe - - - name: Release - uses: goreleaser/goreleaser-action@v5 - if: startsWith(github.ref, 'refs/tags/') - with: - version: latest - args: release --clean - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - - name: Docker - Set up Buildx - id: buildx - uses: docker/setup-buildx-action@v3 - - - name: Docker - Login to DockerHub - uses: docker/login-action@v3 - with: - username: ${{ secrets.DOCKERHUB_USERNAME }} - password: ${{ secrets.DOCKERHUB_TOKEN }} - - - name: Docker - Login to GHCR - uses: docker/login-action@v3 - with: - registry: ghcr.io - username: ${{ github.repository_owner }} + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - - - name: Docker - Docker meta + # This step uses [docker/metadata-action](https://github.com/docker/metadata-action#about) to extract tags and labels that will be applied to the specified image. The `id` "meta" allows the output of this step to be referenced in a subsequent step. The `images` value provides the base name for the tags and labels. + - name: Extract metadata (tags, labels) for Docker id: meta - uses: docker/metadata-action@v5 + uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 with: - images: | - ${{ env.DOCKERHUB_REPO }} - ${{ env.GHCR_REPO }} - tags: | - type=ref,event=branch - type=semver,pattern={{version}} - type=semver,pattern={{major}}.{{minor}} - - - name: Docker - Build and push - uses: docker/build-push-action@v5 + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + # This step uses the `docker/build-push-action` action to build the image, based on your repository's `Dockerfile`. If the build succeeds, it pushes the image to GitHub Packages. + # It uses the `context` parameter to define the build's context as the set of files located in the specified path. For more information, see "[Usage](https://github.com/docker/build-push-action#usage)" in the README of the `docker/build-push-action` repository. + # It uses the `tags` and `labels` parameters to tag and label the image with the output from the "meta" step. + - name: Build and push Docker image + uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4 with: context: . - file: .Dockerfile - platforms: ${{ env.PLATFORMS }} - push: ${{ github.event_name != 'pull_request' }} + push: true tags: ${{ steps.meta.outputs.tags }} - \ No newline at end of file + labels: ${{ steps.meta.outputs.labels }} diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a002dad --- /dev/null +++ b/Dockerfile @@ -0,0 +1,38 @@ +# 使用官方 Golang 镜像作为基础镜像 +FROM golang as builder + +# 安装 ca-certificates,这样你的应用就可以访问带有 SSL 证书的站点了 +RUN apk update && apk add --no-cache ca-certificates tzdata + +# 设置工作目录 +WORKDIR /app + +# 将 go.mod 和 go.sum 文件复制到工作目录 +COPY go.mod go.sum ./ + +# 下载所有依赖项 +RUN go mod download + +# 将源代码复制到工作目录 +COPY . . + +# 构建应用 +RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main . + +# 使用 scratch 作为基础镜像 +FROM scratch + +# 从 builder 镜像中复制 /etc/ssl/certs 到当前镜像中,这样你的应用就可以访问带有 SSL 证书的站点了 +COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ + +# 从 builder 镜像中复制 /usr/share/zoneinfo 到当前镜像中,这样你的应用就可以支持环境变量指定的时区了 +COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo + +# 从 builder 镜像中复制应用到当前镜像中 +COPY --from=builder /app/main /app/main + +# 指定环境变量 TZ,你可以在运行 Docker 容器时通过 -e 参数来覆盖这个值 +ENV TZ=Asia/Shanghai + +# 指定容器启动时运行的命令 +ENTRYPOINT ["/app/main"] \ No newline at end of file