title: Gitea Actions/Github Actions 构建 Go 多平台 Docker 镜像
description: 借助 Gitea Actions/Github Actions 实现发布版本后,自动打包 Go 项目并构建成 Docker 镜像,推送到阿里云 Docker 仓库中
date: 2024-10-01 23:12:43
categories:
借助 Gitea Actions/Github Actions 实现发布版本后,自动打包 Go 项目并构建成 Docker 镜像,推送到阿里云 Docker 仓库中
自行准备例如 gin
项目,新建 git
仓库名为 go-demo
。
FROM golang:1.21-alpine AS go-builder
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories
WORKDIR /workspace
RUN mkdir /data
RUN apk --no-cache add tzdata ca-certificates
ENV GOPROXY=https://goproxy.cn,direct
COPY go.mod go.mod
COPY go.sum go.sum
RUN go mod download
COPY . .
RUN go build -o app
# runner
FROM scratch
WORKDIR /workspace
COPY --from=go-builder /data /data
COPY --from=go-builder /usr/share/zoneinfo /usr/share/zoneinfo
COPY --from=go-builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
ENV TZ=Asia/Shanghai
ENV SERVER_MODE=release \
SERVER_PORT=4000
COPY --from=go-builder /workspace/app /app
ENTRYPOINT ["/app"]
新建.gitea/workflows/build.yaml
文件
需要提前配置 Docker 仓库的密码到 Secrets 中,若需要企业微信通知,也需要配置企业微信机器人的 WebHook 到 Secrets 中。
# 打标签时触发构建,标签例如v1.0.0,同时需要配置 DOCKER_PASSWORD 的 secrets
# 构建后镜像为 ${docker_registry}/${docker_username}/${repo_name}:1.0.0
name: Build Image
on:
push:
tags:
- v*
env:
# 此处可修改为你任意 docker 镜像仓库地址和用户名
DOCKER_REGISTRY: registry.cn-hangzhou.aliyuncs.com
DOCKER_USERNAME: seepine
jobs:
build-image:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 1
- name: Docker build push
uses: seepine/action-docker-build-push@v1
with:
registry: ${{ env.DOCKER_REGISTRY }}
username: ${{ env.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
platforms: linux/amd64,linux/arm64
当我们发布一个 v1.0.0
版本时,将会看到 Actions 进行工作,最终执行完成后将会推送构建成功的消息到企业微信中,消息为
springboot-demo构建成功。
镜像: registry.cn-hangzhou.aliyuncs.com/seepine/go-demo:1.0.0
docker run --rm -p 8080:8080 registry.cn-hangzhou.aliyuncs.com/seepine/go-demo:1.0.0