-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBuilder.ps1
205 lines (168 loc) · 6.15 KB
/
Builder.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
param(
[string]$url,
[string]$ref
)
#reset global vars - useful when we execute for psh, because it reset the previous run values
$global:git = $null
$global:cmd = $null
$global:workingDir = $null
$global:email = $null
$global:name = $null
$global:build = $null
$origin_dir = Get-Location
trap [Exception] {
cd $origin_dir
}
function load_settings([string]$url)
{
trap [Exception] {
write-error $_.Exception
exit
}
$mutex = new-object System.Threading.Mutex($settingsFile)
$mutex.WaitOne()
$settingsFile = [System.IO.Path]::Combine([System.IO.Path]::GetDirectoryName($MyInvocation.ScriptName), "Settings.config")
$settings = [xml](get-content $settingsFile)
$currentProjects = ""
foreach ($project in $settings.settings.project)
{
$currentProjects += $currentProjects + "`r`n"
if($project.url -ne $url -or $project.ref -ne $ref)
{
continue;
}
$global:git = $project.git
$global:cmd = $project.cmd
$global:workingDir = $project.workingDir
$global:email = $project.email
$global:name = $project.name
Write-Host $project.email
Write-Host $project
Write-Host $global:email
foreach($build in $settings.settings.builds.project)
{
if($build.name -ne $project.name)
{
continue;
}
$global:build = $build.build
$build.build = ([int]::Parse($build.build) + 1).ToString()
break;
}
write-host "Found settings for $url: $name"
$writerSettings = new-object System.Xml.XmlWriterSettings
$writerSettings.OmitXmlDeclaration = $true
$writerSettings.NewLineOnAttributes = $true
$writerSettings.Indent = $true
$writer = [System.Xml.XmlWriter]::Create($settingsFile, $writerSettings)
$settings.WriteTo($writer)
$writer.Flush()
$writer.Close()
$mutex.ReleaseMutex()
$mutex.Close()
return
}
$body = "Got a notification about a build for $url ($ref), but could not find a matching project to build with`r`nCurrentProjects`r`n"+$currentProjects
send_email -subject "Could not find matching project for $url" -body $body
write-error "Could not find matching project for $url"
exit
}
function send_email([string]$subject, [string]$body)
{
Write-Host $subject
if($global:email -ne $null){
trap [Exception] {
write-error $_.Exception
exit
}
$settingsFile = [System.IO.Path]::Combine([System.IO.Path]::GetDirectoryName($MyInvocation.ScriptName), "Settings.config")
$settings= [xml](get-content $settingsFile)
$emailSettings = $settings.settings.email
$smtp = new-object System.Net.Mail.SmtpClient
$smtp.Host = $emailSettings.smtpServer
$smtp.Port = $emailSettings.port
$smtp.EnableSsl = $emailSettings.useSSL
$smtp.Credentials = new-object System.Net.NetworkCredential($emailSettings.username, $emailSettings.password)
$smtp.Send($emailSettings.from, $global:email, $subject, $body)
}
}
load_settings($url)
$parts = $ref.Split('/')
$branch = $parts[$parts.Length -1]
$git_log = ""
if(Test-Path $workingDir/.git) # repository exists, try updating or nuke if failed
{
Write-Host "Fetching updates from $git / $branch as $env:username"
cd $workingDir
$git_log = git pull origin $branch
Write-Host $git_log
if($lastExitCode -ne 0)
{
$body = $git_log -join "`r`n"
write-host $body
send_email -subject "Failed to update repository: $name" -body $body
cd ..
Remove-Item $workingDir -force -recurse
send_email -subject "Nuking repo and trying fresh clone: $name" -body $body
}
else
{
git submodule init
git submodule update
}
}
if( (Test-Path $workingDir/.git) -eq $false ) # need new updates
{
Write-Host "Clone git repository from $git as $env:username"
$git_log = git clone $git $workingDir 2>&1
if($lastExitCode -ne 0)
{
$body = $git_log -join "`r`n"
write-host $body
send_email -subject "Failed to clone repository: $name" -body $body
exit
}
cd $workingDir
if( $branch -ne "master" ) { # need to checkout the branch
$git_log = git checkout remotes/origin/$branch -b $branch
$co_exit_code = $lastExitCode
$body = $git_log -join "`r`n"
write-host $body
if($co_exit_code -ne 0)
{
send_email -subject "Failed to clone repository: $name, could not switch branch to $branch" -body $body
exit
}
}
git submodule init
git submodule update
}
write-output $git_log
write-host "done updating from repository"
$log = $env:push_msg
if($log -eq $null -or $log.Length -eq 0)
{
$log = git log -1 --oneline
}
$env:ccnetnumericlabel = $build
$env:buildlabel = $build
write-host "Build started for $name $ref"
send_email -subject "Build started for $name $ref ($global:build)" -body "Starting build for $name for:`r`n$log"
$error.Clear()
$buildStart = [DateTime]::Now
$output = Invoke-Expression "$cmd 2>&1" -ErrorAction silentlycontinue
if ($error.Count -gt 0 -or $lastexitcode -ne 0)
{
$body = "Build failed for $name $ref ($global:build). Duration $([DateTime]::Now - $buildStart) for:`r`n$log`r`n`r`nBuild Log:`r`n" + ($output -join "`r`n") + "`r`n$($error.Count) Errors: " + ($error -join "`r`n")
write-host $body
send_email -subject "Build FAILED for $name" -body $body
write-host "BUILD FAILED"
}
else
{
$body = "Build passed for $name $ref ($global:build). Duration $([DateTime]::Now - $buildStart) for:`r`n$log`r`n`r`n" + ($output -join "`r`n")
write-host $body
send_email -subject "Build SUCCESSFULL for $name ($global:build)" -body $body
write-host "BUILD SUCCESSFULL"
}
cd $origin_dir