-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdocker-build.ps1
164 lines (139 loc) · 7.43 KB
/
docker-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
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
[CmdletBinding()]
param (
[Parameter(Mandatory = $false)] [string] $Version, # = '2022.04.5-20240327-01',
[Parameter(Mandatory = $false)] [string] $SourceImageTag = '2022.04.7-linux',
[Parameter(Mandatory = $false)] [string[]] $DockerRepository = @('teslaconsulting/teamcity-agent'),
[Parameter(Mandatory = $false)] [string] $Branch,
[Parameter(Mandatory = $false)] [string] $Sha,
[Parameter(Mandatory = $false)] [string] $DotnetSdkVersion6Tag = '6.0-focal',
[Parameter(Mandatory = $false)] [string] $DotnetSdkVersion7Tag = '7.0-jammy',
[Parameter(Mandatory = $false)] [string] $DotnetSdkVersion8Tag = '8.0-jammy',
[Parameter(Mandatory = $false)] [string] $DotnetSdkVersion9Tag = '9.0-preview-noble',
[Parameter(Mandatory = $false)] [switch] $NoSquash,
[Parameter(Mandatory = $false)] [switch] $Latest,
[Parameter(Mandatory = $false)] [switch] $WhatIf
)
function AddImageTag($imageTag) {
if ($imageTag -And !($script:imageTags -Contains $imageTag)) {
$script:imageTags += @($imageTag)
}
}
function private:AddTag($pattern, $replacement) {
if (!$replacement) { $replacement = '${1}' }
if ($pattern -And $Version -match $pattern) {
$tag = $Version -replace $pattern, $replacement
if ($tag) {
AddImageTag $tag
}
}
}
function private:AddDockerImage($image) {
if ($image -And !($script:dockerImages -Contains $image)) {
$script:dockerImages += @($image)
}
}
function private:AddBuildArg($buildArgKey, $buildArgValue, $valuePattern) {
if ($buildArgValue -And (!$valuePattern -Or $buildArgValue -match $valuePattern)) {
$script:params += @('--build-arg', "$($buildArgKey)=$($buildArgValue)")
}
}
$root = Split-Path $MyInvocation.MyCommand.Path -Parent -Resolve
$imageTags = @()
private:AddImageTag "sha-$($Sha)"
private:AddImageTag $Branch
if (!$Version) {
$Version = ($SourceImageTag.EndsWith('-linux') ? $SourceImageTag.Substring(0, $SourceImageTag.Length - 5) : $SourceImageTag) + (Get-Date).ToString('yyyyMMdd-HHmmss')
}
#Write-Host "Version: $Version"
AddTag '^(\d+)\.\d+(\.\d+)?(-.+)?$'
AddTag '^(\d+\.\d+)(\.\d+)?(-.+)?$'
AddTag '^(\d+\.\d+(\.\d+)?)(-.+)?$'
AddTag '^(\d+\.\d+(\.\d+)?(-.+)?)$'
if ($Latest) {
private:AddImageTag 'latest'
}
$dockerImages = @()
foreach ($dockerRepos in $dockerRepository) {
foreach ($imageTag in $imageTags) {
private:AddDockerImage "$($dockerRepos):$($imageTag)"
}
}
$params = @('build', "$($root)/teamcity-agent")
$params += @('--pull', '--progress=plain')
if (!$NoSquash) {
$params += @('--squash')
}
private:AddBuildArg 'TEAMCITYAGENT_IMAGE_TAG' $SourceImageTag
Write-Output "Docker images: $dockerImages"
foreach ($dockerImage in $dockerImages) {
$params += @("--tag=$($dockerImage)")
}
#===========================================================
$minver_help = $(docker run --rm teslaconsulting/minver-cli:latest minver --help)
Write-Output "Minver help:`n$minver_help"
$minver_version = $minver_help[0] -Replace '^[^0-9]+([0-9.]+)[^0-9].*$', '$1'
Write-Output "Minver version: $minver_version"
private:AddBuildArg 'MINVER_VERSION' $minver_version
$docker_compose_version = $(docker run --rm docker:cli docker compose version)
Write-Output "Docker compose version raw: $docker_compose_version"
$docker_compose_version = $docker_compose_version.Substring('Docker Compose version v'.Length)
Write-Output "Docker compose version only: '$docker_compose_version'"
private:AddBuildArg 'DOCKER_COMPOSE_VERSION' $docker_compose_version
#$dotnet_info_raw = $(docker run --rm mcr.microsoft.com/dotnet/sdk:3.1-focal dotnet --info) -join ' '
#$dotnet_sdk_version = $dotnet_info_raw -replace '^.+\.NET Core SDKs installed:[^0-9]+([0-9.]+)[^0-9].*$', '$1'
#$aspnetcore_version = $dotnet_info_raw -replace '^.+Microsoft.AspNetCore.App\s+([0-9.]+)[^0-9.].*$', '$1'
#$dotnet_version = $dotnet_info_raw -replace '^.+Microsoft.NETCore.App\s+([0-9.]+)[^0-9.].*$', '$1'
#Write-Output ".NETCore 3.1: Version SDK:$dotnet_sdk_version ASP.NET:$aspnetcore_version .NETCore:$dotnet_version"
#private:AddBuildArg 'DOTNET_SDK_VERSION31' $dotnet_sdk_version
#private:AddBuildArg 'ASPNET_VERSION31' $aspnetcore_version
#private:AddBuildArg 'DOTNET_VERSION31' $dotnet_version
#$dotnet_vers = $(docker run --rm mcr.microsoft.com/dotnet/sdk:5.0-focal sh -c 'echo $DOTNET_SDK_VERSION;echo $ASPNET_VERSION;echo $DOTNET_VERSION')
#Write-Output ".NET 5.0: Version SDK:$($dotnet_vers[0]) ASP.NET:$($dotnet_vers[1]) .NETCore:$($dotnet_vers[2])"
#private:AddBuildArg 'DOTNET_SDK_VERSION5' $dotnet_vers[0]
#private:AddBuildArg 'ASPNET_VERSION5' $dotnet_vers[1]
#private:AddBuildArg 'DOTNET_VERSION5' $dotnet_vers[2]
$dotnet_vers = $(docker run --rm mcr.microsoft.com/dotnet/sdk:$DotnetSdkVersion6Tag sh -c 'echo $DOTNET_SDK_VERSION;echo $ASPNET_VERSION;echo $DOTNET_VERSION')
Write-Output ".NET 6.0: Version SDK:$($dotnet_vers[0]) ASP.NET:$($dotnet_vers[1]) .NETCore:$($dotnet_vers[2])"
private:AddBuildArg 'DOTNET_SDK_VERSION6' $dotnet_vers[0]
private:AddBuildArg 'ASPNET_VERSION6' $dotnet_vers[1]
private:AddBuildArg 'DOTNET_VERSION6' $dotnet_vers[2]
$dotnet_vers = $(docker run --rm mcr.microsoft.com/dotnet/sdk:$DotnetSdkVersion7Tag sh -c 'echo $DOTNET_SDK_VERSION;echo $ASPNET_VERSION;echo $DOTNET_VERSION')
Write-Output ".NET 7.0: Version SDK:$($dotnet_vers[0]) ASP.NET:$($dotnet_vers[1]) .NETCore:$($dotnet_vers[2])"
private:AddBuildArg 'DOTNET_SDK_VERSION7' $dotnet_vers[0]
private:AddBuildArg 'ASPNET_VERSION7' $dotnet_vers[1]
private:AddBuildArg 'DOTNET_VERSION7' $dotnet_vers[2]
$dotnet_vers = $(docker run --rm mcr.microsoft.com/dotnet/sdk:$DotnetSdkVersion8Tag sh -c 'echo $DOTNET_SDK_VERSION;echo $ASPNET_VERSION;echo $DOTNET_VERSION;pwsh --version')
Write-Output ".NET 8.0: Version SDK:$($dotnet_vers[0]) ASP.NET:$($dotnet_vers[1]) .NETCore:$($dotnet_vers[2]) PowerShell:$($dotnet_vers[3].SubString(11))"
private:AddBuildArg 'DOTNET_SDK_VERSION8' $dotnet_vers[0]
private:AddBuildArg 'ASPNET_VERSION8' $dotnet_vers[1]
private:AddBuildArg 'DOTNET_VERSION8' $dotnet_vers[2]
$dotnet_vers = $(docker run --rm mcr.microsoft.com/dotnet/sdk:$DotnetSdkVersion9Tag sh -c 'echo $DOTNET_SDK_VERSION;echo $ASPNET_VERSION;echo $DOTNET_VERSION;pwsh --version')
Write-Output ".NET 9.0: Version SDK:$($dotnet_vers[0]) ASP.NET:$($dotnet_vers[1]) .NETCore:$($dotnet_vers[2]) PowerShell:$($dotnet_vers[3].SubString(11))"
private:AddBuildArg 'DOTNET_SDK_VERSION9' $dotnet_vers[0]
private:AddBuildArg 'ASPNET_VERSION9' $dotnet_vers[1]
private:AddBuildArg 'DOTNET_VERSION9' $dotnet_vers[2]
private:AddBuildArg 'POWERSHELL_VERSION' $dotnet_vers[3].SubString(11)
private:AddBuildArg 'POWERSHELL_DISTRIBUTION_CHANNEL' 'PSDocker-DotnetSDK-Ubuntu-20.04'
private:AddBuildArg 'DOTNET_SDK_VERSION6_TAG' $DotnetSdkVersion6Tag
private:AddBuildArg 'DOTNET_SDK_VERSION7_TAG' $DotnetSdkVersion7Tag
private:AddBuildArg 'DOTNET_SDK_VERSION8_TAG' $DotnetSdkVersion8Tag
private:AddBuildArg 'DOTNET_SDK_VERSION9_TAG' $DotnetSdkVersion9Tag
#===========================================================
Write-Verbose "Execute: docker $params"
docker @params
if (!$?) {
$saveLASTEXITCODE = $LASTEXITCODE
Write-Error "docker build failed (exit=$saveLASTEXITCODE)"
exit $saveLASTEXITCODE
}
if (!$WhatIf -And $dockerImages) {
Write-Host "Pushing docker images"
foreach ($dockerImage in $dockerImages) {
docker push $dockerImage
if (!$?) {
$saveLASTEXITCODE = $LASTEXITCODE
Write-Error "docker push failed (exit=$saveLASTEXITCODE)"
exit $saveLASTEXITCODE
}
}
}