-
-
Notifications
You must be signed in to change notification settings - Fork 386
227 lines (201 loc) · 7.21 KB
/
docker.yml
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
226
227
name: Docker Build and Publish
on:
workflow_call:
inputs:
tags:
type: string
default: |
type=raw,value={{branch}}
type=semver,pattern={{raw}},enable=${{ github.ref_type == 'tag' }}
type=raw,value=latest,enable=${{ github.ref_type == 'tag' }}
type=sha
flavor:
type: string
default: |
latest=auto
prefix=
suffix=
build-args:
type: string
dockerfile:
type: string
default: Dockerfile
context:
type: string
default: "{{defaultContext}}"
context-cache:
type: string
description: a cache key
required: false
context-repository:
type: string
description: github repository to use
context-repository-ref:
type: string
push:
type: boolean
default: true
registry-repo-name:
type: string
default: ${{ github.event.repository.name }}
registry-dockerhub-enable:
type: boolean
default: false
registry-github-enable:
type: boolean
default: true
registry-readme:
type: string
default: https://raw.githubusercontent.com/Josh-XT/AGiXT/main/docs/README.md
runs-on:
type: string
default: ubuntu-22.04
platforms:
type: string
default: linux/amd64,linux/arm64/v8
cache-from:
type: string
default: type=gha
cache-to:
type: string
default: type=gha,mode=max
outputs:
digest:
description: "Digest of docker image"
value: ${{ jobs.build.outputs.digest }}
primary-image:
description: "Primary full name of pushed docker image"
value: ${{ jobs.build.outputs.primary-image }}
secrets:
DOCKERHUB_TOKEN:
required: false
jobs:
build:
name: Docker (${{ inputs.registry-github-enable == true && inputs.registry-dockerhub-enable == true && 'GitHub, DockerHub' || inputs.registry-github-enable == true && 'GitHub' || 'DockerHub' }})
runs-on: ${{ inputs.runs-on }}
permissions:
packages: write
contents: read
outputs:
digest: ${{ steps.dockerBuild.outputs.digest }}
primary-image: ${{ steps.get-primary-image.outputs.primary-image }}
steps:
- name: Log in to Docker Hub
if: inputs.registry-dockerhub-enable
uses: docker/login-action@v3.3.0
with:
username: ${{ vars.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Log in to the Github registry
if: inputs.registry-github-enable
uses: docker/login-action@v3.3.0
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Generate dockerhub full repo path
if: inputs.registry-dockerhub-enable
id: step_one
run: |
echo "dockerhub-repo=${{ vars.DOCKERHUB_USERNAME }}/${{ inputs.registry-repo-name }}" >> "$GITHUB_ENV"
- name: Set up QEMU
uses: docker/setup-qemu-action@v3.3.0
# Needed for cache layers on github registry
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3.8.0
## Cache based contexts
- name: Restore cached context
if: inputs.context-cache
uses: actions/cache/restore@v4.2.0
with:
path: cached-context
key: ${{ inputs.context-cache }}
fail-on-cache-miss: true
- name: Set docker-context for cache
if: inputs.context-cache
run: echo "docker-context=cached-context" >> $GITHUB_ENV
######
## Repo based context (fixes submodules etc)
- name: Checkout repo
uses: actions/checkout@v4.2.2
if: inputs.context-repository
with:
fetch-depth: 1
- name: Checkout external context
uses: actions/checkout@v4.2.2
if: inputs.context-repository
with:
repository: ${{ inputs.context-repository }}
ref: ${{ inputs.context-repository-ref }}
path: repository-context
fetch-depth: 1
submodules: false
- name: Set docker-context for repository
if: inputs.context-repository
run: echo "docker-context=repository-context" >> $GITHUB_ENV
######
###### Docker build metadata
- name: Set image names
run: |
EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64)
echo "image-names<<$EOF" >> "$GITHUB_ENV"
if ${{ inputs.registry-dockerhub-enable }} ; then
echo "${{ env.dockerhub-repo }}" >> "$GITHUB_ENV"
fi
if ${{ inputs.registry-github-enable }} ; then
echo "ghcr.io/${{ github.repository_owner }}/${{ inputs.registry-repo-name }}" >> "$GITHUB_ENV"
fi
echo "$EOF" >> "$GITHUB_ENV"
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5.6.1
with:
images: ${{ env.image-names }}
# generate Docker tags based on the following events/attributes
tags: ${{ inputs.tags }}
flavor: ${{ inputs.flavor }}
######
- name: Build and push Docker images
id: dockerBuild
uses: docker/build-push-action@v6.11.0
with:
platforms: ${{ inputs.platforms }}
file: ${{ inputs.dockerfile }}
context: ${{ env.docker-context || inputs.context }}
build-args: ${{ inputs.build-args }}
push: ${{ inputs.push }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: ${{ inputs.cache-from }}
cache-to: ${{ inputs.cache-to }}
# publish README on docker hub
- uses: actions/checkout@v4.2.2
with:
fetch-depth: 1
###### Load docker readme file from remote
- name: Get remote registry readme
if: startsWith(inputs.registry-readme, 'https')
run: wget ${{ inputs.registry-readme }} -O README.docker.remote.md
- name: Set registry readme file
run: |
if ${{ startsWith( inputs.registry-readme, 'https' ) }} ; then
echo "registry-readme=README.docker.remote.md" >> "$GITHUB_ENV"
else
echo "registry-readme=${{ inputs.registry-readme }}" >> "$GITHUB_ENV"
fi
#######
- name: Docker Hub Description
if: inputs.registry-dockerhub-enable
uses: peter-evans/dockerhub-description@v4
with:
username: ${{ vars.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
repository: ${{ env.dockerhub-repo }}
readme-filepath: ${{ env.registry-readme }}
- name: Selecting primary image from
run: echo "${{ fromJSON(steps.dockerBuild.outputs.metadata)['image.name'] }}"
- name: Primary image will be
run: echo "primary-image=$( cut -d ',' -f 1 <<< "${{ fromJSON(steps.dockerBuild.outputs.metadata)['image.name'] }}" )"
- name: Set output image
id: get-primary-image
run: echo "primary-image=$( cut -d ',' -f 1 <<< "${{ fromJSON(steps.dockerBuild.outputs.metadata)['image.name'] }}" )" >> "$GITHUB_OUTPUT"