前言 {#前言}
在本地推送 Docker 镜像至 Docker Hub 时,经常遇到超时问题。为了避免手动操作带来的不便,决定通过 GitHub Actions
实现自动化工作流,轻松将 Docker 镜像推送至 Docker Hub。接下来,我将详细介绍如何配置并使用 GitHub 工作流实现这一过程。
workflows 流程 {#workflows- 流程}
先来说说 Spring Boot项目打包成
Docker 镜像,并上传到仓库的流程
- 使用
Spring Boot
项目: 传送门 - 运行
maven
命令mvn -B clean package --file pom.xml
打包jar
文件 - 运行
docker build
构建docker
镜像 - 使用
docker push
推送docker
镜像
配置 Docker Hub 认证 {#配置 -Docker-Hub- 认证}
为了将镜像推送到 Docker Hub,我们需要进行 Docker Hub 的认证。以下是配置步骤:
- 登录 Docker Hub 获取用户名和密码。
- 将这些认证信息存储在 GitHub Secrets 中,确保安全性。
创建 GitHub Actions 工作流 {#创建 -GitHub-Actions- 工作流}
- 创建工作流目录
在项目根目录下创建 .github/workflows
文件夹。
- 配置工作流文件
docker-image.yml
创建一个名为 docker-image.yml
的 GitHub Actions 配置文件,内容如下:
name: docker-build-push
on:
push:
branches:
# 监听推送到 master 分支
- "master"
# 监听以 v 开头的标签
tags:
- 'v*'
pull_request:
# 监听针对 master 分支的拉取请求
branches:
- "master"
jobs:
build:
# 使用最新的 Ubuntu 环境
runs-on: ubuntu-latest
<span class="token key atrule">env</span><span class="token punctuation">:</span>
<span class="token comment"># 默认为 Docker Hub,如果使用其他 Docker 注册表,可以修改</span>
<span class="token key atrule">REGISTRY</span><span class="token punctuation">:</span> <span class="token string">'docker.io'</span>
<span class="token comment"># 使用 GitHub 仓库名作为镜像名称,格式为 < 账户>/< 仓库></span>
<span class="token comment"># IMAGE_NAME: ${{ github.generator }}</span>
<span class="token key atrule">IMAGE_NAME</span><span class="token punctuation">:</span> mobaijun/generator
<span class="token key atrule">steps</span><span class="token punctuation">:</span>
<span class="token comment"># 检出代码</span>
<span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> Checkout code
<span class="token comment"># 检出代码,默认会检出整个仓库</span>
<span class="token key atrule">uses</span><span class="token punctuation">:</span> actions/checkout@v3
<span class="token comment"># 设置 JDK 11</span>
<span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> Set up JDK 11
<span class="token comment"># 设置 Java 版本为 11</span>
<span class="token key atrule">uses</span><span class="token punctuation">:</span> actions/setup<span class="token punctuation">-</span>java@v3
<span class="token key atrule">with</span><span class="token punctuation">:</span>
<span class="token comment"># Java 版本设置为 11</span>
<span class="token key atrule">java-version</span><span class="token punctuation">:</span> <span class="token string">'11'</span>
<span class="token comment"># 使用 Temurin 的 JDK 发行版</span>
<span class="token key atrule">distribution</span><span class="token punctuation">:</span> <span class="token string">'temurin'</span>
<span class="token comment"># 启用 Maven 缓存,提升构建速度</span>
<span class="token key atrule">cache</span><span class="token punctuation">:</span> maven
<span class="token comment"># 使用 Maven 构建项目</span>
<span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> Build with Maven
<span class="token comment"># 使用 Maven 清理并构建项目</span>
<span class="token key atrule">run</span><span class="token punctuation">:</span> mvn <span class="token punctuation">-</span>B clean package <span class="token punctuation">-</span><span class="token punctuation">-</span>file pom.xml
<span class="token comment"># 构建 Docker 镜像</span>
<span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> Build Docker image
<span class="token comment"># 根据 Dockerfile 构建镜像,并标记为 latest</span>
<span class="token key atrule">run</span><span class="token punctuation">:</span> docker build <span class="token punctuation">-</span>f ./ballcat<span class="token punctuation">-</span>codegen<span class="token punctuation">-</span>backend/src/main/resources/docker/Dockerfile . <span class="token punctuation">-</span><span class="token punctuation">-</span>tag $<span class="token punctuation">{</span><span class="token punctuation">{</span> env.IMAGE_NAME <span class="token punctuation">}</span><span class="token punctuation">}</span><span class="token punctuation">:</span>latest
<span class="token comment"># 登录到 Docker Hub(或其他 Docker 注册表)</span>
<span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> Log in to Docker Hub
<span class="token comment"># 仅在非 PR 事件中执行登录</span>
<span class="token key atrule">if</span><span class="token punctuation">:</span> github.event_name <span class="token tag">!=</span> 'pull_request'
<span class="token comment"># 使用 Docker 登录动作</span>
<span class="token key atrule">uses</span><span class="token punctuation">:</span> docker/login<span class="token punctuation">-</span>action@v2
<span class="token key atrule">with</span><span class="token punctuation">:</span>
<span class="token comment"># 使用自定义的注册表(默认为 Docker Hub)</span>
<span class="token key atrule">registry</span><span class="token punctuation">:</span> $<span class="token punctuation">{</span><span class="token punctuation">{</span> env.REGISTRY <span class="token punctuation">}</span><span class="token punctuation">}</span>
<span class="token comment"># 从 GitHub Secrets 获取 Docker 用户名</span>
<span class="token key atrule">username</span><span class="token punctuation">:</span> $<span class="token punctuation">{</span><span class="token punctuation">{</span> secrets.DOCKER_USERNAME <span class="token punctuation">}</span><span class="token punctuation">}</span>
<span class="token comment"># 从 GitHub Secrets 获取 Docker 密码</span>
<span class="token key atrule">password</span><span class="token punctuation">:</span> $<span class="token punctuation">{</span><span class="token punctuation">{</span> secrets.DOCKER_PASSWORD <span class="token punctuation">}</span><span class="token punctuation">}</span>
<span class="token comment"># 提取 Docker 元数据(标签和标签)</span>
<span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> Extract Docker metadata
<span class="token key atrule">id</span><span class="token punctuation">:</span> meta
<span class="token comment"># 使用 Docker metadata-action 提取元数据</span>
<span class="token key atrule">uses</span><span class="token punctuation">:</span> docker/metadata<span class="token punctuation">-</span>action@v4
<span class="token key atrule">with</span><span class="token punctuation">:</span>
<span class="token comment"># 使用定义的镜像名称</span>
<span class="token key atrule">images</span><span class="token punctuation">:</span> $<span class="token punctuation">{</span><span class="token punctuation">{</span> env.IMAGE_NAME <span class="token punctuation">}</span><span class="token punctuation">}</span>
<span class="token key atrule">tags</span><span class="token punctuation">:</span> <span class="token punctuation">|</span><span class="token scalar string">
# 设置最新标签
type=raw,value=latest,enable={{is_default_branch}}
# 设置基于标签的标签
type=ref,enable=true,priority=600,prefix=,suffix=,event=tag</span>
<span class="token comment"># 构建并推送 Docker 镜像</span>
<span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> Build and push Docker image
<span class="token comment"># 使用 Docker build-push-action 来构建和推送镜像</span>
<span class="token key atrule">uses</span><span class="token punctuation">:</span> docker/build<span class="token punctuation">-</span>push<span class="token punctuation">-</span>action@v3
<span class="token key atrule">with</span><span class="token punctuation">:</span>
<span class="token comment"># Docker 构建上下文为当前目录</span>
<span class="token key atrule">context</span><span class="token punctuation">:</span> .
<span class="token comment"># Dockerfile 路径</span>
<span class="token key atrule">file</span><span class="token punctuation">:</span> ./ballcat<span class="token punctuation">-</span>codegen<span class="token punctuation">-</span>backend/src/main/resources/docker/Dockerfile
<span class="token comment"># 如果不是 PR,推送镜像</span>
<span class="token key atrule">push</span><span class="token punctuation">:</span> $<span class="token punctuation">{</span><span class="token punctuation">{</span> github.event_name <span class="token tag">!=</span> 'pull_request' <span class="token punctuation">}</span><span class="token punctuation">}</span>
<span class="token comment"># 使用提取的标签推送镜像</span>
<span class="token key atrule">tags</span><span class="token punctuation">:</span> $<span class="token punctuation">{</span><span class="token punctuation">{</span> steps.meta.outputs.tags <span class="token punctuation">}</span><span class="token punctuation">}</span>
<span class="token comment"># 使用提取的标签作为镜像元数据</span>
<span class="token key atrule">labels</span><span class="token punctuation">:</span> $<span class="token punctuation">{</span><span class="token punctuation">{</span> steps.meta.outputs.labels <span class="token punctuation">}</span><span class="token punctuation">}</span>
- 工作流触发时机
- Push 到
master
分支时自动触发工作流。 - Push 以
v
开头的标签时触发版本发布。 - Pull request 针对
master
分支时,自动触发构建和测试过程。
- 登录 Docker Hub
在推送镜像之前,我们需要先登录到 Docker Hub。通过 GitHub Secrets 保护 Docker Hub 的认证信息,确保账号密码安全。
效果展示 {#效果展示}
当你成功配置并推送到 GitHub 后,每次提交或标签推送都会自动触发工作流,并将构建好的 Docker 镜像上传至 Docker
Hub。你可以通过以下图片看到最终的工作流执行结果:
通过这种自动化方式,你可以节省大量的时间和精力,同时确保 Docker 镜像的构建和推送过程完全可重复。快来体验 GitHub Actions
的强大功能吧!
参考文章 {#参考文章}
利用 GitHub Actions 自动构建项目的 docker 镜像并发布到 DockerHub | 二丫讲梵
巧用 Github Action 自动推送 docker 镜像,白 piao github 服务器资源,还省时又省力_github 系统上传 hub.docker-CSDN 博客