-
Notifications
You must be signed in to change notification settings - Fork 1
/
update.ps1
185 lines (164 loc) · 7.02 KB
/
update.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
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
<#
.SYNOPSIS
Updates this official Docker repository with the correct version.
.DESCRIPTION
This script's algorithm uses the existence or not of a folder based on the
version provided in order to create a new version directory or update an
existing one.
.PARAMETER ToVersion
The version to which the repository should be updated.
.PARAMETER TemplatesPath
Path to the template folder. This is concretely the docker folder in
WorkflowGen's source repository.
#>
#requires -Version 5.1
[CmdletBinding()]
param (
[Parameter(Mandatory=$true, Position=0)]
[ValidateNotNullOrEmpty()]
[ValidateScript({ $_ -match "^[0-9]+\.[0-9]+\.[0-9]+$" })]
[string]$ToVersion,
[Parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[string]$TemplatesPath
)
Import-Module powershell-yaml
if ($TemplatesPath) {
$TemplatesPath = Resolve-Path $TemplatesPath
}
$pipelinesDefPath = Join-Path $PSScriptRoot "azure-pipelines.yml"
$minorVersion = $ToVersion.Substring(0, $ToVersion.LastIndexOf("."))
$majorVersion = $minorVersion.Substring(0, $minorVersion.LastIndexOf("."))
$minorVersionRegex = $minorVersion -replace "\.", "\."
$matrix = @{
WFGEN_VERSION_FOLDER = $minorVersion
WFGEN_VERSION = $ToVersion
}
$repoHasVersion = (Get-ChildItem $PSScriptRoot -Directory `
| Where-Object Name -eq $minorVersion `
| Measure-Object `
| ForEach-Object Count) -as [bool]
if ($repoHasVersion) {
Join-Path $PSScriptRoot $minorVersion `
| Get-ChildItem -Recurse -File `
| Where-Object Name -eq "Dockerfile" `
| ForEach-Object {
$content = Get-Content $_.FullName -Encoding UTF8
if ($_.FullName -like "*\onbuild\*") {
$content = $content -replace "(?<=advantys/workflowgen:)$minorVersionRegex\.[0-9]+(?=-win-[ltsc2016|ltsc2019])", $ToVersion
} else {
$content = $content -replace "$minorVersionRegex\.[0-9]+(?=/manual.zip)", $ToVersion
$content = $content -replace "(?<=WFGEN_VERSION=)[^\s]+", $ToVersion
}
Set-Content -Path $_.FullName -Value $content -Encoding UTF8
}
$pipelinesDef = Get-Content $pipelinesDefPath -Raw -Encoding UTF8 | ConvertFrom-Yaml -Ordered
[array]$newJobs = $pipelinesDef.jobs | ForEach-Object {
$currentMatrix = $_.strategy.matrix[$minorVersion]
$currentMatrix.WFGEN_VERSION = $ToVersion
if ($currentMatrix.ADDITIONAL_TAGS) {
$currentMatrix.ADDITIONAL_TAGS = $currentMatrix.ADDITIONAL_TAGS.Split(",") `
| ForEach-Object { $_.Trim() } `
| ForEach-Object {
if ($_ -match "$minorVersionRegex\.[0-9]+") {
return $ToVersion
}
return $_
} `
| ForEach-Object -Begin { $acc = "" } -Process {
if (-not $acc) {
$acc = $_
} else {
$acc = "$acc, $_"
}
} -End { $acc }
}
$_.strategy.matrix[$minorVersion] = $currentMatrix
return $_
}
$pipelinesDef.jobs = $newJobs
$pipelinesDef | ConvertTo-Yaml -OutFile $pipelinesDefPath -Force
} else {
if (-not $TemplatesPath) {
throw [ArgumentException]::new("Parameter TemplatesPath needs to be populated.")
}
$onbuildDockerfileTemplatePath = [io.path]::Combine($TemplatesPath, "workflowgen", "Dockerfile.onbuild.template")
$filesToCopy = @(
(Join-Path $TemplatesPath "Auth.psm1"),
(Join-Path $TemplatesPath "Const.psm1"),
(Join-Path $TemplatesPath "Utils.psm1"),
([io.path]::Combine($TemplatesPath, "workflowgen", "set-state.ps1")),
([io.path]::Combine($TemplatesPath, "workflowgen", "healthcheck.ps1")),
([io.path]::Combine($TemplatesPath, "workflowgen", "monitor-services.ps1")),
([io.path]::Combine($TemplatesPath, "workflowgen", "docker-entrypoint.ps1")),
([io.path]::Combine($TemplatesPath, "workflowgen", "Dockerfile.template"))
)
,"ltsc2019" `
| ForEach-Object {
$path = [io.path]::Combine($PSScriptRoot, $minorVersion, "windows", "windowsservercore-$_")
[pscustomobject]@{
Path = $path
OnbuildPath = (Join-Path $path "onbuild")
}
} `
| ForEach-Object {
New-Item $_.OnbuildPath -ItemType Directory -Force | Out-Null
Copy-Item $filesToCopy $_.Path
Copy-Item $onbuildDockerfileTemplatePath $_.OnbuildPath
Join-Path $_.Path "Dockerfile.template" | Rename-Item -NewName "Dockerfile"
Join-Path $_.OnbuildPath "Dockerfile.onbuild.template" | Rename-Item -NewName "Dockerfile"
}
Join-Path $PSScriptRoot $minorVersion `
| Get-ChildItem -Recurse -File `
| Where-Object Name -eq "Dockerfile" `
| ForEach-Object {
$content = Get-Content $_.FullName -Encoding UTF8
$content = $content -replace "#{WFGEN_VERSION}#", $ToVersion
# Add more conditions when adding more supported versions
if ($_.FullName -like "*ltsc2019*") {
$content = $content -replace "#{WINDOWS_SERVER_VERSION}#", "ltsc2019"
}
Set-Content -Path $_.FullName -Value $content -Encoding UTF8
}
$pipelinesDef = Get-Content $pipelinesDefPath -Raw -Encoding UTF8 | ConvertFrom-Yaml -Ordered
[array]$newJobs = $pipelinesDef.jobs | ForEach-Object {
$newMatrix = @{
WFGEN_VERSION = $ToVersion
WFGEN_VERSION_FOLDER = $minorVersion
WINDOWS_SERVER_VERSION = $(switch ($_.job) {
"Buildltsc2019" { "ltsc2019" }
})
}
$newMatrixLatest = $newMatrix + @{
ADDITIONAL_TAGS = "latest, $majorVersion, $minorVersion, $ToVersion"
}
$matrix = $_.strategy.matrix
$matrix.GetEnumerator() `
| Where-Object { $matrix[$_.Key].ADDITIONAL_TAGS } `
| ForEach-Object {
$matrix[$_.Key].ADDITIONAL_TAGS = $matrix[$_.Key].ADDITIONAL_TAGS.Split(",") `
| ForEach-Object { $_.Trim() } `
| ForEach-Object {
if ($_ -eq "latest" -or $_ -eq $majorVersion -or $_ -eq $minorVersion -or $_ -eq $ToVersion) {
return
}
return $_
} `
| ForEach-Object -Begin { $acc = "" } -Process {
if (-not $acc) {
$acc = $_
} else {
$acc = "$acc, $_"
}
} -End { $acc }
}
$_.strategy.matrix = $matrix
$_.strategy.matrix[$minorVersion] = switch ($_.job) {
"Buildltsc2019" { $newMatrixLatest }
default { $newMatrix }
}
return $_
}
$pipelinesDef.jobs = $newJobs
$pipelinesDef | ConvertTo-Yaml -OutFile $pipelinesDefPath -Force
}