-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdockerTask.ps1
More file actions
executable file
·134 lines (120 loc) · 3.77 KB
/
dockerTask.ps1
File metadata and controls
executable file
·134 lines (120 loc) · 3.77 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
<#
.SYNOPSIS
Builds and runs a Docker image.
.PARAMETER Compose
Runs docker-compose.
.PARAMETER Build
Builds a Docker image.
.PARAMETER Clean
Removes the image testaddin1 and kills all containers based on that image.
.PARAMETER ComposeForDebug
Builds the image and runs docker-compose.
.PARAMETER Environment
The enviorment to build for (Debug or Release), defaults to Debug
.EXAMPLE
C:\PS> .\dockerTask.ps1 -Build
Build a Docker image named testaddin1
#>
Param(
[Parameter(Mandatory=$True,ParameterSetName="Compose")]
[switch]$Compose,
[Parameter(Mandatory=$True,ParameterSetName="ComposeForDebug")]
[switch]$ComposeForDebug,
[Parameter(Mandatory=$True,ParameterSetName="Build")]
[switch]$Build,
[Parameter(Mandatory=$True,ParameterSetName="Clean")]
[switch]$Clean,
[parameter(ParameterSetName="Compose")]
[Parameter(ParameterSetName="ComposeForDebug")]
[parameter(ParameterSetName="Build")]
[parameter(ParameterSetName="Clean")]
[ValidateNotNullOrEmpty()]
[String]$Environment = "Debug"
)
$imageName="testaddin1"
$projectName="testaddin1"
$publicPort=3001
$url="http://localhost:$publicPort"
# Kills all running containers of an image and then removes them.
function CleanAll () {
$composeFileName = "docker-compose.yml"
if ($Environment -ne "Release") {
$composeFileName = "docker-compose.$Environment.yml"
}
if (Test-Path $composeFileName) {
docker-compose -f "$composeFileName" -p $projectName down --rmi all
$danglingImages = $(docker images -q --filter 'dangling=true')
if (-not [String]::IsNullOrWhiteSpace($danglingImages)) {
docker rmi -f $danglingImages
}
}
else {
Write-Error -Message "$Environment is not a valid parameter. File '$composeFileName' does not exist." -Category InvalidArgument
}
}
# Builds the Docker image.
function BuildImage () {
$composeFileName = "docker-compose.yml"
if ($Environment -ne "Release") {
$composeFileName = "docker-compose.$Environment.yml"
}
if (Test-Path $composeFileName) {
Write-Host "Building the image $imageName ($Environment)."
docker-compose -f $composeFileName -p $projectName build
}
else {
Write-Error -Message "$Environment is not a valid parameter. File '$composeFileName' does not exist." -Category InvalidArgument
}
}
# Runs docker-compose.
function Compose () {
$composeFileName = "docker-compose.yml"
if ($Environment -ne "Release") {
$composeFileName = "docker-compose.$Environment.yml"
}
if (Test-Path $composeFileName) {
Write-Host "Running compose file $composeFileName"
docker-compose -f $composeFileName -p $projectName kill
docker-compose -f $composeFileName -p $projectName up -d
}
else {
Write-Error -Message "$Environment is not a valid parameter. File '$dockerFileName' does not exist." -Category InvalidArgument
}
}
# Opens the remote site
function OpenSite () {
Write-Host "Opening site" -NoNewline
$status = 0
#Check if the site is available
while($status -ne 200) {
try {
$response = Invoke-WebRequest -Uri $url -Headers @{"Cache-Control"="no-cache";"Pragma"="no-cache"} -UseBasicParsing
$status = [int]$response.StatusCode
}
catch [System.Net.WebException] { }
if($status -ne 200) {
Write-Host "." -NoNewline
Start-Sleep 1
}
}
Write-Host
# Open the site.
Start-Process $url
}
$Environment = $Environment.ToLowerInvariant()
# Call the correct function for the parameter that was used
if($Compose) {
Compose
OpenSite
}
elseif($ComposeForDebug) {
$env:REMOTE_DEBUGGING = 1
BuildImage
Compose
}
elseif($Build) {
BuildImage
}
elseif ($Clean) {
CleanAll
}