1
+ name : Docker Build and Push
2
+
3
+ on :
4
+ push :
5
+ branches : [ main ]
6
+ tags : [ 'v*' ]
7
+ pull_request :
8
+ branches : [ main ]
9
+
10
+ env :
11
+ REGISTRY : ghcr.io
12
+ IMAGE_NAME : ${{ github.repository }}
13
+
14
+ jobs :
15
+ build :
16
+ runs-on : ubuntu-latest
17
+ permissions :
18
+ contents : read
19
+ packages : write
20
+
21
+ steps :
22
+ - name : Checkout repository
23
+ uses : actions/checkout@v4
24
+ with :
25
+ fetch-depth : 0
26
+
27
+ - name : Set up Docker Buildx
28
+ uses : docker/setup-buildx-action@v3
29
+
30
+ - name : Log in to Container Registry
31
+ if : github.event_name != 'pull_request'
32
+ uses : docker/login-action@v3
33
+ with :
34
+ registry : ${{ env.REGISTRY }}
35
+ username : ${{ github.actor }}
36
+ password : ${{ secrets.GITHUB_TOKEN }}
37
+
38
+ - name : Extract metadata
39
+ id : meta
40
+ uses : docker/metadata-action@v5
41
+ with :
42
+ images : ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
43
+ tags : |
44
+ type=ref,event=branch
45
+ type=ref,event=pr
46
+ type=sha,prefix={{branch}}-
47
+ type=semver,pattern={{version}}
48
+ type=semver,pattern={{major}}.{{minor}}
49
+ type=raw,value=latest,enable={{is_default_branch}}
50
+
51
+ - name : Generate version
52
+ id : version
53
+ run : |
54
+ if [ "${{ github.event_name }}" == "push" ] && [[ "${{ github.ref }}" == refs/tags/* ]]; then
55
+ VERSION="${{ github.ref_name }}"
56
+ else
57
+ MB_DATE=$(date '+%Y.%m.%d')
58
+ MB_HASH=$(git rev-parse --short HEAD)
59
+ VERSION="$MB_DATE-$MB_HASH"
60
+ fi
61
+ echo "version=$VERSION" >> $GITHUB_OUTPUT
62
+
63
+ - name : Build and push Docker image
64
+ uses : docker/build-push-action@v5
65
+ with :
66
+ context : .
67
+ platforms : linux/amd64
68
+ push : ${{ github.event_name != 'pull_request' }}
69
+ tags : ${{ steps.meta.outputs.tags }}
70
+ labels : ${{ steps.meta.outputs.labels }}
71
+ build-args : |
72
+ VERSION=${{ steps.version.outputs.version }}
73
+ cache-from : type=gha
74
+ cache-to : type=gha,mode=max
0 commit comments