1+ name : Build and Push Docker Image
2+
3+ on :
4+ push :
5+ branches : [main]
6+ pull_request :
7+ branches : [main]
8+ workflow_dispatch :
9+ inputs :
10+ reason :
11+ description : ' Reason for manual trigger'
12+ required : false
13+ default : ' Manual trigger'
14+
15+ env :
16+ REGISTRY : ghcr.io
17+ IMAGE_NAME : ${{ github.repository }}
18+
19+ jobs :
20+ build-and-push :
21+ runs-on : ubuntu-latest
22+ permissions :
23+ contents : read
24+ packages : write
25+
26+ steps :
27+ - name : Checkout repository
28+ uses : actions/checkout@v6
29+ with :
30+ fetch-depth : ' 0'
31+
32+ # 安装 Node.js
33+ - name : Set up Node.js
34+ uses : actions/setup-node@v4
35+ with :
36+ node-version : 22
37+ cache : ' npm'
38+
39+ - name : Install dependencies and build
40+ run : |
41+ npm ci
42+ npm run build
43+
44+ # 提取版本号
45+ - name : Extract version from package.json
46+ id : extract-version
47+ run : |
48+ FULL_VERSION=$(node -p "require('./package.json').version")
49+ MAJOR_MINOR_VERSION=$(echo "$FULL_VERSION" | sed -E 's/^([0-9]+\.[0-9]+)\.[0-9]+$/\1/')
50+
51+ echo "full-version=$FULL_VERSION" >> $GITHUB_OUTPUT
52+ echo "major-minor-version=$MAJOR_MINOR_VERSION" >> $GITHUB_OUTPUT
53+ echo "✅ 完整版本:$FULL_VERSION"
54+ echo "✅ 主版本:$MAJOR_MINOR_VERSION"
55+
56+ - name : Set up QEMU
57+ uses : docker/setup-qemu-action@v4
58+
59+ - name : Set up Docker Buildx
60+ uses : docker/setup-buildx-action@v4
61+
62+ - name : Log in to the Container registry
63+ uses : docker/login-action@v4
64+ with :
65+ registry : ${{ env.REGISTRY }}
66+ username : ${{ github.actor }}
67+ password : ${{ secrets.GITHUB_TOKEN }}
68+
69+ - name : Extract metadata (tags, labels) for Docker
70+ id : meta
71+ uses : docker/metadata-action@v6
72+ with :
73+ images : ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
74+ tags : |
75+ type=raw,value=latest,enable={{is_default_branch}}
76+ type=ref,event=branch
77+ type=ref,event=pr
78+ type=raw,value=${{ steps.extract-version.outputs.full-version }}
79+ type=raw,value=${{ steps.extract-version.outputs.major-minor-version }}
80+
81+ - name : Build and push Docker image
82+ id : build-and-push
83+ uses : docker/build-push-action@v7
84+ with :
85+ context : .
86+ push : ${{ github.event_name != 'pull_request' }}
87+ tags : ${{ steps.meta.outputs.tags }}
88+ labels : ${{ steps.meta.outputs.labels }}
89+ platforms : linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8,linux/ppc64le,linux/riscv64,linux/s390x
90+ cache-from : type=gha
91+ cache-to : type=gha,mode=max
0 commit comments