This repository has been archived by the owner on Feb 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
278 lines (231 loc) · 13.6 KB
/
docker-build-push.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
name: Docker Image CI
on:
push:
branches: [ main ]
schedule:
- cron: '0 5 * * *'
workflow_dispatch:
jobs:
get-version-number:
runs-on: windows-latest
outputs:
VERSION: ${{ steps.step1.outputs.VERSION }}
GCLOUD_CLI_VERSION: ${{ steps.step1.outputs.GCLOUD_CLI_VERSION }}
KUBECTL_MAJOR_MINOR_VERSION: ${{ steps.step1.outputs.KUBECTL_MAJOR_MINOR_VERSION }}
CONTINUE: ${{ steps.step1.outputs.Continue }}
WIN2019_VERSION: ${{ steps.step1.outputs.WIN2019_VERSION }}
WIN2022_VERSION: ${{ steps.step1.outputs.WIN2022_VERSION }}
steps:
- uses: actions/checkout@v3
- id: step1
name: Compare latest version with container
run: |
Write-Output "Getting GCLOUD CLI version (needed for gke-auth-plugin)"
$googleCloudSdkInfo = Invoke-RestMethod "https://registry.hub.docker.com/v2/repositories/google/cloud-sdk/tags?page=1"
$otherImages = $googleCloudSdkInfo.results | Where-Object { $_.name -ine "latest" }
$latestTag = $googleCloudSdkInfo.results | Where-Object { $_.name -ieq "latest" }
if($null -eq $latestTag) {
throw "Couldnt find latest tag for Google Cloud SDK version from DockerHub"
}
$latestTagImage = $latestTag.images | Where-Object {$_.os -ieq "linux" -and $_.architecture -ieq "amd64" -and $_.status -ieq "active"} | Select-Object -First 1
if($null -eq $latestTagImage) {
throw "Couldnt find latest tag image for Google Cloud SDK version for linux/amd64"
}
$latestDigest = $latestTagImage.digest
Write-Output "Found latest digest: $latestDigest"
$GCLOUD_CLI_VERSION = ""
foreach($result in $otherImages) {
$matchingImageDigest = $result.images | Where-Object {$_.os -ieq "linux" -and $_.architecture -ieq "amd64" -and $_.status -ieq "active" -and $_.digest -ieq $latestDigest } | Select-Object -First 1
if($null -eq $matchingImageDigest) {
continue;
}
else {
$version = $result.name
Write-Output "Found version '$version' that matches digest: $latestDigest"
$versionSplit = $version.Split(".")
$GCLOUD_CLI_VERSION = "$($versionSplit[0]).$($versionSplit[1]).$($versionSplit[2])"
break;
}
}
if ([string]::IsNullOrWhiteSpace($GCLOUD_CLI_VERSION)) {
throw "No GCLOUD CLI version with digest $latestDigest found"
}
else {
echo "GCLOUD_CLI_VERSION=$GCLOUD_CLI_VERSION" >> $env:GITHUB_OUTPUT
}
Write-Output "Getting kubectl version"
$chocoInformationRaw = choco info kubernetes-cli --limitoutput
$versionOutput = ($chocoInformationRaw.Split("|"))[1]
[System.Version]$version = $null
$versionParsed = [System.Version]::TryParse($versionOutput, [ref]$version)
if(-not $versionParsed) {
Write-Host "Unable to parse '$versionOutput' as a valid version. Won't continue"
echo "CONTINUE=No" >> $env:GITHUB_OUTPUT
}
else {
$versionToCompare = "$($version.Major).$($version.Minor).$($version.Build)"
Write-Host "Parsed version as $versionToCompare"
# Major/minor version is used for linux dockerfile to install kubectl correctly
$majorMinorVersion = "$($version.Major).$($version.Minor)"
Write-Host "kubectl Major.Minor: $majorMinorVersion"
$workerToolsTags = Invoke-RestMethod "https://registry.hub.docker.com/v2/repositories/octopuslabs/k8s-workertools/tags?page_size=50"
$matchingTag = $workerToolsTags.results | Where-Object { $_.name -eq $versionToCompare }
echo "VERSION=$versionToCompare" >> $env:GITHUB_OUTPUT
echo "KUBECTL_MAJOR_MINOR_VERSION=$majorMinorVersion" >> $env:GITHUB_OUTPUT
if ($null -ne $matchingTag)
{
Write-Host "Docker container already has latest version"
echo "CONTINUE=No" >> $env:GITHUB_OUTPUT
}
else
{
Write-Host "We need to upgrade the container to $versionToCompare"
Write-Host "Getting OS versions for windows 2022 and 2019"
$win2019_manifest = (docker manifest inspect --verbose "octopuslabs/workertools:latest-windows.2019" | ConvertFrom-Json)
$WIN2019_VERSION = $win2019_manifest.Descriptor.Platform.'os.version'
Write-Host "WIN2019_VERSION: $WIN2019_VERSION"
$win2022_manifest = (docker manifest inspect --verbose "octopuslabs/workertools:latest-windows.2022" | ConvertFrom-Json)
$WIN2022_VERSION = $win2022_manifest.Descriptor.Platform.'os.version'
Write-Host "WIN2022_VERSION: $WIN2022_VERSION"
if([string]::IsNullOrWhiteSpace($WIN2019_VERSION) -or [string]::IsNullOrWhiteSpace($WIN2022_VERSION)) {
throw "Could not establish OS versions for windows 2022 and 2019 needed for docker manifest"
}
echo "WIN2019_VERSION=$WIN2019_VERSION" >> $env:GITHUB_OUTPUT
echo "WIN2022_VERSION=$WIN2022_VERSION" >> $env:GITHUB_OUTPUT
Write-Host "We have everything we need, continuing."
echo "CONTINUE=Yes" >> $env:GITHUB_OUTPUT
}
}
shell: powershell
build-ubuntu:
needs: [get-version-number]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: DockerHub Login
env:
USERNAME: ${{ secrets.DOCKER_HUB_USER }}
PASSWORD: ${{ secrets.DOCKER_HUB_PAT }}
run: docker login --username $USERNAME --password "$PASSWORD"
if: ${{ needs.get-version-number.outputs.CONTINUE == 'Yes' }}
- name: Build the ubuntu.2004 image
env:
VERSION_NUMBER: ${{ needs.get-version-number.outputs.VERSION }}
KUBECTL_MAJOR_MINOR_VERSION: ${{ needs.get-version-number.outputs.KUBECTL_MAJOR_MINOR_VERSION }}
run: docker build ./ubuntu-2004 --build-arg KUBECTL_MAJOR_MINOR_VERSION=$KUBECTL_MAJOR_MINOR_VERSION --tag octopuslabs/k8s-workertools:$VERSION_NUMBER-ubuntu.2004 --tag octopuslabs/k8s-workertools:latest-ubuntu.2004
if: ${{ needs.get-version-number.outputs.CONTINUE == 'Yes' }}
- name: Push the ubuntu.2004 version-specific image
env:
VERSION_NUMBER: ${{ needs.get-version-number.outputs.VERSION }}
run: docker push octopuslabs/k8s-workertools:$VERSION_NUMBER-ubuntu.2004
if: ${{ needs.get-version-number.outputs.CONTINUE == 'Yes' }}
- name: Push the ubuntu.2004 latest image
run: docker push octopuslabs/k8s-workertools:latest-ubuntu.2004
if: ${{ needs.get-version-number.outputs.CONTINUE == 'Yes' }}
- name: Build the ubuntu.2204 image
env:
VERSION_NUMBER: ${{ needs.get-version-number.outputs.VERSION }}
KUBECTL_MAJOR_MINOR_VERSION: ${{ needs.get-version-number.outputs.KUBECTL_MAJOR_MINOR_VERSION }}
run: docker build ./ubuntu-2204 --build-arg KUBECTL_MAJOR_MINOR_VERSION=$KUBECTL_MAJOR_MINOR_VERSION --tag octopuslabs/k8s-workertools:$VERSION_NUMBER-ubuntu.2204 --tag octopuslabs/k8s-workertools:latest-ubuntu.2204
if: ${{ needs.get-version-number.outputs.CONTINUE == 'Yes' }}
- name: Push the ubuntu.2204 version-specific image
env:
VERSION_NUMBER: ${{ needs.get-version-number.outputs.VERSION }}
run: docker push octopuslabs/k8s-workertools:$VERSION_NUMBER-ubuntu.2204
if: ${{ needs.get-version-number.outputs.CONTINUE == 'Yes' }}
- name: Push the ubuntu.2204 latest image
run: docker push octopuslabs/k8s-workertools:latest-ubuntu.2204
if: ${{ needs.get-version-number.outputs.CONTINUE == 'Yes' }}
build-win-2019:
needs: [get-version-number]
runs-on: windows-2019
steps:
- uses: actions/checkout@v3
- name: DockerHub Login
env:
USERNAME: ${{ secrets.DOCKER_HUB_USER }}
PASSWORD: ${{ secrets.DOCKER_HUB_PAT }}
run: docker login --username ${env:USERNAME} --password "${env:PASSWORD}"
if: ${{ needs.get-version-number.outputs.CONTINUE == 'Yes' }}
- name: Build the win2019 image
env:
VERSION_NUMBER: ${{ needs.get-version-number.outputs.VERSION }}
GCLOUD_CLI_VERSION: ${{ needs.get-version-number.outputs.GCLOUD_CLI_VERSION }}
run: docker build ./windows-2019 --build-arg GCLOUD_CLI_VERSION=${env:GCLOUD_CLI_VERSION} --tag octopuslabs/k8s-workertools:${env:VERSION_NUMBER}-windows.2019 --tag octopuslabs/k8s-workertools:latest-windows.2019
if: ${{ needs.get-version-number.outputs.CONTINUE == 'Yes' }}
- name: Push the win2019 version-specific image
env:
VERSION_NUMBER: ${{ needs.get-version-number.outputs.VERSION }}
run: docker push octopuslabs/k8s-workertools:${env:VERSION_NUMBER}-windows.2019
if: ${{ needs.get-version-number.outputs.CONTINUE == 'Yes' }}
- name: Push the win2019 latest image
run: docker push octopuslabs/k8s-workertools:latest-windows.2019
if: ${{ needs.get-version-number.outputs.CONTINUE == 'Yes' }}
build-win-2022:
needs: [get-version-number]
runs-on: windows-2022
steps:
- uses: actions/checkout@v3
- name: DockerHub Login
env:
USERNAME: ${{ secrets.DOCKER_HUB_USER }}
PASSWORD: ${{ secrets.DOCKER_HUB_PAT }}
run: docker login --username ${env:USERNAME} --password "${env:PASSWORD}"
if: ${{ needs.get-version-number.outputs.CONTINUE == 'Yes' }}
- name: Build the win2022 image
env:
VERSION_NUMBER: ${{ needs.get-version-number.outputs.VERSION }}
GCLOUD_CLI_VERSION: ${{ needs.get-version-number.outputs.GCLOUD_CLI_VERSION }}
run: docker build ./windows-2022 --build-arg GCLOUD_CLI_VERSION=${env:GCLOUD_CLI_VERSION} --tag octopuslabs/k8s-workertools:${env:VERSION_NUMBER}-windows.2022 --tag octopuslabs/k8s-workertools:latest-windows.2022
if: ${{ needs.get-version-number.outputs.CONTINUE == 'Yes' }}
- name: Push the win2022 version-specific image
env:
VERSION_NUMBER: ${{ needs.get-version-number.outputs.VERSION }}
run: docker push octopuslabs/k8s-workertools:${env:VERSION_NUMBER}-windows.2022
if: ${{ needs.get-version-number.outputs.CONTINUE == 'Yes' }}
- name: Push the win2022 latest image
run: docker push octopuslabs/k8s-workertools:latest-windows.2022
if: ${{ needs.get-version-number.outputs.CONTINUE == 'Yes' }}
build-docker-manifest:
needs: [get-version-number, build-ubuntu, build-win-2019, build-win-2022]
runs-on: ubuntu-latest
steps:
- name: DockerHub Login
env:
USERNAME: ${{ secrets.DOCKER_HUB_USER }}
PASSWORD: ${{ secrets.DOCKER_HUB_PAT }}
run: docker login --username $USERNAME --password "$PASSWORD"
if: ${{ needs.get-version-number.outputs.CONTINUE == 'Yes' }}
- name: Create docker manifest for latest tag
run: docker manifest create octopuslabs/k8s-workertools:latest octopuslabs/k8s-workertools:latest-windows.2019 octopuslabs/k8s-workertools:latest-windows.2022 octopuslabs/k8s-workertools:latest-ubuntu.2204
if: ${{ needs.get-version-number.outputs.CONTINUE == 'Yes' }}
- name: Annotate docker manifest for latest tag
env:
WIN2019_VERSION: ${{ needs.get-version-number.outputs.WIN2019_VERSION }}
WIN2022_VERSION: ${{ needs.get-version-number.outputs.WIN2022_VERSION }}
run: |
docker manifest annotate --os "windows" --os-version "$WIN2019_VERSION" --arch "amd64" "octopuslabs/k8s-workertools:latest" "octopuslabs/k8s-workertools:latest-windows.2019" && \
docker manifest annotate --os "windows" --os-version "$WIN2022_VERSION" --arch "amd64" "octopuslabs/k8s-workertools:latest" "octopuslabs/k8s-workertools:latest-windows.2022"
if: ${{ needs.get-version-number.outputs.CONTINUE == 'Yes' }}
- name: Push docker manifest for latest tag
run: docker manifest push octopuslabs/k8s-workertools:latest
if: ${{ needs.get-version-number.outputs.CONTINUE == 'Yes' }}
- name: Create docker manifest for version-specific tag
env:
VERSION_NUMBER: ${{ needs.get-version-number.outputs.VERSION }}
run: docker manifest create octopuslabs/k8s-workertools:$VERSION_NUMBER octopuslabs/k8s-workertools:$VERSION_NUMBER-windows.2019 octopuslabs/k8s-workertools:$VERSION_NUMBER-windows.2022 octopuslabs/k8s-workertools:$VERSION_NUMBER-ubuntu.2204
if: ${{ needs.get-version-number.outputs.CONTINUE == 'Yes' }}
- name: Annotate docker manifest for version-specific tag
env:
VERSION_NUMBER: ${{ needs.get-version-number.outputs.VERSION }}
WIN2019_VERSION: ${{ needs.get-version-number.outputs.WIN2019_VERSION }}
WIN2022_VERSION: ${{ needs.get-version-number.outputs.WIN2022_VERSION }}
run: |
docker manifest annotate --os "windows" --os-version "$WIN2019_VERSION" --arch "amd64" "octopuslabs/k8s-workertools:$VERSION_NUMBER" "octopuslabs/k8s-workertools:$VERSION_NUMBER-windows.2019" && \
docker manifest annotate --os "windows" --os-version "$WIN2022_VERSION" --arch "amd64" "octopuslabs/k8s-workertools:$VERSION_NUMBER" "octopuslabs/k8s-workertools:$VERSION_NUMBER-windows.2022"
if: ${{ needs.get-version-number.outputs.CONTINUE == 'Yes' }}
- name: Push docker manifest for version-specific tag
env:
VERSION_NUMBER: ${{ needs.get-version-number.outputs.VERSION }}
run: docker manifest push octopuslabs/k8s-workertools:$VERSION_NUMBER
if: ${{ needs.get-version-number.outputs.CONTINUE == 'Yes' }}