-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-dockerimages.ps1
125 lines (106 loc) · 3.96 KB
/
build-dockerimages.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
[CmdletBinding()]
Param
(
[Parameter(Mandatory = $false)][string]$branch = "local",
[Parameter(Mandatory = $false)][string]$target = "develop",
[Parameter(Mandatory = $false)][string]$commit = "",
[Parameter(Mandatory = $false)][string]$pullRequestId = "",
[Parameter(Mandatory = $false)][string]$buildCounter = "0",
[Parameter(Mandatory = $false)][ValidateSet("true", "false")][string]$local = "true",
[Parameter(Mandatory = $false)][switch]$systemprune,
[Parameter(Mandatory = $false)][switch]$pushImage
)
$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';
# common repository functions
Import-Module .\repository.psm1 -Force
Function Get-Result {
if ($LastExitCode -ne 0) {
$text = "ERROR: Exiting with error code $LastExitCode"
Write-Error "##teamcity[buildStatus status='$text']"
if (-not ($local -eq 'true')) { [System.Environment]::Exit(1) }
}
return $true
}
Function Invoke-Exe {
Param(
[parameter(Mandatory = $true)][string] $cmd,
[parameter(Mandatory = $true)][string] $args
)
Write-Host "Executing: `"$cmd`" $args"
Invoke-Expression "& `"$cmd`" $args"
$result = Get-Result
}
Function Get-BranchTag {
[OutputType([string])]
param(
[Parameter(Mandatory = $true)]
[string]$branchName
)
$tagPart = $branchName
# if ($branchName -eq "master") {
# $tagPart = "master"
# } elseif ($branchName -eq "develop") {
# $tagPart = "develop"
# } else
if ($branchName -like "release/*") {
$tagPart = "release"
} elseif ($branchName -like "bugfix/*" -or $branchName -like "hotfix/*" -or $branchName -like "feature/*") {
# extract jira key, i.e. CFG-123
$tagPart = ($branchName | Select-String -Pattern '((?<!([A-Z]{1,10})-?)[A-Z]+-\d+)' | % matches).value
}
# else {
# $tagPart = $branchName
# }
$tagPart
}
Function New-BuildJson {
Param (
[Parameter(Mandatory = $true)][string]$versionjsonpath,
[Parameter(Mandatory = $true)][string]$buildjsonpath,
[Parameter(Mandatory = $true)][string]$buildCounter
)
$version = Get-Content $versionjsonpath -raw | ConvertFrom-Json
$buildobject = New-Object -TypeName psobject
$build = New-Object -TypeName psobject
$builditems = [ordered] @{
"version" = ""
"timestamp" = ""
"tag" = ""
"suffix" = ""
}
$NewBuildVersion = "$($version.version).$buildCounter"
$buildTime = (Get-Date).ToUniversalTime().ToString("u")
$builditems.version = $NewBuildVersion
$builditems.timestamp = $buildTime
$builditems.Keys | % { $build | Add-Member -MemberType NoteProperty -Name $_ -Value $builditems.$_ } > $null
$buildobject | Add-Member -MemberType NoteProperty -Name Build -Value $build
$buildobject | ConvertTo-Json -Depth 5 | Out-File $buildjsonpath -force
return $buildobject
}
if ($systemprune.IsPresent) {
Invoke-Exe -cmd docker -args "system prune --force"
}
$config = Get-RepositoryConfiguration
$BuildNumber = (New-BuildJson -versionJsonPath $PSScriptRoot\repository.json -BuildJsonPath $PSScriptRoot\src\$($config.build.publishableProject)\build.json -buildCounter $buildCounter).build.version
$dockerpath = "Dockerfile.*"
$dockercontext = "."
Write-Output $version
Write-Output "buildNumber: $buildNumber"
Write-Output "branch: $branch"
Write-Output "dockerpath: $dockerpath"
Write-Output "dockercontext: $dockercontext"
Write-Output "buildconfiguration: $buildconfiguration"
Write-Output "nugetfeed: $($config.nuget.feed)"
Write-Output "buildimage=$($config.docker.buildimage)"
Write-Output "runtimeimage=$($config.docker.runtimeimage)"
Write-Output "image:$($config.docker.image)"
#Run Build for all Dockerfiles in /Docker path
$dockerFiles = Get-ChildItem -Path $dockercontext -Filter $dockerpath -Recurse
foreach ($dockerfile in $dockerFiles) {
#Docker build and tag
$branchTag = Get-BranchTag -branchName $branch
$dockerFileName = $dockerfile.name
$HostOS = $dockerFileName.split(".").split()[-1]
Write-Output "Building $dockerFileName"
$imageversion = "$buildNumber-$branchTag-$HostOS"
$analysisArgs = "$($config.sonar.propertyPrefix)sonar.scm.disabled=true";