Skip to content

Commit

Permalink
Update docker-image.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
xbcvv authored Jul 27, 2024
1 parent 3846b81 commit f2b2253
Showing 1 changed file with 53 additions and 35 deletions.
88 changes: 53 additions & 35 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# 工作流名称
name: Build and Push Docker Image

on:
push:
# push tag 时触发执行
tags:
- v*
branches:
Expand All @@ -12,45 +14,61 @@ on:

jobs:
build:
# 在 Ubuntu 上运行
runs-on: ubuntu-latest

# 定义环境变量, 后面会使用
# 定义 APP_NAME 用于 docker build-args
# 定义 DOCKERHUB_REPO 标记 docker hub repo 名称
env:
APP_NAME: aggregator
DOCKERHUB_REPO: qdw1010/aggregator

steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Set up QEMU
uses: docker/setup-qemu-action@v2
with:
platforms: all

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
with:
install: true

- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Generate App Version
id: version
run: echo "APP_VERSION=$(git describe --tags --always)" >> $GITHUB_ENV

- name: Build and push Docker image
uses: docker/build-push-action@v3
with:
context: .
platforms: linux/amd64,linux/arm64
push: true
build-args: |
APP_NAME=${{ env.APP_NAME }}
APP_VERSION=${{ env.APP_VERSION }}
tags: |
${{ env.DOCKERHUB_REPO }}:latest
${{ env.DOCKERHUB_REPO }}:${{ env.APP_VERSION }}
# git checkout 代码
- name: Checkout repository
uses: actions/checkout@v2

# 设置 QEMU, 后面 docker buildx 依赖此.
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
with:
platforms: all

# 设置 Docker buildx, 方便构建 Multi platform 镜像
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
with:
install: true

# 登录 docker hub
- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
# GitHub Repo => Settings => Secrets 增加 docker hub 登录密钥信息
# DOCKERHUB_USERNAME 是 docker hub 账号名
# DOCKERHUB_TOKEN: docker hub => Account Setting => Security 创建
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

# 通过 git 命令获取当前 tag 信息, 存入环境变量 APP_VERSION
- name: Generate App Version
id: version
run: echo "APP_VERSION=$(git describe --tags --always)" >> $GITHUB_ENV

- name: Build and push Docker image
uses: docker/build-push-action@v3
with:
context: .
# 生成多平台镜像
platforms: linux/amd64,linux/arm64
# 是否 docker push
push: true
# docker build arg, 注入 APP_NAME/APP_VERSION
build-args: |
APP_NAME=${{ env.APP_NAME }}
APP_VERSION=${{ env.APP_VERSION }}
# 生成两个 docker tag: ${APP_VERSION} 和 latest
tags: |
${{ env.DOCKERHUB_REPO }}:latest
${{ env.DOCKERHUB_REPO }}:${{ env.APP_VERSION }}

0 comments on commit f2b2253

Please sign in to comment.