-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker.ps1
106 lines (81 loc) · 3.33 KB
/
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<#
.DESCRIPTION
Script for deploying docker images
.EXAMPLE
./script.ps1 -docker-hub-username gigas002 -github-username Gigas002 -i @{"dotnet.cli"="Cli.Dockerfile";"dotnet.benchmarks"="Benchmarks.Dockerfile"} -github-repo-name dotnet_gh_deploy -b "Directory.Build.props" -docker-continious-tag "latest"
.LINK
https://github.com/Gigas002/dotnet_gh_deploy
#>
[CmdletBinding(PositionalBinding = $false)]
param (
# Docker hub token
[Parameter ()]
[Alias("docker-hub-token")]
[SecureString] $dockerHubToken = (Read-Host "Enter your docker hub token token" -AsSecureString),
# Github token
[Parameter ()]
[Alias("github-token")]
[SecureString] $githubToken = (Read-Host "Enter your github token" -AsSecureString),
# Docker hub username
[Parameter ()]
[ValidateNotNullOrEmpty ()]
[Alias("docker-hub-username")]
[string] $dockerHubUsername = "gigas002",
# Github username
[Parameter ()]
[ValidateNotNullOrEmpty ()]
[Alias("github-username")]
[string] $githubUsername = "Gigas002",
# Dictionary<ProjectName,DockerfilePath>
[Parameter ()]
[ValidateNotNullOrEmpty ()]
[hashtable] $inputs = [ordered]@{
"dotnet.cli" = "Cli.Dockerfile";
"dotnet.benchmarks" = "Benchmarks.Dockerfile"
},
# Github repo name
[Parameter ()]
[ValidateNotNullOrEmpty ()]
[Alias("github-repo-name")]
[string] $githubRepoName = "dotnet_gh_deploy",
# Directory.Build.props path
[Parameter ()]
[ValidateNotNullOrEmpty ()]
[Alias("b", "build-props-path")]
[string] $buildPropsPath = "Directory.Build.props",
# docker continious tag
[Parameter ()]
[ValidateNotNullOrEmpty ()]
[Alias("docker-continious-tag")]
[string] $dockerContiniousTag = "latest"
)
#region Constants
Set-Variable DockerHubRegistry -Option ReadOnly -Value "index.docker.io"
Set-Variable GithubRegistry -Option ReadOnly -Value "ghcr.io"
#endregion
#region Functions
function DeployDocker([string]$registry, [string] $username, [SecureString] $token,
[string] $imageKey, [string] $tag) {
$dockerfile = $inputs[$imageKey]
Write-Host "docker build image: $imageKey with $dockerfile" -ForegroundColor Yellow
docker build -t $tag -f $dockerfile .
if ($token -and $token.Length -gt 0) {
$decryptedToken = ConvertFrom-SecureString $token -AsPlainText
Write-Host "docker login to registry: $registry" -ForegroundColor Yellow
docker login $registry -u $username -p $decryptedToken
Write-Host "docker push image: $tag" -ForegroundColor Yellow
docker push $tag
Write-Host "docker logout: $registry" -ForegroundColor Yellow
docker logout $registry
}
}
#endregion
Write-Host "Deploy of docker images started..." -ForegroundColor Yellow
$_, $_, $_, $dockerTag = ./read_version.ps1 -b $buildPropsPath -dockerContiniousTag $dockerContiniousTag
foreach ($imageKey in $inputs.Keys) {
$dockerHubTag = "$dockerHubUsername/${imageKey}:$dockerTag"
$githubTag = "$GithubRegistry/$githubUserName/$githubRepoName/${imageKey}:$dockerTag"
DeployDocker $DockerHubRegistry $dockerHubUsername $dockerHubToken $imageKey $dockerHubTag
DeployDocker $GithubRegistry $githubUsername $githubToken $imageKey $githubTag
}
Write-Host "Finished deploy of docker images" -ForegroundColor Green