-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathbuild-tag-push.ps1
77 lines (66 loc) · 2.47 KB
/
build-tag-push.ps1
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
param(
[string] $imageVersion='',
[string] $registryUser='sixeyed',
[string] $localRegistry='',
[string] $buildArgs='',
[bool] $ignoreTestFailures=$false,
[object[]] $dockerConfig
)
$ErrorActionPreference = 'SilentlyContinue'
# builds and pushes Docker images
# expectations:
# - directory structure is: {image}/{os}/{os-branch}, e.g. jenkins/windowsservercore/ltsc2016
# - Dockerfile and context are in the {os-branch} directory
# - this command is executed from the {os-branch} directory
# input:
# - imageVersion - version of the image being built
# - registryUser - registry username or organization
# - localRegistry - registry push destination, in addition to Docker Hub
# - buildArgs - comma separated values to pass as `build-arg` in Docker build
# output:
# - image built and pushed with multiple tags using the format {user}/{image}:{version-{os}-{os-branch},
# - e.g.
# - sixeyed/jenkins:2.121.1-windowsservercore-ltsc2016
# - sixeyed/jenkins:windowsservercore-ltsc2016
# - sixeyed/jenkins
$path = $(pwd).ToString().Split('\')
$count = $path.Length
if (($imageVersion -eq '') -and (Test-Path ..\..\imageVersion.txt)) {
$imageVersion = Get-Content ..\..\imageVersion.txt
}
$branchName = $path[$count-1]
$osName = $path[$count-2]
$imageName = $path[$count-3]
$fullTag = "$($registryUser)/$($imageName):$($imageVersion)-$($osName)-$($branchName)"
if ($buildArgs.Length -gt 0) {
$buildArg=@()
$buildArgsExpanded=$buildArgs.Split(',')
foreach ($arg in $buildArgsExpanded){
$buildArg += "--build-arg", $arg
}
}
Write-Host "* Building image: $fullTag, with args: $buildArg"
& docker $dockerConfig image build $buildArg -t $fullTag .
if (Test-Path ..\..\test.ps1) {
Write-Host '** Executing test script'
..\..\test.ps1 -imageTag $fullTag -dockerConfig $dockerConfig
if (($LastExitCode -ne 0) -and ($ignoreTestFailures -eq $false)) {
exit 1
}
}
Write-Host "* Pushing image tags for: $fullTag"
$tags = @($fullTag,
"$($registryUser)/$($imageName):$($osName)-$($branchName)")
$registries = @('')
if ($localRegistry -ne '') {
$registries += "$($localRegistry)/"
}
foreach ($tag in $tags) {
Write-Host "** Processing $tag"
foreach ($registry in $registries){
$registryTag = "$($registry)$tag"
Write-Host "** Pushing $registryTag"
& docker $dockerConfig image tag $fullTag $registryTag
& docker $dockerConfig image push $registryTag
}
}