-
Notifications
You must be signed in to change notification settings - Fork 0
/
Publish-DockerImage.ps1
89 lines (77 loc) · 2.23 KB
/
Publish-DockerImage.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
87
88
89
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingCmdletAliases", "")]
[CmdletBinding()]
param(
[string] $BasePath = (Get-Location),
[string] $Name = (. (Join-Path $BasePath 'project.config.ps1')).Image.Name,
[string[]] $Tag,
[string] $UserName = $env:DOCKER_USERNAME,
[string] $UserPassword = $env:DOCKER_USERPASSWORD
)
$ErrorActionPreference = 'Stop'
Write-Verbose "Docker Image: validate tag existance"
function Read-DockerImageTags {
param (
$Name,
$Next = "https://hub.docker.com/v2/repositories/$($Name)/tags/"
)
try {
do {
$response = Invoke-RestMethod $Next;
$response.results |
% { $_.name }
$Next = $response.next
} while ($Next)
} catch {
if ($_.Exception.Response.StatusCode.Value__ -ne 404) {
throw
}
}
}
$matchedTag = Read-DockerImageTags $Name |
? { $Tag -contains $_ } |
Select-Object -First 1
if ($matchedTag) {
Write-Error "Docker Image: Tag '$($matchedTag)' exists for image '$($Name)'."
}
Write-Verbose "Docker Image: tag"
$publishTag = $Tag |
% {
$newTag = "$($Name):$($_)"
Write-Verbose "Docker Image: * $($newTag)"
$arguments = @(
'tag', $Name, $newTag
)
& docker $arguments |
Out-Default
if ($LASTEXITCODE) {
Write-Error "Docker Image: 'docker $($arguments -join ' ')' failed with status $($LASTEXITCODE)."
}
$newTag
}
if ($UserName) {
Write-Verbose "Docker Image: login"
$arguments = @(
'login',
'--username', $UserName,
'--password-stdin'
)
$UserPassword |
& docker $arguments |
Out-Default
if ($LASTEXITCODE) {
Write-Error "Docker Image: 'docker $($arguments -join ' ')' failed with status $($LASTEXITCODE)."
}
}
Write-Verbose "Docker Image: push"
$publishTag |
% {
Write-Verbose "Docker Image: * $($_)"
$arguments = @(
'push', $_
)
& docker $arguments |
Out-Default
if ($LASTEXITCODE) {
Write-Error "Docker Image: 'docker $($arguments -join ' ')' failed with status $($LASTEXITCODE)."
}
}