-
Notifications
You must be signed in to change notification settings - Fork 0
225 lines (202 loc) · 7.59 KB
/
docker-build.yml
File metadata and controls
225 lines (202 loc) · 7.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
name: Reusable Docker
on:
workflow_call:
inputs:
### Docker Name
repo_name:
description: "Summary generate name, default will use repo name"
required: false
type: string
default: "${{ github.repository }}"
docker_name:
description: "ghcr.io push name"
required: true
type: string
###
### Docker Settings
publish:
description: "Push Image to ghcr.io, default is always push"
required: false
type: boolean
default: true
context:
description: "Custom file path"
required: false
type: string
dockerfile:
description: "Docker file name, default ``Dockerfile``"
required: false
type: string
default: "Dockerfile"
platforms:
description: "Image support platforms, default ``linux/amd64,linux/arm64``"
required: false
type: string
default: "linux/amd64,linux/arm64"
### Docker Labeling ###
docker_label_author:
description: "Docker Label: Author"
required: true
type: string
docker_label_title:
description: "Docker Label: Title"
required: false
type: string
docker_label_description:
description: "Docker Label: Description"
required: false
type: string
###
### Version Detecter ###
version_detecter:
description: "Enable version detecter, default ``false``"
required: false
type: boolean
default: false
version_detecter_file:
description: "File of detect version string, default ``Dockerfile``"
required: false
type: string
default: "Dockerfile"
version_detecter_var:
description: "Placeholder of detect"
required: false
type: string
version_detecter_cut:
description: "Cut number, default ``2``"
required: false
type: string
default: "2"
###
### Build Args ###
build_args:
description: "Build Args"
required: false
type: boolean
default: false
###
### Special Build Args ###
special_build:
description: "Special step for generate buildtime from docker meta step"
required: false
type: boolean
default: false
special_build_args:
description: "Special step of the build time var name"
required: false
type: string
###
permissions:
contents: read
packages: write
jobs:
docker-test:
name: Docker Build (Test)
runs-on: ubuntu-latest
steps:
- name: Checking Repository
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
- name: Setup multiarch
uses: docker/setup-qemu-action@ce360397dd3f832beb865e1373c09c0e9f86d70a # v4.0.0
- name: Setup Buildx
uses: docker/setup-buildx-action@4d04d5d9486b7bd6fa91e7baf45bbb4f8b9deedd # v4.0.0
- name: Cache Docker layers
uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5
with:
path: /tmp/.buildx-cache
key: ${{ github.repository }}-${{ github.workflow }}-${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ github.repository }}-${{ github.workflow }}-${{ runner.os }}-buildx-
- name: Build & Test
uses: docker/build-push-action@bcafcacb16a39f128d818304e6c9c0c18556b85f # v7.1.0
with:
context: ${{ inputs.context }}
file: ${{ inputs.dockerfile }}
platforms: ${{ inputs.platforms }}
push: false
tags: ${{ inputs.docker_name }}:test
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max
- name: Move cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
docker-publish:
name: Docker Build (Publish)
runs-on: ubuntu-latest
needs: [ docker-test ]
if: github.event_name != 'pull_request'
steps:
- name: Checking Repository
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
- name: Setup multiarch
uses: docker/setup-qemu-action@ce360397dd3f832beb865e1373c09c0e9f86d70a # v4.0.0
- name: Setup Buildx
uses: docker/setup-buildx-action@4d04d5d9486b7bd6fa91e7baf45bbb4f8b9deedd # v4.0.0
- name: Cache Docker layers
uses: actions/cache@27d5ce7f107fe9357f9df03efb73ab90386fccae # v5.0.5
with:
path: /tmp/.buildx-cache
key: ${{ github.repository }}-${{ github.workflow }}-${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ github.repository }}-${{ github.workflow }}-${{ runner.os }}-buildx-
- name: Login ghcr.io
uses: docker/login-action@4907a6ddec9925e35a0a9e82d7399ccc52663121 # v4.1.0
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Detect Version
id: detect_version
if: ${{ inputs.version_detecter }}
run: |
version=$(grep '${{ inputs.version_detecter_var }}' ${{ inputs.version_detecter_file }} | cut -d "=" -f ${{ inputs.version_detecter_cut }})
echo "version=${version}" >> $GITHUB_OUTPUT
echo "## ${{ inputs.repo_name }}" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "Detect Image Version: ${version}" >> $GITHUB_STEP_SUMMARY
- name: Docker meta
id: meta
uses: docker/metadata-action@030e881283bb7a6894de51c315a6bfe6a94e05cf # v6.0.0
with:
images: |
${{ inputs.docker_name }}
tags: |
type=raw,value=latest,enable={{is_default_branch}}
type=semver,pattern={{version}},value=${{ steps.detect_version.outputs.version }}
labels: |
org.opencontainers.image.authors=${{ inputs.docker_label_author }}
org.opencontainers.image.title=${{ inputs.docker_label_title }}
org.opencontainers.image.description=${{ inputs.docker_label_description }}
- name: Build & Push
if: ${{ ! inputs.special_build }}
uses: docker/build-push-action@bcafcacb16a39f128d818304e6c9c0c18556b85f # v7.1.0
with:
context: ${{ inputs.context }}
file: ${{ inputs.dockerfile }}
platforms: ${{ inputs.platforms }}
push: ${{ inputs.publish }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
${{ inputs.build_args }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max
- name: Build & Push (Special Build Args)
if: ${{ inputs.special_build }}
uses: docker/build-push-action@bcafcacb16a39f128d818304e6c9c0c18556b85f # v7.1.0
with:
context: ${{ inputs.context }}
file: ${{ inputs.dockerfile }}
platforms: linux/amd64,linux/arm64
push: ${{ inputs.publish }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
build-args: |
${{ inputs.special_build_args }}=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.created'] }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max
- name: Move cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache