forked from Sitecore/docker-images
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuild.ps1
139 lines (108 loc) · 4.48 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
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
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)]
[string]$VersionsFilter = "*",
[Parameter(Mandatory = $true)]
[ValidateScript( {Test-Path $_ -PathType 'Container'})]
[string]$InstallSourcePath,
[Parameter(Mandatory = $false)]
[string]$Organization,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$Repository,
[Parameter(Mandatory = $false)]
[ValidateSet('Always', 'IfChanged', 'Never')]
[string]$PushMode = 'Never',
[Parameter(Mandatory = $false)]
[switch]$RemoveInstallationSourceFiles
)
Import-Module .\build-support.psm1 -Force
function Find-ExternalBaseImages {
[cmdletbinding()]
param(
[object[]]$Images,
[Parameter(Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$Repository # Can include Organization if needed: <Organization>/<Repository> | <Repository>
)
$externalImages = @{}
Foreach ($image in $Images) {
if ($image.BaseImages) {
Foreach ($baseImage in $image.BaseImages) {
if (-not $externalImages.ContainsKey($baseImage) -And -not $baseImage.StartsWith($Repository, "CurrentCultureIgnoreCase")) {
$externalImages.add($baseImage, $baseImage)
}
}
}
}
$externalImages.Values
}
$ErrorActionPreference = "STOP"
$imagesPath = (Join-Path $PSScriptRoot "\sitecore")
if (![string]::IsNullOrEmpty($Organization) -and -not $Repository.Contains('/')) {
$Repository = "{0}/{1}" -f $Organization, $Repository
}
$images = Scan-ImageBuildFolders -Path $imagesPath -InstallSourcePath $InstallSourcePath -Filter $VersionsFilter -Repository $Repository
$images = Sort-Images -Images $images
# Pull latest bases images
Find-ExternalBaseImages -Images $images -Repository $Repository| ForEach-Object {
$baseImage = $_
Write-Host ("Pulling latest base image '{0}'..." -f $baseImage)
docker pull $baseImage
$LASTEXITCODE -ne 0 | Where-Object { $_ } | ForEach-Object { throw ("Pulling '{0}' failed" -f $baseImage) }
}
$images | ForEach-Object {
$image = $_
$tag = $image.FullName
# Save the digest of previous builds for later comparison
$previousDigest = $null
if ((docker image ls $tag --quiet)) {
$previousDigest = (docker image inspect $tag) | ConvertFrom-Json | ForEach-Object { $_.Id }
}
Write-Host ("Building '{0}'..." -f $tag)
# Copy any missing source files into build context
$image.Sources | ForEach-Object {
$sourcePath = $_
$sourceItem = Get-Item -Path $sourcePath
$targetPath = Join-Path $image.Path $sourceItem.Name
if (!(Test-Path -Path $targetPath)) {
Copy-Item $sourceItem -Destination $targetPath -Verbose:$VerbosePreference
}
}
if ((docker image ls $tag --quiet)) {
$previousDigest = (docker image inspect $tag) | ConvertFrom-Json | ForEach-Object { $_.Id }
}
# Build image
$exp = 'docker image build --isolation "hyperv"'
if ($image.Memory) {
$exp = $exp + ' --memory ' + $image.Memory
}
if ($image.Args) {
$image.Args.PSobject.Properties | ForEach-Object {
$exp = $exp + ' --build-arg "' + $_.name + '=' + $_.value + '"'
}
}
$exp = $exp + ' --tag ' + $tag
$exp = $exp + ' "' + $image.Path + '"'
Write-Host $exp
Invoke-Expression $exp
$LASTEXITCODE -ne 0 | Where-Object { $_ } | ForEach-Object { throw ("Build of '{0}' failed" -f $tag) }
if ($RemoveInstallationSourceFiles) {
Write-Host ("Done with Installation Source - Removing '{0}'" -f $targetPath) -ForegroundColor Green
Remove-Item $targetPath -Force
}
if ($PushMode -eq 'Never') {
Write-Warning "Done. PushMode is set to 'Never' therefore the image is not pushed to the remote repository."
return
}
# Determine if we need to push
$currentDigest = (docker image inspect $tag) | ConvertFrom-Json | ForEach-Object { $_.Id }
if (($PushMode -eq 'IfChanged') -And ($currentDigest -eq $previousDigest)) {
Write-Host "Done. PushMode is set to 'IfChanged' and the image has not changed since last build, therefore is not pushed to the remote repository." -ForegroundColor Green
return
}
# Push image
docker image push $tag
$LASTEXITCODE -ne 0 | Where-Object { $_ } | ForEach-Object { throw ("Push of '{0}' failed" -f $tag) }
Write-Host ("Image '{0}' pushed." -f $tag) -ForegroundColor Green
}