-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdockerTask.ps1
124 lines (110 loc) · 3.44 KB
/
dockerTask.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<#
.SYNOPSIS
Builds and runs a Docker image.
.PARAMETER Compose
Runs docker-compose.
.PARAMETER Build
Builds a Docker image.
.PARAMETER Clean
Removes the image cb and kills all containers based on that image.
.PARAMETER Environment
The enviorment to build for (Debug or Release), defaults to Debug
.EXAMPLE
C:\PS> .\dockerTask.ps1 -Build
Build a Docker image named cb
#>
Param(
[Parameter(Mandatory=$True,ParameterSetName="Compose")]
[switch]$Compose,
[Parameter(Mandatory=$True,ParameterSetName="Build")]
[switch]$Build,
[Parameter(Mandatory=$True,ParameterSetName="Clean")]
[switch]$Clean,
[parameter(ParameterSetName="Compose")]
[parameter(ParameterSetName="Build")]
[parameter(ParameterSetName="Clean")]
[ValidateNotNullOrEmpty()]
[String]$Environment = "Debug"
)
$imageName="cb"
$projectName="cb"
$publicPort=8080
$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($Build) {
BuildImage
}
elseif ($Clean) {
CleanAll
}