forked from SenexCrenshaw/StreamMaster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_docker.ps1
86 lines (69 loc) · 2.21 KB
/
build_docker.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
param (
[switch]$PrintOnly,
[switch]$BuildProd,
[switch]$DebugLog
)
$gitVersion = "dotnet-gitversion"
&$gitVersion /updateAssemblyInfo | Out-Null
$json = &$gitVersion /output json | Out-String
$obj = $json | ConvertFrom-Json
$semVer = $obj.SemVer
$buildMetaDataPadded = $obj.AssemblySemVer
$branchName = $obj.BranchName
if ($PrintOnly -or $DebugLog) {
Write-Output $obj
}
$env:DOCKER_BUILDKIT = 1
$env:COMPOSE_DOCKER_CLI_BUILD = 1
# Multiple tags
$tags = if ($BuildProd) {
"docker.io/senexcrenshaw/streammaster:latest",
"docker.io/senexcrenshaw/streammaster:$semVer",
"docker.io/senexcrenshaw/streammaster:$buildMetaDataPadded"
}
else {
"docker.io/senexcrenshaw/streammaster:$branchName-$semVer-$buildMetaDataPadded"
}
Write-Output "Tags to be used:"
$tags | ForEach-Object { Write-Output $_ }
if ($PrintOnly -or $DebugLog) {
$buildCommand = "docker buildx build --platform ""linux/amd64,linux/arm64"" -f ./Dockerfile . --push " + ($tags | ForEach-Object { "--tag=$_" })
Write-Output "Build Command: $buildCommand"
}
if ($PrintOnly) {
Write-Output "PrintOnly flag is set. Exiting without building."
exit
}
# Show the build command if either PrintOnly or DebugLog is set
# Capture the start time
$startTime = Get-Date
# Initialize line counter
$lineCounter = 0
# Prefix for the dots
Write-Host -NoNewline "Building Image "
# Skip build process if PrintOnly flag is set
if ($PrintOnly) {
Write-Output "PrintOnly flag is set. Exiting without building."
exit
}
# Run the build and push operation, displaying a dot for every 10 lines of output
try {
docker buildx build --platform "linux/amd64,linux/arm64" -f ./Dockerfile . --push $(foreach ($tag in $tags) { "--tag=$tag" }) 2>&1 | ForEach-Object {
if ($DebugLog) {
$_ # Output the line for logging purposes if DebugLog flag is set
}
$lineCounter++
if ($lineCounter % 10 -eq 0) {
Write-Host -NoNewline "."
}
}
}
catch {
Write-Error "Docker build failed with error: $_"
exit 1
}
# Capture the end time
$endTime = Get-Date
# Calculate the total time taken
$overallTime = $endTime - $startTime
Write-Output "`nOverall time taken: $($overallTime.TotalSeconds) seconds"