-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.ps1
148 lines (125 loc) · 4.4 KB
/
install.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
if($PSVersionTable.PSVersion.Major -lt 2) {
Write-Warning "posh-jenkins requires PowerShell 2.0 or better; you have version $($Host.Version)."
return
}
[System.Reflection.Assembly]::LoadWithPartialName("System.Web") | out-null
if ($args.length -match 0) {
Throw (New-Object system.ArgumentNullException -arg "url")
}
$jenkinsUrl = $args[0];
function ConvertTo-UrlEncodedPath([string]$dataToConvert)
{
begin {
function EncodeCore([string]$data) { return [System.Web.HttpUtility]::UrlPathEncode($data) }
}
process { if ($_ -as [string]) { EncodeCore($_) } }
end { if ($dataToConvert) { EncodeCore($dataToConvert) } }
}
<#
.SYNOPSIS
Instructs Jenkins to build the specified job with the default parameters.
.DESCRIPTION
Instructs Jenkins to build the specified job with the default parameters.
.PARAMETER job
The name of the job, as displayed using Jenkins-Jobs.
.EXAMPLE
PS C:\> Jenkins-Build "Job Name"
#>
function Jenkins-Build($job)
{
$encoded = ConvertTo-UrlEncodedPath($job)
$url = $jenkinsUrl + "job/" + $encoded + "/buildWithParameters"
$req = [System.Net.WebRequest]::Create($url)
$req.Method ="GET"
$req.ContentLength = 0
$resp = $req.GetResponse()
return $resp.StatusCode
}
Set-Alias jb Jenkins-Build
<#
.SYNOPSIS
Lists the jobs configured on the Jenkins server, colouring each depending on status.
.DESCRIPTION
Lists the jobs configured on the Jenkins server, colouring each depending on status.
.EXAMPLE
PS C:\> Jenkins-Jobs
#>
function Jenkins-Jobs()
{
$url = $jenkinsUrl + "api/xml?tree=jobs[name,color,lastBuild[building,result]]"
$client = New-Object System.Net.WebClient
$bytes = $client.DownloadData($url)
$response = [System.Text.Encoding]::ASCII.GetString($bytes)
$xml = [xml]($response)
$string = $xml.hudson.job | Select-Object @{Name="Name"; Expression={$_.name} }, @{Name="LastResult"; Expression={$_.lastBuild.result}} | Format-Table Name, @{Name="Last Result"; Expression={$_.LastResult}} -autosize | Out-String
$array = $string.Split("`n", [StringSplitOptions]::RemoveEmptyEntries)
$array | foreach {
if($_ -match "SUCCESS"){
Write-Host $_ -ForegroundColor "cyan";
} elseif ($_ -match "UNSTABLE") {
Write-Host $_ -ForegroundColor "yellow";
} elseif ($_ -match "FAILURE") {
Write-Host $_ -ForegroundColor "red";
} else {
Write-Host $_
}
}
}
Set-Alias jj Jenkins-Jobs
<#
.SYNOPSIS
Displays various details about the Jenkins server.
.DESCRIPTION
Displays various details about the Jenkins server.
.EXAMPLE
PS C:\> Jenkins-Info
#>
function Jenkins-Info()
{
$url = $jenkinsUrl + "api/xml"
$client = New-Object System.Net.WebClient
$bytes = $client.DownloadData($url)
$response = [System.Text.Encoding]::ASCII.GetString($bytes)
$xml = [xml]($response)
return $xml.hudson
}
Set-Alias ji Jenkins-Info
function script:jenkinsJobs($filter) {
$url = $jenkinsUrl + "api/xml?tree=jobs[name]"
$client = New-Object System.Net.WebClient
$bytes = $client.DownloadData($url)
$response = [System.Text.Encoding]::ASCII.GetString($bytes)
$xml = [xml]($response)
return $string = $xml.hudson.job | foreach { $_.name } | where { $_ -like "$filter*" } | foreach {
if ($_ -like '* *') {
"""$_"""
} else {
$_
}
}
}
function script:expandJenkinsBuildAlias($cmd, $rest) {
return "jb $cmd$rest"
}
function JenkinsBuildTabExpansion($lastBlock) {
if($lastBlock -match '^$jb (?<job>\S+)$') {
$lastBlock = expandJenkinsBuildAlias $Matches['cmd'] $Matches['args']
}
switch -regex ($lastBlock) {
'^jb.* (?<names>\S*)$' {
jenkinsJobs $matches['names']
}
}
}
if (Test-Path Function:\TabExpansion) {
Rename-Item Function:\TabExpansion JenkinsTabExpansionBackup
}
function TabExpansion($line, $lastWord) {
$lastBlock = [regex]::Split($line, '[|;]')[-1].TrimStart()
switch -regex ($lastBlock) {
# Execute Jenkins-Build tab completion for all Jenkins-Build commands
'jb (.*)' { JenkinsBuildTabExpansion $lastBlock }
# Fall back on existing tab expansion
default { if (Test-Path Function:\JenkinsTabExpansionBackup) { JenkinsTabExpansionBackup $line $lastWord } }
}
}