对于个人开发者来说如果不想再自己电脑上搭建CI/DI系统(毕竟吃资源),Github Action是一个不二的选择。
本文我们来通过 Github Action 实现 SpringBoot 项目的自动编译、制作doceker镜像,最后推送到docker hub 仓库。
Github Action 是 Github 推出的一个持续集成和持续交付的平台,能够让你自动化你的编译、测试和部署流程。基础教程:Github Action 指南
1. 说说workflow 流程
先来说说springboot项目打包成docker镜像,并上传到仓库的流程
-
- 使用springboot项目 https://github.com/admin4j/alertmanager-dingtalk-webhook
-
- 运行maven命令
mvn -B clean package --file pom.xml
打包jar文件
- 运行maven命令
-
- 运行
docker build
构建docker镜像
- 运行
-
- 使用docker push 推送docker镜像
2. 配置 docker hub 访问密码
既然要想 docker hub 推送镜像那就必须要 docker hub 的认证配置,如退四步添加配置
3 上代码实战
3.1 创建一个 workflow
3.2 选择模板,这里先随便选一个,后续直接替换
3.3 修改 workflow file,保存运行
# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
name: CI
on:
push:
branches:
- "master"
tags:
- 'v*'
pull_request:
branches: [ "master" ]
env:
# Use docker.io for Docker Hub if empty
REGISTRY: ''
# github.repository as <account>/<repo>
IMAGE_NAME: ${{ github.repository }}
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up JDK 8
uses: actions/setup-java@v3
with:
java-version: '8'
distribution: 'temurin'
cache: maven
- name: Build with Maven
run: mvn -B clean package --file pom.xml
- name: Build the Docker image
run: docker build . --file Dockerfile --tag ${{env.IMAGE_NAME}}
- name: echo
run: echo $GITHUB_REF_NAME
# 登录
- name: Log into registry ${{ env.REGISTRY }}
if: github.event_name != 'pull_request'
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
# Extract metadata (tags, labels) for Docker
# https://github.com/docker/metadata-action
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.IMAGE_NAME }}
tags: |
# set latest tag for default branch
type=raw,value=latest,enable={{is_default_branch}}
# tag event
type=ref,enable=true,priority=600,prefix=,suffix=,event=tag
# 推送
- name: Build and push Docker image
uses: docker/build-push-action@v3
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
4. workflow 解释
监听 master 和 tag 推送
on:
push:
branches:
- "master"
tags:
- 'v*'
pull_request:
branches: [ "master" ]
-
uses: actions/checkout@v3
负责拉取代码 -
actions/setup-java@v3
负责maven编译 - docker/login-action@v2 负责登录 docker hub
-
docker/metadata-action@v4
负责 修改docker image tag信息 -
docker/build-push-action@v3
负责推送 docker镜像
5.最终效果
修改master 上文件,自动推送 docker laster 镜像
推送以v开头tag,自动推送admin4j/alertmanager-dingtalk-webhook:v*
镜像
查看效果https://hub.docker.com/repository/docker/admin4j/alertmanager-dingtalk-webhook/tags