-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRun-Docker.ps1
More file actions
102 lines (79 loc) · 3.41 KB
/
Copy pathRun-Docker.ps1
File metadata and controls
102 lines (79 loc) · 3.41 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
<#
.SYNOPSIS
Builds a Docker image and optionally pushes it to a registry.
.DESCRIPTION
Builds a Docker image from a Dockerfile (optionally in a subfolder), applies additional tags,
and optionally pushes them to a registry. Uses the LibreDevOpsHelpers Docker helpers.
.PARAMETER DockerFileName
Name of the Dockerfile, for example Dockerfile or Dockerfile.alpine.
.PARAMETER DockerImageName
Base image name, for example base-images/azdo-agent-containers.
.PARAMETER RegistryUrl
Registry host, for example ghcr.io.
.PARAMETER RegistryUsername
Registry username.
.PARAMETER RegistryPassword
Registry password or token. Supplied as a string for pipeline use and converted to a secure
string before being passed to the registry.
.PARAMETER ImageOrg
Optional organisation override. Defaults to RegistryUsername.
.PARAMETER WorkingDirectory
Folder this script runs in. Defaults to the current directory.
.PARAMETER BuildContext
Docker build context. Defaults to the current directory.
.PARAMETER DebugMode
'true' or 'false'. Toggles DebugPreference.
.PARAMETER PushDockerImage
'true' or 'false'. Whether to push after build.
.PARAMETER AdditionalTags
Extra tags to apply and push. Defaults to latest and the current year-month.
.EXAMPLE
./Run-Docker.ps1 -BuildContext "$PWD/containers/alpine" -RegistryUrl ghcr.io `
-RegistryUsername $Env:GHCR_USER -RegistryPassword $Env:GHCR_TOKEN
#>
param(
[string] $DockerFileName = 'Dockerfile',
[string] $DockerImageName = 'base-images/azdo-agent-containers',
[string] $RegistryUrl = 'ghcr.io',
[string] $RegistryUsername,
[string] $RegistryPassword,
[string] $ImageOrg,
[string] $WorkingDirectory = (Get-Location).Path,
[string] $BuildContext = (Get-Location).Path,
[string] $DebugMode = 'false',
[string] $PushDockerImage = 'true',
[string[]] $AdditionalTags = @('latest', (Get-Date -Format 'yyyy-MM'))
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
$scriptDir = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
$manifest = Join-Path $scriptDir 'LibreDevOpsHelpers' 'LibreDevOpsHelpers.psd1'
if (-not (Test-Path -LiteralPath $manifest)) {
throw "Module manifest not found: $manifest"
}
Import-Module $manifest -Force -ErrorAction Stop
Set-Location $WorkingDirectory
if (-not $ImageOrg) { $ImageOrg = $RegistryUsername }
$fullImageName = '{0}/{1}/{2}' -f $RegistryUrl, $ImageOrg, $DockerImageName
$debug = ConvertTo-LdoBoolean -Value $DebugMode
$push = ConvertTo-LdoBoolean -Value $PushDockerImage
if ($debug) { $DebugPreference = 'Continue' }
Assert-LdoDockerExists
$dockerfilePath = Join-Path $BuildContext $DockerFileName
Build-LdoDockerImage -DockerfilePath $dockerfilePath -ContextPath $BuildContext -ImageName $fullImageName
$tagsToPush = @()
foreach ($tag in $AdditionalTags) {
$fullTag = '{0}:{1}' -f $fullImageName, $tag
Write-LdoLog -Level INFO -Message "Tagging: $fullTag"
docker tag $fullImageName $fullTag
if ($LASTEXITCODE -ne 0) {
throw "docker tag failed for $fullTag (exit $LASTEXITCODE)."
}
$tagsToPush += $fullTag
}
if ($push) {
$securePassword = ConvertTo-SecureString $RegistryPassword -AsPlainText -Force
Push-LdoDockerImage -FullTagNames $tagsToPush -RegistryUrl $RegistryUrl `
-RegistryUsername $RegistryUsername -RegistryPassword $securePassword
}
Write-LdoLog -Level SUCCESS -Message 'All done.'