Skip to content

Commit 20a37eb

Browse files
author
R. Tyler Croy
authored
Merge pull request jenkinsci#43 from mkobit/git-sha-with-sh-step
Switch Git commit example to using `sh` step and using `returnStdout`
2 parents bc1cdf1 + 51aa88c commit 20a37eb

File tree

2 files changed

+6
-11
lines changed

2 files changed

+6
-11
lines changed

pipeline-examples/gitcommit/README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# Synopsis
2+
23
Demonstrate how to expose the git_commit to a Pipeline job.
34

45
# Background
56

67
The git plugin exposes some environment variables to a freestyle job that are not currently exposed to a Pipeline job.
7-
Here's how to recover that ability using a git command and Pipeline's readFile() function.
8+
Here's how to recover that ability using a git command and Pipeline's [`sh`][pipeline-sh-documentation] step.
9+
10+
[pipeline-sh-documentation]: https://jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#sh-shell-script

pipeline-examples/gitcommit/gitcommit.groovy

+2-10
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,6 @@
22
// checked out your sources on the slave. A 'git' executable
33
// must be available.
44
// Most typical, if you're not cloning into a sub directory
5-
sh('git rev-parse HEAD > GIT_COMMIT')
6-
git_commit=readFile('GIT_COMMIT')
5+
gitCommit = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
76
// short SHA, possibly better for chat notifications, etc.
8-
short_commit=git_commit.take(6)
9-
10-
//create a GIT_COMMIT file in workspace and read back into a string in Pipeline
11-
// If you have your sources checked out in a 'src' subdir
12-
sh('cd src && git rev-parse HEAD > GIT_COMMIT')
13-
git_commit=readFile('src/GIT_COMMIT')
14-
// short SHA, possibly better for chat notifications, etc.
15-
short_commit=git_commit.take(6)
7+
shortCommit = gitCommit.take(6)

0 commit comments

Comments
 (0)