-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdockerTask.ps1
More file actions
132 lines (118 loc) · 4.02 KB
/
Copy pathdockerTask.ps1
File metadata and controls
132 lines (118 loc) · 4.02 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
<#
.SYNOPSIS
Builds and runs a Docker image.
.PARAMETER Compose
Runs docker-compose.
.PARAMETER Build
Builds a Docker image.
.PARAMETER Clean
Removes the image vscodedebugging and kills all containers based on that image.
.PARAMETER ComposeForDebug
Builds the image and runs docker-compose.
.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 vscodedebugging
#>
Param(
[Parameter(Mandatory=$True,ParameterSetName="Compose")]
[switch]$Compose,
[Parameter(Mandatory=$True,ParameterSetName="ComposeForDebug")]
[switch]$ComposeForDebug,
[Parameter(Mandatory=$True,ParameterSetName="StartDebugging")]
[switch]$StartDebugging,
[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="vscodedebugging"
$projectName="vscodedebugging"
$serviceName="vscodedebugging"
$containerName="${projectName}_${serviceName}_1"
$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) {
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-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
}
}
function StartDebugging () {
$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
}
$Environment = $Environment.ToLowerInvariant()
# Call the correct function for the parameter that was used
if($Compose) {
Compose
}
elseif($ComposeForDebug) {
$env:REMOTE_DEBUGGING = 1
BuildImage
Compose
}
elseif($StartDebugging) {
StartDebugging
}
elseif($Build) {
BuildImage
}
elseif ($Clean) {
CleanAll
}