-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-build.ps1
85 lines (67 loc) · 2.08 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
[CmdletBinding()]
param (
[Parameter(Mandatory = $false)] [string] $DockerContext = '',
[Parameter(Mandatory = $false)] [string] $Context = '.',
[Parameter(Mandatory = $false)] [string] $Dockerfile = '',
[Parameter(Mandatory = $false)] [string] $Tags,
[Parameter(Mandatory = $false)] [string] $Labels,
[Parameter(Mandatory = $false)] [string] $BuildArgs,
[Parameter(Mandatory = $false)] [switch] $NoPull,
[Parameter(Mandatory = $false)] [switch] $Squash,
[Parameter(Mandatory = $false)] [switch] $NoPush
)
Write-Output "Powershell Version: $($PSVersionTable.PSVersion)"
Write-Output "Tags (raw): $Tags"
Write-Output "Labels (raw): $Labels"
$tagStrs = $Tags.Trim() -split '\r|\n' | Where-Object { $_ }
$labelStrs = $Labels.Trim() -split '\r|\n' | Where-Object { $_ }
$buildArgStrs = $BuildArgs.Trim() -split '\r|\n' | Where-Object { $_ }
if (!$tagStrs) {
throw 'Tags is required'
}
[System.Array]::Reverse($tagStrs)
[string[]]$params = @()
if ($DockerContext) {
$params += @('--context', $DockerContext)
}
$params += @('build', $Context)
if (!$NoPull) {
$params += @('--pull')
}
if ($Squash -And !($Env:OS)) {
$params += @('--squash')
}
if ($Dockerfile) {
$params += @('--file', $Dockerfile)
}
if ($labelStrs) {
$params += $labelStrs | ForEach-Object { @('--label', $_) }
}
if ($tagStrs) {
$params += $tagStrs | ForEach-Object { @('--tag', $_) }
}
if ($buildArgStrs) {
$params += $buildArgStrs | ForEach-Object { @('--build-arg', $_) }
}
Write-Host "docker $params"
docker @params
if (!$?) {
exit $LASTEXITCODE
}
$firstImage = $true
if (!$NoPush -And $tagStrs) {
Write-Host "Pushing docker images"
foreach ($dockerImageTag in $tagStrs) {
if ($firstImage) {
$firstImage = $false
if ($Env:GITHUB_OUTPUT) {
Write-Output "image=$dockerImageTag" | Out-File -FilePath $Env:GITHUB_OUTPUT -Encoding utf8 -Append
}
Write-Output "Build image: $dockerImageTag"
}
docker push $dockerImageTag
if (!$?) {
exit $LASTEXITCODE
}
}
}