-
Notifications
You must be signed in to change notification settings - Fork 5
/
RunContainerAgent.ps1
71 lines (68 loc) · 2.21 KB
/
RunContainerAgent.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
Param(
[string] $AzureDevOpsOrg="https://dev.azure.com/drobny",
[string] $PersonalAccessToken=$ENV:ADOPAT,
[Parameter(Mandatory=$true)]
[string] $AgentName,
[string] $PoolName = 'Container',
[string] $ImageName = 'containeragent.azurecr.io/pipeline-agent:windows',
[switch] $Interactive,
[switch] $RunOnce,
[hashtable] $VolumeMount
)
# Docker Installed?
if ($null -eq (Get-Command Docker))
{
Throw "Docker for Windows is not installed!"
}
# Volumne Mounting
[string[]] $volumes = @()
if ($null -ne $VolumeMount) {
foreach($entry in $VolumeMount.GetEnumerator()) {
$volumes += "-v $($entry.Key):$($entry.Value)"
}
}
# Interactive Mode
if ($Interactive.IsPresent) {
[string[]] $argList = @( `
"run", "-it", "--rm", "--name $AgentName", `
"-e VSTS_AGENT_INPUT_URL=$AzureDevOpsOrg", `
"-e VSTS_AGENT_INPUT_AUTH=pat", `
"-e VSTS_AGENT_INPUT_TOKEN=$PersonalAccessToken", `
"-e VSTS_AGENT_INPUT_POOL=$PoolName", `
"-e VSTS_AGENT_INPUT_AGENT=$AgentName")
if ($RunOnce.IsPresent) {
$argList += "-e RUN_ONCE=True"
}
if ($volumes.Count -ne 0) {
$argList += $volumes
}
$argList += @($ImageName, `
"PowerShell")
Start-Process docker -ArgumentList $argList
}
else {
[string[]] $argList = @( `
"run", "-d", "--rm", "--name $AgentName", `
"-e VSTS_AGENT_INPUT_URL=$AzureDevOpsOrg", `
"-e VSTS_AGENT_INPUT_AUTH=pat", `
"-e VSTS_AGENT_INPUT_TOKEN=$PersonalAccessToken", `
"-e VSTS_AGENT_INPUT_POOL=$PoolName", `
"-e VSTS_AGENT_INPUT_AGENT=$AgentName")
if ($RunOnce.IsPresent) {
$argList += "-e RUN_ONCE=True"
}
if ($volumes.Count -ne 0) {
$argList += $volumes
}
$argList += $ImageName
[string] $tmpOut = [System.IO.Path]::GetTempFileName();
[string] $tmpErr = [System.IO.Path]::GetTempFileName();
Start-Process docker -ArgumentList $argList -RedirectStandardOutput $tmpOut -RedirectStandardError $tmpErr -Wait -WindowStyle Hidden
if ((Get-Item $tmpOut).Length -gt 0) {
Get-Content $tmpOut
}
if ((Get-Item $tmpErr).Length -gt 0) {
Get-Content $tmpErr
}
Remove-Item $tmpOut,$tmpErr -Force -ErrorAction SilentlyContinue
}