-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbuild-docker.ps1
More file actions
183 lines (150 loc) Β· 5.43 KB
/
Copy pathbuild-docker.ps1
File metadata and controls
183 lines (150 loc) Β· 5.43 KB
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
#!/usr/bin/env pwsh
<#
.SYNOPSIS
Builds the CodeMedic Docker container with versioning from Nerd Bank Git Versioning.
.DESCRIPTION
This script builds a Docker container for CodeMedic, automatically retrieving
the version number from NBGv2 and tagging the container appropriately.
.PARAMETER Registry
Optional Docker registry prefix (e.g., "docker.io/username" or "ghcr.io/owner")
.PARAMETER Push
If specified, pushes the built image to the registry
.PARAMETER BuildConfiguration
Build configuration (Release or Debug). Default is Release.
.EXAMPLE
.\build-docker.ps1
Builds the container locally with version tags
.EXAMPLE
.\build-docker.ps1 -Registry "ghcr.io/csharpfritz" -Push
Builds and pushes to GitHub Container Registry
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$false)]
[string]$Registry = "",
[Parameter(Mandatory=$false)]
[switch]$Push,
[Parameter(Mandatory=$false)]
[ValidateSet('Release', 'Debug')]
[string]$BuildConfiguration = 'Release'
)
$ErrorActionPreference = 'Stop'
Write-Host "π₯ CodeMedic Docker Build Script" -ForegroundColor Cyan
Write-Host "=================================" -ForegroundColor Cyan
Write-Host ""
# Ensure we're in the repository root
$scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Path
Set-Location $scriptPath
# Check if Docker is available
try {
docker --version | Out-Null
} catch {
Write-Error "Docker is not installed or not in PATH. Please install Docker first."
exit 1
}
# Check if nbgv is available, if not install it
$nbgvInstalled = Get-Command nbgv -ErrorAction SilentlyContinue
if (-not $nbgvInstalled) {
Write-Host "π¦ Installing Nerd Bank Git Versioning CLI tool..." -ForegroundColor Yellow
dotnet tool install -g nbgv
if ($LASTEXITCODE -ne 0) {
Write-Error "Failed to install nbgv. Please install it manually: dotnet tool install -g nbgv"
exit 1
}
}
# Get version information from NBGv2
Write-Host "π’ Retrieving version information from NBGv2..." -ForegroundColor Green
$versionJson = nbgv get-version -f json | ConvertFrom-Json
if (-not $versionJson) {
Write-Error "Failed to retrieve version information from NBGv2"
exit 1
}
$version = $versionJson.Version
$semVer2 = $versionJson.SemVer2
$assemblyVersion = $versionJson.AssemblyVersion
$fileVersion = $versionJson.AssemblyFileVersion
$informationalVersion = $versionJson.AssemblyInformationalVersion
$majorMinor = "$($versionJson.VersionMajor).$($versionJson.VersionMinor)"
$commit = $versionJson.GitCommitIdShort
Write-Host " Version: $version" -ForegroundColor White
Write-Host " SemVer2: $semVer2" -ForegroundColor White
Write-Host " Assembly Version: $assemblyVersion" -ForegroundColor White
Write-Host " File Version: $fileVersion" -ForegroundColor White
Write-Host " Informational: $informationalVersion" -ForegroundColor White
Write-Host " Commit: $commit" -ForegroundColor White
Write-Host ""
# Construct image name
$imageName = "codemedic"
if ($Registry) {
$imageName = "$Registry/$imageName"
}
# Build tags
$tags = @(
"${imageName}:${semVer2}",
"${imageName}:${majorMinor}",
"${imageName}:latest"
)
# Add commit-specific tag for tracking
$tags += "${imageName}:${semVer2}-${commit}"
Write-Host "π³ Building Docker image..." -ForegroundColor Green
Write-Host " Image name: $imageName" -ForegroundColor White
Write-Host " Tags:" -ForegroundColor White
foreach ($tag in $tags) {
Write-Host " - $tag" -ForegroundColor White
}
Write-Host ""
# Build the Docker image
$dockerBuildArgs = @(
"build",
"-f", "Dockerfile",
"--build-arg", "BUILD_CONFIGURATION=$BuildConfiguration",
"--build-arg", "VERSION=$semVer2",
"--build-arg", "ASSEMBLY_VERSION=$assemblyVersion",
"--build-arg", "FILE_VERSION=$fileVersion",
"--build-arg", "INFORMATIONAL_VERSION=$informationalVersion"
)
# Add all tags
foreach ($tag in $tags) {
$dockerBuildArgs += "-t"
$dockerBuildArgs += $tag
}
$dockerBuildArgs += "."
Write-Host "π¨ Executing: docker $($dockerBuildArgs -join ' ')" -ForegroundColor Gray
& docker $dockerBuildArgs
if ($LASTEXITCODE -ne 0) {
Write-Error "Docker build failed with exit code $LASTEXITCODE"
exit $LASTEXITCODE
}
Write-Host ""
Write-Host "β
Docker image built successfully!" -ForegroundColor Green
Write-Host ""
# Push if requested
if ($Push) {
if (-not $Registry) {
Write-Error "Cannot push without specifying a registry. Use -Registry parameter."
exit 1
}
Write-Host "π€ Pushing images to registry..." -ForegroundColor Green
foreach ($tag in $tags) {
Write-Host " Pushing: $tag" -ForegroundColor White
docker push $tag
if ($LASTEXITCODE -ne 0) {
Write-Error "Failed to push $tag"
exit $LASTEXITCODE
}
}
Write-Host ""
Write-Host "β
All images pushed successfully!" -ForegroundColor Green
Write-Host ""
}
# Display usage examples
Write-Host "π Usage Examples:" -ForegroundColor Cyan
Write-Host " Run health check:" -ForegroundColor White
Write-Host " docker run --rm -v `${PWD}:/repo $($tags[0]) health /repo" -ForegroundColor Gray
Write-Host ""
Write-Host " Show help:" -ForegroundColor White
Write-Host " docker run --rm $($tags[0]) --help" -ForegroundColor Gray
Write-Host ""
Write-Host " Interactive shell:" -ForegroundColor White
Write-Host " docker run --rm -it --entrypoint /bin/bash $($tags[0])" -ForegroundColor Gray
Write-Host ""