-
Notifications
You must be signed in to change notification settings - Fork 47
/
Jenkinsfile
225 lines (222 loc) · 8.94 KB
/
Jenkinsfile
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
pipeline {
agent { label "windows" }
environment {
GITHUB_TOKEN = credentials("github-publish-token")
}
stages {
// Configure git
stage("Configure git") {
steps {
bat "git config user.email burt-macklin@jenkins"
bat 'git config user.name "Agent Burt Macklin"'
}
}
// Determine build & publish flags for branch
stage("Setup bleeding edge environment") {
when { branch "main" }
steps {
script {
env.ARTIFACT_CACHES = "bleeding-edge"
env.AUTO_PUBLISH = "true"
env.BUILD_CONFIG = "debug"
env.IS_PRERELEASE = "true"
env.JOB_CACHE = "bleeding-edge"
env.TAG_PREFIX = "Unstable Release"
}
}
}
stage("Setup experimental environment") {
when { branch "experimental" }
steps {
script {
env.ARTIFACT_CACHES = "experimental"
env.AUTO_PUBLISH = "false"
env.BUILD_CONFIG = "debug"
env.IS_PRERELEASE = "true"
env.JOB_CACHE = "experimental"
env.TAG_PREFIX = "Experimental Release"
}
}
}
stage("Setup pre-release environment") {
when { branch "prerelease" }
steps {
script {
env.ARTIFACT_CACHES = "bleeding-edge,prerelease"
env.AUTO_PUBLISH = "true"
env.BUILD_CONFIG = "release"
env.IS_PRERELEASE = "true"
env.JOB_CACHE = "prerelease"
env.TAG_PREFIX = "Pre-Release"
}
}
}
stage("Setup release environment") {
when { branch "release" }
steps {
script {
env.ARTIFACT_CACHES = "bleeding-edge,prerelease,release"
env.AUTO_PUBLISH = "true"
env.BUILD_CONFIG = "release"
env.IS_PRERELEASE = "false"
env.JOB_CACHE = "release"
env.TAG_PREFIX = "Stable Release"
}
}
}
// Determine the version number
stage("Calculate semver") {
steps {
bat "gitversion /output buildserver"
script {
def props = readProperties file: "gitversion.properties"
env.GITVERSION_SEMVER = props.GitVersion_SemVer
env.PUBLISH_TAG = "v${props.GitVersion_SemVer}"
}
}
}
// Build
stage("Build") {
steps {
powershell '''
$DriveRoot = Split-Path -Path $env:WORKSPACE -Qualifier
$ReleasePath = Join-Path -Path $DriveRoot -ChildPath "usi-releases"
$JobReleasePath = Join-Path -Path $ReleasePath -ChildPath $env:JOB_CACHE
$ReferencePath = Join-Path -Path $JobReleasePath -ChildPath "000_USITools"
dotnet build --output FOR_RELEASE/GameData/UmbraSpaceIndustries/LifeSupport --configuration $env:BUILD_CONFIG `
--verbosity detailed /p:ReferencePath="$ReferencePath" ./Source/USILifeSupport/USILifeSupport.csproj
'''
}
}
// Update artifact cache
stage("Cache artifacts") {
steps {
powershell '''
try {
Write-Output "Determining releases folder location..."
$DriveRoot = Split-Path -Path $env:WORKSPACE -Qualifier
$ReleaseFolder = "usi-releases"
$ReleasePath = Join-Path -Path $DriveRoot -ChildPath $ReleaseFolder
Write-Output "Releases folder location is: $ReleasePath"
Write-Output "Determining artifact folder locations..."
$CacheNames = $env:ARTIFACT_CACHES.Split(",")
$CacheNames.ForEach({
Write-Output "Working on cache for: $_..."
$CachePath = Join-Path -Path $ReleasePath -ChildPath $_
Write-Output "Branch folder location is: $CachePath"
New-Item -Path $ReleasePath -Name $_ -ItemType Directory -Force
Write-Output "Removing old artifacts..."
$NewArtifacts = Get-ChildItem -Path ./FOR_RELEASE/GameData/UmbraSpaceIndustries -Attributes Directory -Name
$NewArtifacts.ForEach({
$ArtifactPath = Join-Path -Path (Join-Path -Path $CachePath -ChildPath "UmbraSpaceIndustries") -ChildPath $_
if (Test-Path -Path $ArtifactPath) {
Remove-Item -Path $ArtifactPath -Recurse -Force
}
})
Write-Output "Caching new artifacts..."
Copy-Item -Path ./FOR_RELEASE/GameData/* -Destination $CachePath -Recurse
})
}
catch {
throw $_
}
'''
}
}
// Packaging
stage("Package artifacts") {
steps {
powershell '''
Write-Output "Gathering artifacts to package..."
if (Test-Path -Path "./artifacts") {
Remove-Item -Path "./artifacts/*" -Recurse -Force
}
New-Item -Path . -Name "artifacts" -ItemType Directory -Force
Copy-Item -Path ./FOR_RELEASE/* -Destination ./artifacts -Recurse
Copy-Item -Path ./*.txt -Destination ./artifacts
Write-Output "Pulling in cached dependencies..."
$DriveRoot = Split-Path -Path $env:WORKSPACE -Qualifier
$ReleasePath = Join-Path -Path $DriveRoot -ChildPath "usi-releases"
$ModuleManagerPath = Join-Path -Path $ReleasePath -ChildPath "ModuleManager"
$CachePath = Join-Path -Path $ReleasePath -ChildPath $env:JOB_CACHE
$UsiCachePath = Join-Path -Path $CachePath -ChildPath "UmbraSpaceIndustries"
Copy-Item -Path $ModuleManagerPath/* -Destination ./artifacts/GameData
Copy-Item -Path (Join-Path -Path $CachePath -ChildPath "000_USITools") -Destination ./artifacts/GameData -Recurse
Copy-Item -Path (Join-Path -Path $CachePath -ChildPath "CommunityCategoryKit") -Destination ./artifacts/GameData -Recurse
Copy-Item -Path (Join-Path -Path $CachePath -ChildPath "CommunityResourcePack") -Destination ./artifacts/GameData -Recurse
Copy-Item -Path (Join-Path -Path $UsiCachePath -ChildPath "FX") -Destination ./artifacts/GameData/UmbraSpaceIndustries -Recurse
'''
script {
env.ARCHIVE_FILENAME = "USI-LS_${env.GITVERSION_SEMVER}.zip"
zip dir: "artifacts", zipFile: "${env.ARCHIVE_FILENAME}", archive: true
}
}
}
// Tag commit, if necessary
stage("Tag commit") {
steps {
powershell '''
Write-Output "Looking for tag $env:PUBLISH_TAG..."
$tagFound = git tag -l "$env:PUBLISH_TAG"
if ( $tagFound -ne $env:PUBLISH_TAG )
{
Write-Output "Tag not found. Creating tag..."
git tag -a $env:PUBLISH_TAG -m "$env:TAG_PREFIX $env:GITVERSION_SEMVER"
Write-Output "Pushing tag to GitHub..."
git push --tags
}
'''
}
}
// Push artifacts to GitHub
stage("Push release artifacts to GitHub") {
when {
environment name: "AUTO_PUBLISH", value: "true"
}
steps {
powershell '''
echo "Creating release on GitHub..."
$RepoUrl = [uri]$env:GIT_URL
$RepoPath = $RepoUrl.LocalPath.Replace(".git", "")
$Url = "https://api.github.com/repos$RepoPath/releases"
$Headers = @{
"Accept" = "application/vnd.github.v3+json"
"Authorization" = "token $env:GITHUB_TOKEN"
}
$Body = @{
tag_name = "$env:PUBLISH_TAG"
name = "$env:TAG_PREFIX $env:GITVERSION_SEMVER"
prerelease = ($env:IS_PRERELEASE -eq "true")
}
$Json = ConvertTo-Json $Body
$Response = Invoke-WebRequest -Method Post -Uri $Url -Headers $Headers `
-ContentType "application/json" -Body $Json -UseBasicParsing
if ( $Response.StatusCode -ge 300 ) {
Write-Output "Could not create GitHub Release"
Write-Output "Status Code: $Response.StatusCode"
Write-Output $Response.Content
throw $Response.StatusCode
} else {
Write-Output "GitHub API replied with status code: $($Response.StatusCode)"
Write-Output ($Response.Content | ConvertFrom-Json | ConvertTo-Json -Depth 100)
}
echo "Uploading artifacts to GitHub..."
$ReleaseMetadata = ConvertFrom-Json $Response.Content
$UploadUrl = $ReleaseMetadata | Select -ExpandProperty "upload_url"
$UploadUrl = $UploadUrl.Replace("{?name,label}", "?name=$env:ARCHIVE_FILENAME")
$Response = Invoke-WebRequest -Method Post -Uri $UploadUrl -Headers $Headers `
-ContentType "application/zip" -InFile $env:ARCHIVE_FILENAME -UseBasicParsing
if ( $Response.StatusCode -ge 300 ) {
Write-Output "Could not upload artifacts to GitHub"
Write-Output "Status Code: $Response.StatusCode"
Write-Output $Response.Content
throw $Response.StatusCode
} else {
Write-Output "GitHub API replied with status code: $($Response.StatusCode)"
Write-Output ($Response.Content | ConvertFrom-Json | ConvertTo-Json -Depth 100)
}
'''
}
}
}
}