1+ name : Build and Push Docker Image
2+
3+ on :
4+ # Trigger on pushes to main branch
5+ push :
6+ branches : [ main, master ]
7+ # Trigger on new releases/tags
8+ release :
9+ types : [ published ]
10+ # Allow manual trigger
11+ workflow_dispatch :
12+
13+ env :
14+ GHCR_REGISTRY : ghcr.io
15+ DOCKERHUB_REGISTRY : docker.io
16+ IMAGE_NAME : ${{ github.repository }}
17+
18+ jobs :
19+ build-and-push :
20+ runs-on : ubuntu-latest
21+ permissions :
22+ contents : read
23+ packages : write
24+
25+ steps :
26+ - name : Checkout repository
27+ uses : actions/checkout@v4
28+
29+ - name : Log in to GitHub Container Registry
30+ uses : docker/login-action@v3
31+ with :
32+ registry : ${{ env.GHCR_REGISTRY }}
33+ username : ${{ github.actor }}
34+ password : ${{ secrets.GITHUB_TOKEN }}
35+
36+ - name : Log in to Docker Hub
37+ uses : docker/login-action@v3
38+ with :
39+ registry : ${{ env.DOCKERHUB_REGISTRY }}
40+ username : ${{ secrets.DOCKERHUB_USERNAME }}
41+ password : ${{ secrets.DOCKERHUB_TOKEN }}
42+
43+ - name : Extract metadata for GHCR
44+ id : meta-ghcr
45+ uses : docker/metadata-action@v5
46+ with :
47+ images : ${{ env.GHCR_REGISTRY }}/${{ env.IMAGE_NAME }}
48+ tags : |
49+ # set latest tag for main/master branch only
50+ type=raw,value=latest,enable=${{ github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master' }}
51+ # set full version tag for releases (1.0.0, 1.0.1, etc.)
52+ type=semver,pattern={{version}}
53+
54+ - name : Extract metadata for Docker Hub
55+ id : meta-dockerhub
56+ uses : docker/metadata-action@v5
57+ with :
58+ images : ${{ github.repository }}
59+ tags : |
60+ # set latest tag for main/master branch only
61+ type=raw,value=latest,enable=${{ github.ref == 'refs/heads/main' || github.ref == 'refs/heads/master' }}
62+ # set full version tag for releases (1.0.0, 1.0.1, etc.)
63+ type=semver,pattern={{version}}
64+
65+ - name : Build and push to both registries
66+ uses : docker/build-push-action@v5
67+ with :
68+ context : .
69+ push : true
70+ tags : |
71+ ${{ steps.meta-ghcr.outputs.tags }}
72+ ${{ steps.meta-dockerhub.outputs.tags }}
73+ labels : ${{ steps.meta-ghcr.outputs.labels }}
0 commit comments