Skip to content

Commit 1f08f36

Browse files
committed
CI: 添加 Docker 自动构建与发布工作流
1 parent 39c7713 commit 1f08f36

1 file changed

Lines changed: 63 additions & 0 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
name: Docker Build and Publish
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
# Publish semver tags as releases.
7+
tags: [ 'v*.*.*' ]
8+
pull_request:
9+
branches: [ "main" ]
10+
11+
env:
12+
# Use docker.io for Docker Hub if empty
13+
REGISTRY: docker.io
14+
# github.repository as <account>/<repo>
15+
IMAGE_NAME: ${{ github.repository }}
16+
17+
jobs:
18+
build:
19+
runs-on: ubuntu-latest
20+
permissions:
21+
contents: read
22+
packages: write
23+
# This is used to complete the identity challenge
24+
# with sigstore/fulcio when running outside of PRs.
25+
id-token: write
26+
27+
steps:
28+
- name: Checkout repository
29+
uses: actions/checkout@v4
30+
31+
# Set up Buildx (required for multi-arch build if needed, and for caching)
32+
- name: Set up Docker Buildx
33+
uses: docker/setup-buildx-action@v3
34+
35+
# Login to Docker Hub
36+
- name: Log into Docker Hub
37+
if: github.event_name != 'pull_request'
38+
uses: docker/login-action@v3
39+
with:
40+
username: ${{ secrets.DOCKERHUB_USERNAME }}
41+
password: ${{ secrets.DOCKERHUB_TOKEN }}
42+
43+
# Extract metadata (tags, labels) for Docker
44+
- name: Extract Docker metadata
45+
id: meta
46+
uses: docker/metadata-action@v5
47+
with:
48+
images: ${{ secrets.DOCKERHUB_USERNAME }}/home
49+
tags: |
50+
type=raw,value=latest,enable={{is_default_branch}}
51+
type=sha,format=long
52+
53+
# Build and push Docker image with Buildx (don't push on PR)
54+
- name: Build and push Docker image
55+
id: build-and-push
56+
uses: docker/build-push-action@v5
57+
with:
58+
context: .
59+
push: ${{ github.event_name != 'pull_request' }}
60+
tags: ${{ steps.meta.outputs.tags }}
61+
labels: ${{ steps.meta.outputs.labels }}
62+
cache-from: type=gha
63+
cache-to: type=gha,mode=max

0 commit comments

Comments
 (0)