This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
/
_dockerTaskGeneric.ps1
162 lines (145 loc) · 5.55 KB
/
_dockerTaskGeneric.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<#
.SYNOPSIS
Builds and runs a Docker image.
.PARAMETER Compose
Runs docker-compose.
.PARAMETER Build
Builds a Docker image.
.PARAMETER Clean
Removes the image <%= imageName %> and kills all containers based on that image.<% if (includeComposeForDebug) { %>
.PARAMETER ComposeForDebug
Builds the image and runs docker-compose.<% } %><% if (includeStartDebugging) { %>
.PARAMETER StartDebugging
Finds the running container and starts the debugger inside of it.<% } %>
.PARAMETER Environment
The enviorment to build for (Debug or Release), defaults to Debug
.EXAMPLE
C:\PS> .\dockerTask.ps1 -Build
Build a Docker image named <%= imageName %>
#>
Param(
[Parameter(Mandatory=$True,ParameterSetName="Compose")]
[switch]$Compose,<% if (includeComposeForDebug) { %>
[Parameter(Mandatory=$True,ParameterSetName="ComposeForDebug")]
[switch]$ComposeForDebug,<% } %><% if (includeStartDebugging) { %>
[Parameter(Mandatory=$True,ParameterSetName="StartDebugging")]
[switch]$StartDebugging,<% } %>
[Parameter(Mandatory=$True,ParameterSetName="Build")]
[switch]$Build,
[Parameter(Mandatory=$True,ParameterSetName="Clean")]
[switch]$Clean,
[parameter(ParameterSetName="Compose")]<% if (includeComposeForDebug) { %>
[Parameter(ParameterSetName="ComposeForDebug")]<% } %>
[parameter(ParameterSetName="Build")]
[parameter(ParameterSetName="Clean")]
[ValidateNotNullOrEmpty()]
[String]$Environment = "Debug"
)
$imageName="<%= imageName %>"
$projectName="<%= composeProjectName %>"<% if (includeStartDebugging) { %>
$serviceName="<%= serviceName %>"
$containerName="<%= '${projectName}_${serviceName}' %>_1"<% } %><% if (isWebProject) { %>
$publicPort=<%= portNumber %>
$url="http://localhost:$publicPort"<% } %><% if (projectType === 'dotnet' && (dotnetVersion === 'RC2' || dotnetVersion === 'RTM')) { %>
$runtimeID = "debian.8-x64"
$framework = "netcoreapp1.0"<% } %>
# 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) {<% if (projectType === 'dotnet' && (dotnetVersion === 'RC2' || dotnetVersion === 'RTM')) { %>
Write-Host "Building the project ($ENVIRONMENT)."
$pubFolder = "bin\$Environment\$framework\publish"
dotnet publish -f $framework -r $runtimeID -c $Environment -o $pubFolder
Write-Host "Building the image $imageName ($Environment)."
docker-compose -f "$pubFolder\$composeFileName" -p $projectName build<% } else { %>
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
}
}<% if (includeStartDebugging) { %>
function StartDebugging () {<% if (isWebProject) { %>
Write-Host "Running on $url"
<% } %>
$containerId = (docker ps -f "name=$containerName" -q -n=1)
if ([System.String]::IsNullOrWhiteSpace($containerId)) {
Write-Error "Could not find a container named $containerName"
}
docker exec -i $containerId /clrdbg/clrdbg --interpreter=mi
}<% } %><% if (isWebProject) { %>
# 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<% if (isWebProject) { %>
OpenSite<% } %>
}<% if (includeComposeForDebug) { %>
elseif($ComposeForDebug) {
$env:REMOTE_DEBUGGING = 1
BuildImage
Compose
}<% } %><% if (includeStartDebugging) { %>
elseif($StartDebugging) {
StartDebugging
}<% } %>
elseif($Build) {
BuildImage
}
elseif ($Clean) {
CleanAll
}