-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbuild.ps1
85 lines (72 loc) · 2.46 KB
/
build.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
78
79
80
81
82
83
84
85
$ErrorActionPreference = 'Stop'
#Set-PSDebug -Trace 1
if (-Not (Test-Path -Path Env:\DOCKER_USER)) {
Write-Error 'Missing environment variable DOCKER_USER'
}
if (-Not (Test-Path -Path Env:\DOCKER_PASS)) {
Write-Error 'Missing environment variable DOCKER_PASS'
}
#region Import registry definition
Import-LocalizedData -BaseDirectory "$PSScriptRoot" -FileName registry.psd1 -BindingVariable Registry
'Imported registry definition'
$Registry
#endregion
#region Determine which images to build
#$Files = Get-ChildItem -Path "$PSScriptRoot" -File -Recurse -Filter 'image.psd1'
$Files = & git diff --name-only HEAD~1 | ForEach-Object { Get-Item -Path $_ }
if ($Files.Length -eq 0) {
return
}
#endregion
#region Load image definitions
$Images = @{}
foreach ($File in $Files) {
$Images[$File.Directory.BaseName] = Import-LocalizedData -BaseDirectory $File.Directory -FileName image.psd1
$Images[$File.Directory.BaseName].DirectoryName = $File.DirectoryName
}
#endregion
#region Login to Docker Hub
docker login -u $env:DOCKER_USER -p $env:DOCKER_PASS
#endregion
#region Enumerate images
while ($Images.Count -gt 0) {
$ImageName = $Images.Keys | Get-Random
$DependsOn = $Images[$ImageName].DependsOn
"--- ImageName=$ImageName DependsOn=$DependsOn"
If ($DependsOn.Length -gt 0 -and $Images.ContainsKey($DependsOn)) {
' Dependent image must be built first'
} else {
$Image = $Images[$ImageName]
#region Build image
$param = @("build", "-t", "$($Image.Name):$($Image.Version)", "$($Image.DirectoryName)")
"docker $($param -join ' ')"
& docker @param
#endregion
#region Build public image name
$Repository = $Registry.User
if ($Registry.Host) {
$Repository = "$($Registry.Host)/$Repository"
}
#endregion
#region Tag image with public name(s)
foreach ($Tag in $Image.Tags) {
$param = @("tag", "$($Image.Name):$($Image.Version)", "$Repository/$($Image.Name):$Tag")
"docker $($param -join ' ')"
& docker @param
}
#endregion
#region Push image with public name(s)
foreach ($Tag in $Image.Tags) {
$param = @("push", "$Repository/$($Image.Name):$Tag")
"docker $($param -join ' ')"
& docker @param
}
#endregion
#endregion
$Images.Remove($ImageName)
}
}
#endregion
#region Logout of Docker Hub
docker logout
#endregion