1+ name : Build and Push Docker Image
2+
3+ on :
4+ push :
5+ branches : [ main, master ]
6+ tags : ['v*']
7+ pull_request :
8+ branches : [ main, master ]
9+ workflow_dispatch :
10+
11+ concurrency :
12+ group : docker-${{ github.workflow }}-${{ github.ref }}
13+ cancel-in-progress : true
14+
15+ env :
16+ REGISTRY : ghcr.io
17+ IMAGE_NAME : ${{ github.repository }}
18+
19+ jobs :
20+ build :
21+ runs-on : ubuntu-latest
22+ permissions :
23+ contents : read
24+ packages : write
25+ steps :
26+ - name : Checkout repository
27+ uses : actions/checkout@v4
28+
29+ - name : Set up QEMU (multi-architecture)
30+ uses : docker/setup-qemu-action@v3
31+
32+ - name : Set up Docker Buildx
33+ uses : docker/setup-buildx-action@v3
34+
35+ - name : Extract metadata
36+ id : meta
37+ uses : docker/metadata-action@v5
38+ with :
39+ images : ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
40+ tags : |
41+ type=ref,event=branch
42+ type=ref,event=tag
43+ type=sha,format=short
44+ type=raw,value=latest,enable={{is_default_branch}}
45+
46+ # PR: Build only, no push, single architecture
47+ - name : Build (PR without push)
48+ if : github.event_name == 'pull_request'
49+ uses : docker/build-push-action@v5
50+ with :
51+ context : .
52+ platforms : linux/amd64
53+ push : false
54+ tags : ${{ steps.meta.outputs.tags }}
55+ labels : ${{ steps.meta.outputs.labels }}
56+ cache-from : type=gha
57+ cache-to : type=gha,mode=max
58+
59+ # Non-PR: Login + multi-architecture push
60+ - name : Login to container registry
61+ if : github.event_name != 'pull_request'
62+ uses : docker/login-action@v3
63+ with :
64+ registry : ${{ env.REGISTRY }}
65+ username : ${{ github.actor }}
66+ password : ${{ secrets.GITHUB_TOKEN }}
67+
68+ - name : Build and push Docker image
69+ if : github.event_name != 'pull_request'
70+ uses : docker/build-push-action@v5
71+ with :
72+ context : .
73+ platforms : linux/amd64,linux/arm64
74+ push : true
75+ tags : ${{ steps.meta.outputs.tags }}
76+ labels : ${{ steps.meta.outputs.labels }}
77+ cache-from : type=gha
78+ cache-to : type=gha,mode=max
79+
0 commit comments