Files
composite-actions/.github/workflows/docker-build-push-test.yaml
严浩 b821c98ddd
2025-04-04 16:03:38 +08:00

134 lines
4.4 KiB
YAML

# name: _打包推送镜像
on:
workflow_dispatch:
pull_request:
paths:
- 'docker-build-push/**'
- '.github/workflows/docker-build-push-test.yaml'
push:
paths:
- 'docker-build-push/**'
- '.github/workflows/docker-build-push-test.yaml'
env:
TZ: Asia/Shanghai
jobs:
build-and-push-ghcr:
runs-on: ubuntu-latest
env:
# https://github.com/docker/metadata-action/tree/v5/?tab=readme-ov-file#semver
# Event: push, Ref: refs/head/main, Tags: main
# Event: push tag, Ref: refs/tags/v1.2.3, Tags: 1.2.3, 1.2, 1, latest
# Event: push tag, Ref: refs/tags/v2.0.8-rc1, Tags: 2.0.8-rc1
metadata-action-tags: |
type=ref,event=branch
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
steps:
- name: 🔑 登录 GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: 🐳 构建并推送 Docker 镜像
uses: yanhao98/composite-actions/docker-build-push@main
with:
file: ./Dockerfile.test
platforms: linux/amd64,linux/arm64
push: true
load: false
meta_images: ghcr.io/${{ github.repository }}
meta_tags: ${{ env.metadata-action-tags }}
cache-from: type=gha,scope=${{ github.workflow }}
cache-to: type=gha,scope=${{ github.workflow }}
build-and-push-multi-registry:
runs-on: ubuntu-latest
steps:
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ vars.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: yanhao98/composite-actions/docker-build-push@main
id: docker-build-push
with:
file: ./Dockerfile.test
platforms: linux/amd64,linux/arm64
push: true
load: false
meta_images: |
docker.io/${{ vars.DOCKERHUB_USERNAME }}/docker-example
ghcr.io/${{ github.repository }}
meta_tags: | # https://github.com/docker/metadata-action
type=raw,value=latest,enable=true
cache-gha:
runs-on: ubuntu-latest
steps:
- uses: yanhao98/composite-actions/docker-build-push@main
id: docker-build-push
with:
file: ./Dockerfile.test
platforms: linux/amd64
push: false
load: true
build-args: |
SHA=${{ github.sha }}
# #####
# scope: https://github.com/docker/build-push-action/issues/252#issuecomment-881050512
# cache-to: mode=max
# #####
cache-from: type=gha,scope=${{ github.workflow }}
cache-to: type=gha,scope=${{ github.workflow }}
- name: Check Docker image
run: |
set -x;
docker images;
docker run --rm ${{ steps.docker-build-push.outputs.imageid }} cat /root/sha.txt;
cache-local:
runs-on: ubuntu-latest
steps:
- name: 🗄️ 缓存Docker层
uses: actions/cache@v4
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-IMAGE_NAME-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-IMAGE_NAME-
- uses: yanhao98/composite-actions/docker-build-push@main
id: docker-build-push
with:
file: ./Dockerfile.test
platforms: linux/amd64
push: false
load: true
build-args: |
SHA=${{ github.sha }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new
# Temp fix: 如果要在一个 job 中多次使用 buildx 缓存才需要这个步骤。
# https://github.com/docker/build-push-action/issues/252
# https://github.com/moby/buildkit/issues/1896
- name: 🔄 更新缓存
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
- name: Check Docker image
run: |
set -x;
docker images;
docker run --rm ${{ steps.docker-build-push.outputs.imageid }} cat /root/sha.txt;