-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRun-Docker.ps1
More file actions
130 lines (111 loc) · 6.15 KB
/
Copy pathRun-Docker.ps1
File metadata and controls
130 lines (111 loc) · 6.15 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
<#
.SYNOPSIS
Build (and optionally push) a Docker image – GitHub-Actions-friendly.
#>
param (
[string] $DockerFileName = 'Dockerfile',
[string] $DockerImageName = 'azdo-agent-containers/default',
[string] $RegistryUrl = 'ghcr.io',
[string] $RegistryUsername,
[string] $RegistryPassword,
[string] $ImageOrg,
[string] $WorkingDirectory = (Get-Location).Path,
[string] $BuildContext = (Get-Location).Path,
[string] $DebugMode = 'false', # ← string on purpose (CI inputs)
[string] $PushDockerImage = 'true',
[string[]] $AdditionalTags = @('latest', (Get-Date -Format 'yyyy-MM'))
)
#───────────────────────────────────────────────────────────────────────────────
# 0. Trust PSGallery + install LibreDevOpsHelpers
#───────────────────────────────────────────────────────────────────────────────
try {
if (Get-Command Set-PSRepository -EA SilentlyContinue) {
if ((Get-PSRepository -Name PSGallery).InstallationPolicy -ne 'Trusted') {
Set-PSRepository -Name PSGallery -InstallationPolicy Trusted -EA Stop
}
}
elseif (Get-Command Set-PSResourceRepository -EA SilentlyContinue) {
Set-PSResourceRepository -Name PSGallery -Trusted -EA Stop
}
else {
throw 'Neither PowerShellGet nor PSResourceGet is available.'
}
Write-Host "✅ PSGallery is trusted"
} catch {
Write-Error "❌ Failed to trust PSGallery: $_"; exit 1
}
if (-not (Get-Module -ListAvailable -Name LibreDevOpsHelpers)) {
try {
Install-Module LibreDevOpsHelpers -Repository PSGallery `
-Scope CurrentUser -Force -AllowClobber -EA Stop
Write-Host "✅ Installed LibreDevOpsHelpers"
} catch {
Write-Error "❌ Could not install LibreDevOpsHelpers: $_"; exit 1
}
}
$modInfo = Import-Module LibreDevOpsHelpers -PassThru
$version = $modInfo.Version.ToString()
_LogMessage INFO "✅ LibreDevOpsHelpers loaded with version - $version" -InvocationName $MyInvocation.MyCommand.Name
#───────────────────────────────────────────────────────────────────────────────
# 1. Resolve paths & flags *before* we cd anywhere
#───────────────────────────────────────────────────────────────────────────────
$repoRoot = Convert-Path $WorkingDirectory # absolute
if ($BuildContext -eq 'github_workspace') { $BuildContext = $repoRoot }
if (-not [IO.Path]::IsPathRooted($BuildContext)) {
$BuildContext = Join-Path $repoRoot $BuildContext
}
$BuildContext = (Resolve-Path $BuildContext).Path # canonical
# Dockerfile:
if ([IO.Path]::IsPathRooted($DockerFileName) -or
$DockerFileName -match '[\\/]' ) {
# caller supplied an explicit path (e.g. containers/ubuntu/Dockerfile)
$DockerfilePath = (Resolve-Path $DockerFileName).Path
} else {
$DockerfilePath = (Resolve-Path (Join-Path $BuildContext $DockerFileName)).Path
}
if (-not $ImageOrg) { $ImageOrg = $RegistryUsername }
$DockerImageName = "{0}/{1}/{2}" -f $RegistryUrl, $ImageOrg, $DockerImageName
$DebugMode = ConvertTo-Boolean $DebugMode
$PushDockerImage = ConvertTo-Boolean $PushDockerImage
if ($DebugMode) { $DebugPreference = 'Continue' }
#───────────────────────────────────────────────────────────────────────────────
# 2. Build helpers (only Build-DockerImage changed)
#───────────────────────────────────────────────────────────────────────────────
function Build-DockerImage {
[CmdletBinding()] param(
[Parameter(Mandatory)][string] $DockerfilePath,
[string] $ContextPath = '.',
[Parameter(Mandatory)][string] $ImageName
)
Write-Host "⏳ Building '$ImageName' from Dockerfile: $DockerfilePath"
Write-Host " context: $ContextPath"
docker build `
-f $DockerfilePath `
-t $ImageName `
$ContextPath | Out-Host
if ($LASTEXITCODE -ne 0) {
Write-Error "docker build failed (exit $LASTEXITCODE)"; return $false
}
return $true
}
#───────────────────────────────────────────────────────────────────────────────
# 3. Build / Tag / Push
#───────────────────────────────────────────────────────────────────────────────
Assert-DockerExists
$built = Build-DockerImage `
-DockerfilePath $DockerfilePath `
-ContextPath $BuildContext `
-ImageName $DockerImageName
if (-not $built) { Write-Error '❌ docker build failed'; exit 1 }
foreach ($tag in $AdditionalTags) {
$full = '{0}:{1}' -f $DockerImageName, $tag
_LogMessage INFO "🏷 Tagging: $full" -InvocationName $MyInvocation.MyCommand.Name
docker tag $DockerImageName $full
}
if ($PushDockerImage) {
$tags = $AdditionalTags | ForEach-Object { '{0}:{1}' -f $DockerImageName, $_ }
if (-not (Push-DockerImage -FullTagNames $tags -RegistryUrl $RegistryUrl -RegistryUsername $RegistryUsername -RegistryPassword $RegistryPassword)) {
Write-Error '❌ docker push failed'; exit 1
}
}
_LogMessage INFO '✅ All done.' -InvocationName $MyInvocation.MyCommand.Name