1
+ # This workflow uses actions that are not certified by GitHub.
2
+ # They are provided by a third-party and are governed by
3
+ # separate terms of service, privacy policy, and support
4
+ # documentation.
5
+
6
+ name : Build and Publish Docker Image
7
+
8
+ on :
9
+ workflow_call :
10
+ inputs :
11
+ name :
12
+ description : Name of the container
13
+ type : string
14
+ required : true
15
+ directory :
16
+ description : Directory of service
17
+ type : string
18
+ required : true
19
+ file :
20
+ description : Dockerfile to build
21
+ type : string
22
+ required : true
23
+ template-file :
24
+ description : Template dockefile to resolve
25
+ type : string
26
+ required : false
27
+
28
+
29
+ env :
30
+ REGISTRY : ghcr.io
31
+
32
+ jobs :
33
+ build-and-publish-with-cfg :
34
+ runs-on : ubuntu-latest
35
+ permissions :
36
+ contents : read
37
+ packages : write
38
+ steps :
39
+ - name : CPU Info
40
+ run : |
41
+ cat /proc/cpuinfo
42
+ echo "Number of cores: $(nproc)"
43
+ echo "Number of threads: $(nproc --all)"
44
+ - name : Checkout repository
45
+ uses : actions/checkout@v4
46
+ with :
47
+ fetch-depth : 0
48
+
49
+ - name : Set up QEMU
50
+ uses : docker/setup-qemu-action@v3
51
+
52
+ - name : Set up Docker Buildx
53
+ id : buildx
54
+ uses : docker/setup-buildx-action@v3
55
+
56
+ - name : Log in to the Container registry
57
+ uses : docker/login-action@v3
58
+ with :
59
+ registry : ${{ env.REGISTRY }}
60
+ username : ${{ github.actor }}
61
+ password : ${{ secrets.GITHUB_TOKEN }}
62
+
63
+ - name : Extract metadata (tags, labels) for docker image
64
+ id : meta
65
+ uses : docker/metadata-action@v5
66
+ with :
67
+ images : ${{ env.REGISTRY }}/${{ github.repository }}/${{ inputs.name }}
68
+ tags : |
69
+ type=raw,enable={{is_default_branch}},value=vnext
70
+ type=sha,enable={{is_default_branch}},prefix=vnext-,format=short
71
+ type=ref,event=pr,prefix=vnext-pr,suffix=-{{sha}}
72
+ type=ref,event=pr,prefix=vnext-pr
73
+ type=ref,enable={{is_default_branch}},event=branch
74
+ type=semver,pattern={{version}}
75
+ type=semver,pattern={{major}}.{{minor}}
76
+ type=semver,pattern={{major}}
77
+
78
+ - name : Set build args
79
+ id : build-args
80
+ run : |
81
+ echo "commit_date=$(date -u +%FT%TZ --date=@$(git show --format=%ct HEAD --quiet))" >> $GITHUB_OUTPUT
82
+ short_commit=$(git show --format=%h HEAD --quiet)
83
+ echo "short_commit=${short_commit}" >> $GITHUB_OUTPUT
84
+ echo "date=$(date -u +%FT%TZ)" >> $GITHUB_OUTPUT
85
+ version=$(git describe --tags --abbrev=0 | sed 's/^v//')
86
+ release_url="${{ github.server_url }}/${{ github.repository }}/releases/tag/${{ github.ref_name }}"
87
+ if [[ "${{ github.ref }}" != "refs/tags/"* ]]; then
88
+ version="${version}-pr${{ github.event.number }}"
89
+ elif curl --fail-with-body -s "$release_url" > /dev/null; then
90
+ echo "release_url=$release_url" >> $GITHUB_OUTPUT
91
+ fi
92
+ echo "version=$version" >> $GITHUB_OUTPUT
93
+
94
+ - name : First try to build and push docker image
95
+ uses : docker/build-push-action@v5
96
+ # This needs to be true to proceed to the next step of failure
97
+ continue-on-error : true
98
+ # You need to specify an id to be able to tell what the status of this action was
99
+ id : BuildAndPush1
100
+ with :
101
+ context : ./
102
+ platforms : linux/amd64,linux/arm64
103
+ builder : ${{ steps.buildx.outputs.name }}
104
+ build-args : |
105
+ DIRECTORY=${{ inputs.directory }}
106
+ NAME=${{ inputs.name }}
107
+ COMMIT_DATE=${{ steps.build-args.outputs.commit_date }}
108
+ SHORT_COMMIT=${{ steps.build-args.outputs.short_commit }}
109
+ DATE=${{ steps.build-args.outputs.date }}
110
+ VERSION=${{ steps.build-args.outputs.version }}
111
+ RELEASE_URL=${{ steps.build-args.outputs.release_url }}
112
+ file : ${{ inputs.file }}
113
+ push : true
114
+ tags : ${{ steps.meta.outputs.tags }}
115
+ labels : ${{ steps.meta.outputs.labels }}
116
+
117
+ - name : Second try to build and push docker image when first build fails
118
+ # Only run this step if step 1 fails. It knows that step one failed because we specified an `id` for the first step
119
+ if : steps.BuildAndPush1.outcome == 'failure'
120
+ uses : docker/build-push-action@v5
121
+ with :
122
+ context : ./
123
+ platforms : linux/amd64,linux/arm64
124
+ builder : ${{ steps.buildx.outputs.name }}
125
+ build-args : |
126
+ DIRECTORY=${{ inputs.directory }}
127
+ NAME=${{ inputs.name }}
128
+ COMMIT_DATE=${{ steps.build-args.outputs.commit_date }}
129
+ SHORT_COMMIT=${{ steps.build-args.outputs.short_commit }}
130
+ DATE=${{ steps.build-args.outputs.date }}
131
+ VERSION=${{ steps.build-args.outputs.version }}
132
+ RELEASE_URL=${{ steps.build-args.outputs.release_url }}
133
+ file : ${{ inputs.file }}
134
+ push : true
135
+ tags : ${{ steps.meta.outputs.tags }}
136
+ labels : ${{ steps.meta.outputs.labels }}
0 commit comments