Skip to content

Commit acfaca9

Browse files
author
Michael Sauter
authored
Merge pull request #565 from SimonGolms/feature/skipping-commits
feat: improve skipping commits
2 parents 4cdd980 + eee89fa commit acfaca9

File tree

4 files changed

+64
-9
lines changed

4 files changed

+64
-9
lines changed

docs/modules/jenkins-shared-library/partials/odsComponentPipeline.adoc

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,4 +414,18 @@ def sayHello(def context) {
414414

415415
=== Skipping pipeline runs
416416

417-
If the message of the built commit contains `[ci skip]`, the pipeline is skipped. The Jenkins build status will be set to `NOT_BUILT`, the Bitbucket build status to `SUCCESSFUL` (as there is no "skipped" state). The pipeline will start to execute initially, but abort before launching any agent nodes or starting any of the stages defined in the `Jenkinsfile`.
417+
If the subject of the built commit message contains `[ci skip]`, `[skip ci]` or `\\***NO_CI***`, the pipeline is skipped.
418+
[source,sh]
419+
skip
420+
----
421+
# skip pipeline (one-line commit)
422+
$ git commit -m "docs: update README [ci skip]"
423+
424+
# run pipeline (multi-line commit) as it is not part of the subject
425+
$ git commit -m "docs: update README
426+
427+
- add section installation
428+
- [ci skip]"
429+
----
430+
431+
The Jenkins build status will be set to `NOT_BUILT`, the Bitbucket build status to `SUCCESSFUL` (as there is no "skipped" state). The pipeline will start to execute initially, but abort before launching any agent nodes or starting any of the stages defined in the `Jenkinsfile`.

src/org/ods/component/Pipeline.groovy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ class Pipeline implements Serializable {
149149

150150
skipCi = isCiSkip()
151151
if (skipCi) {
152-
logger.info 'Skipping build due to [ci skip] in the commit message ...'
152+
logger.info 'Skipping build due to [ci skip], [skip ci] or ***NO_CI***' +
153+
'in the commit message ...'
153154
updateBuildStatus('NOT_BUILT')
154155
setBitbucketBuildStatus('SUCCESSFUL')
155156
return

src/org/ods/services/GitService.groovy

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ class GitService {
108108
).trim()
109109
}
110110

111+
String getCommitSubject() {
112+
script.sh(
113+
returnStdout: true,
114+
script: 'git show --pretty=%s -s',
115+
label: 'Get Git commit subject'
116+
).trim()
117+
}
118+
111119
String getCommitMessage() {
112120
script.sh(
113121
returnStdout: true,
@@ -124,12 +132,25 @@ class GitService {
124132
).trim()
125133
}
126134

127-
/** Looks in commit message for string '[ci skip]', '[ciskip]', '[ci-skip]' and '[ci_skip]'. */
128-
boolean isCiSkipInCommitMessage() {
129-
return script.sh(
130-
returnStdout: true, script: 'git show --pretty=%s%b -s',
131-
label: 'check skip CI?'
132-
).toLowerCase().replaceAll('[\\s\\-\\_]', '').contains('[ciskip]')
135+
/** Looks in commit message for the following strings
136+
* '[ci skip]', '[ciskip]', '[ci-skip]', '[ci_skip]',
137+
* '[skip ci]', '[skipci]', '[skip-ci]', '[skip_ci]',
138+
* '***NO_CI***', '***NO CI***', '***NOCI***', '***NO-CI***'
139+
*/
140+
boolean isCiSkipInCommitMessage(String gitCommit = '') {
141+
def gitCommitSubject = ''
142+
if (gitCommit) {
143+
def indexEndOfLine = gitCommit.indexOf('\n')
144+
gitCommitSubject = gitCommit[0..indexEndOfLine]
145+
} else {
146+
gitCommitSubject = getCommitSubject()
147+
}
148+
149+
gitCommitSubject = gitCommitSubject.toLowerCase().replaceAll('[\\s\\-\\_]', '')
150+
151+
return (gitCommitSubject.contains('[ciskip]')
152+
|| gitCommitSubject.contains('[skipci]')
153+
|| gitCommitSubject.contains('***noci***'))
133154
}
134155

135156
void checkout(String gitCommit, def userRemoteConfigs) {

test/groovy/org/ods/services/GitServiceSpec.groovy

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,24 @@ class GitServiceSpec extends SpecHelper {
1818
then:
1919
releaseBranch == "release/0.0.1"
2020
}
21-
}
2221

22+
def "git skipping commit message"() {
23+
given:
24+
def script = new PipelineScript()
25+
def service = new GitService(script, new Logger(script, false))
26+
27+
when:
28+
def result = service.isCiSkipInCommitMessage(gitCommitMessage)
29+
30+
then:
31+
result == isSkippingCommitMessage
32+
33+
where:
34+
gitCommitMessage || isSkippingCommitMessage
35+
'docs: update README [ci skip]' || true
36+
'docs: update README [skip ci]' || true
37+
'docs: update README ***NO_CI***' || true
38+
'docs: update README' || false
39+
'docs: update README\n\n- typo\n- [ci skip]' || false
40+
}
41+
}

0 commit comments

Comments
 (0)