Skip to content

Commit 4d6c509

Browse files
committed
Merge pull request #3 from artstorm/feature/git-trailers
Add git.getTrailerValue() function
2 parents 882ef1e + 2f48227 commit 4d6c509

File tree

3 files changed

+145
-36
lines changed

3 files changed

+145
-36
lines changed

README.md

Lines changed: 121 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,66 @@
66

77
A Jenkins shared library with a collection of pipeline steps and functionality useful when setting up a Jenkins CI pipeline for Unity projects. In addition to Unity specific functionality, there is also a selection of more generic functions and steps that have proven useful when setting up continuous integration for game dev projects.
88

9+
## Available Functions
10+
11+
### Unity Specific
12+
13+
| Function | Summary |
14+
| ------------------------------------------------------------------------------ | ----------------------------------------------------------------- |
15+
| [unityBuildNumber](#unity-build-number) | Get the build number set for the Standalone build in Unity. |
16+
| [unityCodeCoverageReport](#unity-code-coverage-report) | Runs Unity's Code Coverage reporter. |
17+
| [unityCodeCoverageReportSummary](#unity-code-coverage-report-summary) | Parses Summary.xml from the Unity generated code coverage report. |
18+
| [unityPublishCodeCoverageHTMLReport](#unity-publish-code-coverage-html-report) | Publishes the Unity Code Coverage Report to the job page. |
19+
| [unityTestRunner](#unity-test-runner) | Uses Unity's Test Runner to execute the test suite. |
20+
| [unityTestRunnerReport](#unity-test-runner-report) | Create a report from NUnit tests. |
21+
| [tests.summary](#tests) | Get Junit test result summary. |
22+
23+
### Generic
24+
25+
| Function | Summary |
26+
| --------------------------------------------------------- | ---------------------------------------------------------------------- |
27+
| [getBuildType](#get-build-type) | Retrieves the build type for the current running build. |
28+
| [setJobDisplayName](#set-job-display-name) | Sets the display name based on the build type. |
29+
| [git.branchName](#git-branch-name) | Get the current branch name. |
30+
| [git.commitSha](#git-commit-sha) | Get the full git commit sha for current commit. |
31+
| [git.commitShaShort](#git-commit-sha-short) | Get the short 7 character git commit sha for current commit. |
32+
| [git.getTrailerValue](#git-trailer-value) | Get the trailer value for the specified token. |
33+
| [github.pullRequestComment](#github-pull-request-comment) | Creates or updates the Jenkins bot issue comment for the pull request. |
34+
| [github.ownerRepo](#github-owner-repo) | Get the `owner/repo` part from the project's git url. |
35+
| [github.pullRequest](#github-pull-request) | Returns the pull request the current commit belongs to. |
36+
| [github.issueComments](#github-issue-comments) | Retrieves all comments for an issue/pull request. |
37+
| [github.createIssueComment](#github-create-issue-comment) | Create issue comment. |
38+
| [github.updateIssueComment](#github-update-issue-comment) | Update issue comment. |
39+
| [github.createCheckRun](#github-create-check-run) | Create a check run. |
40+
| [github.updateCheckRun](#github-update-check-run) | Update a check run. |
41+
942
## Installation
43+
1044
Read the [Extending with Shared Libraries > Using Libraries](https://www.jenkins.io/doc/book/pipeline/shared-libraries/#using-libraries) section in Jenkins User Handbook for full instruction how to add a shared library to a Jenkins instance and how to access the functionality from a `Jenkinsfile`.
1145

1246
When using the library in a Jenkinsfile, use a version specifier, to avoid surprises if future updates of the library introduces breaking changes.
1347

1448
```groovy
15-
@Library('shared-library@v1.0.3') _
49+
@Library('shared-library@v1.0.4') _
1650
```
1751

1852
### Pipeline Utility Steps
53+
1954
Several functions in this library uses functions from the Jenkins plugin [Pipeline Utility Steps](https://plugins.jenkins.io/pipeline-utility-steps/). Make sure this plugin is installed in the Jenkins instance using this shared library.
2055

2156
### GitHub App
22-
For the GitHub specific steps a GitHub app must be registered to obtain the access token for Jenkins to communicate with the GitHub API. It also requires the Jenkins plugins
57+
58+
For the GitHub specific steps a GitHub app must be registered to obtain the access token for Jenkins to communicate with the GitHub API. It also requires the Jenkins plugins
2359

2460
## Pipeline Steps and Functions
61+
2562
An overview of the pipeline steps and functionality this shared library exposes.
2663

2764
### Unity Specific
65+
2866
#### Unity Build Number
29-
This function parses Unity's Project Settings and returns the build number that has been set
67+
68+
This function parses Unity's Project Settings and returns the build number that has been set
3069
for the Standalone build in Unity's `Project Settings > Player > Build`.
3170

3271
```groovy
@@ -36,6 +75,7 @@ steps {
3675
```
3776

3877
#### Unity Code Coverage Report
78+
3979
Runs Unity's Code Coverage reporter. This assumes the Unity project has the Code Coverage package installed.
4080

4181
The reporter takes two parameters, `unityCodeCoverageReport(assemblyFilters, pathFilters)`.
@@ -48,23 +88,26 @@ post {
4888
}
4989
}
5090
```
91+
5192
#### Unity Code Coverage Report Summary
93+
5294
Parses `Summary.xml` from the Unity generated code coverage report and returns the summary as an object with the summary items as properties. This can then be posted to Discord, Slack, a time series database, or any other destination.
5395

5496
Available properties:
5597

56-
- coveredLines
57-
- uncoveredLines
58-
- coverableLines
59-
- totalLines
60-
- lineCoverage
98+
- coveredLines
99+
- uncoveredLines
100+
- coverableLines
101+
- totalLines
102+
- lineCoverage
61103

62104
```groovy
63105
def codeCoverage = unityCodeCoverageReportSummary()
64106
echo "${codeCoverage.lineCoverage}"
65107
```
66108

67109
#### Unity Publish Code Coverage HTML Report
110+
68111
Publishes the Unity Code Coverage Report so it's available from the job page.
69112

70113
```groovy
@@ -76,6 +119,7 @@ post {
76119
```
77120

78121
#### Unity Test Runner
122+
79123
Uses Unity's Test Runner to execute the test suite.
80124

81125
```groovy
@@ -85,6 +129,7 @@ steps {
85129
```
86130

87131
#### Unity Test Runner Report
132+
88133
Creates a merged report from Unitys PlayMode and EditMode test files.
89134

90135
```groovy
@@ -96,14 +141,15 @@ post {
96141
```
97142

98143
#### tests
144+
99145
Parses the results from the Unity test runner and returns the summary as an object. This can then be posted to Discord, Slack, a time series database, or any other destination.
100146

101147
Available properties:
102148

103-
- total
104-
- failed
105-
- skipped
106-
- passed
149+
- total
150+
- failed
151+
- skipped
152+
- passed
107153

108154
```groovy
109155
def testSummary = tests.summary()
@@ -113,29 +159,32 @@ echo "${testSummary.passed}"
113159
### Common
114160

115161
#### Get Build Type
116-
Retrieves the build type for the current running build based on branch name prefixes. This is useful to rely on branch names to determine the build logic.
117162

118-
- internal: `/feature/*`
119-
- testflight: `/testflight/*`
120-
- release: `/release/*`
121-
- standard: `*`
163+
Retrieves the build type for the current running build based on branch name prefixes. This is useful to rely on branch names to determine the build logic.
164+
165+
- internal: `/feature/*`
166+
- testflight: `/testflight/*`
167+
- release: `/release/*`
168+
- standard: `*`
122169

123170
```groovy
124171
steps {
125-
echo "Build type: ${git.getBuildType()}"
172+
echo "Build type: ${getBuildType()}"
126173
}
127174
```
128175

129-
130176
#### Set Job Display Name
177+
131178
Sets name for the running job based on git branch name.
179+
132180
```groovy
133181
steps {
134182
setJobDisplayName()
135183
}
136184
```
137185

138186
#### Git Branch Name
187+
139188
Get the current branch name.
140189

141190
```groovy
@@ -145,6 +194,7 @@ steps {
145194
```
146195

147196
#### Git Commit SHA
197+
148198
Get the full git commit sha for current commit.
149199

150200
```groovy
@@ -154,6 +204,7 @@ steps {
154204
```
155205

156206
#### Git Commit SHA Short
207+
157208
Get the short 7 character git commit sha for current commit.
158209

159210
```groovy
@@ -162,7 +213,18 @@ steps {
162213
}
163214
```
164215

216+
#### Git Trailer Value
217+
218+
Get the [trailer](https://git-scm.com/docs/git-interpret-trailers) value for the specified token for the current commit.
219+
220+
```groovy
221+
steps {
222+
echo "Git commit message trailer value : ${git.getTrailerValue('some-token')}"
223+
}
224+
```
225+
165226
#### GitHub Pull Request Comment
227+
166228
Creates or updates the Jenkins bot issue comment for the pull request.
167229

168230
This can be used to have a comment in the PR that Jenkins updates with current information about the build.
@@ -173,54 +235,60 @@ steps {
173235
}
174236
```
175237

176-
177238
#### GitHub Owner Repo
239+
178240
Get the <owner/repo> part from the project's git url.
179241

180242
```groovy
181243
def owner = github.ownerRepo()
182244
```
183245

184246
#### GitHub Pull Request
247+
185248
Returns the pull request the current commit belongs to.
186249

187250
```groovy
188251
def pr = github.pullRequest()
189252
```
190253

191254
#### GitHub Issue Comments
255+
192256
Retrieves all comments for an issue/pull request.
193257

194258
```groovy
195259
def comment = github.issueComments(42)
196260
```
197261

198262
#### GitHub Create Issue Comment
263+
199264
Create issue comment.
200265

201266
```groovy
202267
github.createIssueComment(42, "some comment")
203268
```
204269

205-
206270
#### GitHub Update Issue Comment
271+
207272
Update issue comment.
208273

209274
```groovy
210-
github.createIssueComment(42, "some updated comment")
275+
github.updateIssueComment(42, "some updated comment")
211276
```
212277

213278
#### GitHub Create Check Run
279+
214280
Create a check run.
215281

216282
Start a check run for a specific build build.
283+
217284
```groovy
218285
steps {
219286
checkRunIosId = github.createCheckRun("build_ios", "queued")
220287
}
221288
```
222289

223290
#### GitHub Update Check Run
291+
224292
Update a check run.
225293

226294
```groovy
@@ -236,4 +304,34 @@ success {
236304
github.updateCheckRun(checkRunIosId, '', 'success')
237305
}
238306
}
239-
```
307+
```
308+
309+
## Development
310+
311+
Gradle is used to test the pipeline during development.
312+
313+
### Running Tests
314+
315+
The test suite is executed with:
316+
317+
```sh
318+
./gradlew test
319+
```
320+
321+
Then gradle is executed for the first time in a session a daemon is started. Manage the daemon with these commands.
322+
323+
```sh
324+
## See running daemons
325+
gradle --status
326+
327+
## Stop running daemons
328+
gradle --stop
329+
```
330+
331+
### macOS
332+
333+
Gradle needs Java runtime, and the version of Gradle used in this project uses Java 17. Install Java 17 with Homebrew and then set `JAVA_HOME` in the terminal, for the session, in the terminal.
334+
335+
```
336+
export JAVA_HOME=/usr/local/opt/openjdk@17
337+
```

vars/git.groovy

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,14 @@ def branchName() {
3333
def commitShaShort() {
3434
return sh(returnStdout: true, script: "git rev-parse --short HEAD").trim();
3535
}
36+
37+
/**
38+
* Get the trailer value for the specified token for the current commit message.
39+
*
40+
* Resources:
41+
* - https://git-scm.com/docs/git-interpret-trailers
42+
* - https://stackoverflow.com/q/69532088/1152087
43+
*/
44+
def getTrailerValue(String token) {
45+
return sh(returnStdout: true, script: "git log -1 --pretty='%(trailers:key=${token},valueonly)'")
46+
}

vars/git.txt

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
<dl>
2-
<dt><code>git.branchName()</code></dt>
3-
<dd>
4-
Get the current branch name.
5-
</dd>
2+
<dt><code>git.branchName()</code></dt>
3+
<dd>Get the current branch name.</dd>
64

7-
<dt><code>git.commitSha()</code></dt>
8-
<dd>
9-
Get the full git commit sha for current commit.
10-
</dd>
5+
<dt><code>git.commitSha()</code></dt>
6+
<dd>Get the full git commit sha for current commit.</dd>
117

12-
<dt><code>git.commitShaShort()</code></dt>
13-
<dd>
14-
Get the short 7 character git commit sha for current commit.
15-
</dd>
16-
</dl>
8+
<dt><code>git.commitShaShort()</code></dt>
9+
<dd>Get the short 7 character git commit sha for current commit.</dd>
10+
11+
<dt><code>git.getTrailerValue(String token)</code></dt>
12+
<dd>
13+
Get the trailer value for the specified token for the current commit
14+
message.
15+
</dd>
16+
</dl>

0 commit comments

Comments
 (0)