Add GitHub workflow files

This commit is contained in:
Evil0ctal
2025-10-17 04:36:35 -07:00
parent 8f9b069756
commit 8d744397b7

View File

@@ -1,83 +1,87 @@
name: Build and Push Docker Image name: Publish Docker Image
on: on:
push: push:
branches: branches:
- main - main
- develop
tags: tags:
- 'v*.*.*' - '*'
paths: paths:
- 'api-service/**' - 'api-service/**'
- '.github/workflows/docker-image.yml' - '.github/workflows/docker-image.yml'
pull_request:
branches:
- main
paths:
- 'api-service/**'
workflow_dispatch: workflow_dispatch:
inputs:
name:
description: 'Person to greet'
required: true
default: 'Mona the Octocat'
home:
description: 'location'
required: false
default: 'The Octoverse'
env: env:
REGISTRY: docker.io APP_NAME: wechat-decrypt-api
IMAGE_NAME: ${{ secrets.DOCKERHUB_USERNAME }}/wechat-decrypt-api DOCKERHUB_REPO: ${{ secrets.DOCKERHUB_USERNAME }}/wechat-decrypt-api
jobs: jobs:
build-and-push: main:
runs-on: ubuntu-latest runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps: steps:
- name: Checkout repository # git checkout 代码
- name: Checkout
uses: actions/checkout@v4 uses: actions/checkout@v4
with:
fetch-depth: 0 # 获取完整的 git 历史,以便 git describe 工作
# 设置 QEMU, 后面 docker buildx 依赖此
- name: Set up QEMU - name: Set up QEMU
uses: docker/setup-qemu-action@v3 uses: docker/setup-qemu-action@v3
# 设置 Docker buildx, 方便构建 Multi platform 镜像
- name: Set up Docker Buildx - name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3 uses: docker/setup-buildx-action@v3
- name: Log in to Docker Hub # 登录 docker hub
if: github.event_name != 'pull_request' - name: Login to DockerHub
uses: docker/login-action@v3 uses: docker/login-action@v3
with: with:
username: ${{ secrets.DOCKERHUB_USERNAME }} username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }} password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Extract metadata (tags, labels) # 通过 git 命令获取当前 tag 信息, 存入环境变量 APP_VERSION
id: meta - name: Generate App Version
uses: docker/metadata-action@v5 run: echo APP_VERSION=`git describe --tags --always` >> $GITHUB_ENV
with:
images: ${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=raw,value=latest,enable={{is_default_branch}}
type=sha,prefix={{branch}}-
- name: Build and push Docker image # 构建 Docker 并推送到 Docker hub
- name: Build and push
id: docker_build
uses: docker/build-push-action@v5 uses: docker/build-push-action@v5
with: with:
context: ./api-service context: ./api-service
file: ./api-service/Dockerfile file: ./api-service/Dockerfile
platforms: linux/amd64,linux/arm64 push: true
push: ${{ github.event_name != 'pull_request' }} # 生成多平台镜像
tags: ${{ steps.meta.outputs.tags }} platforms: |
labels: ${{ steps.meta.outputs.labels }} linux/amd64
linux/arm64
# 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 }}
cache-from: type=gha cache-from: type=gha
cache-to: type=gha,mode=max cache-to: type=gha,mode=max
# 注意: 如果需要自动更新 Docker Hub 描述,请确保 DOCKERHUB_TOKEN 具有 "Read, Write & Delete" 权限 # 自动更新 Docker Hub 描述(从 README.md 同步到 overview
# 或者手动在 Docker Hub 上更新仓库描述 - name: Update Docker Hub Description
# - name: Docker Hub Description uses: peter-evans/dockerhub-description@v4
# if: github.event_name != 'pull_request' && github.ref == 'refs/heads/main' with:
# uses: peter-evans/dockerhub-description@v4 username: ${{ secrets.DOCKERHUB_USERNAME }}
# with: password: ${{ secrets.DOCKERHUB_TOKEN }}
# username: ${{ secrets.DOCKERHUB_USERNAME }} repository: ${{ env.DOCKERHUB_REPO }}
# password: ${{ secrets.DOCKERHUB_TOKEN }} readme-filepath: ./README.md
# repository: ${{ env.IMAGE_NAME }} short-description: "微信视频号解密工具 API 服务 - 基于 Isaac64 PRNG 算法的视频解密服务"
# readme-filepath: ./api-service/README.md