Ben
Ben

Reputation: 57207

Jenkins substring environment variable

I'm using Jenkins and the Git plugin which offers the following environment variables:

  • GIT_COMMIT: SHA of the current
  • GIT_BRANCH: Name of the branch currently being used, e.g. "master" or "origin/foo"
  • GIT_PREVIOUS_COMMIT: SHA of the previous built commit from the same branch (the current SHA on first build in branch)
  • GIT_URL: Repository remote URL
  • GIT_URL_N: Repository remote URLs when there are more than 1 remotes, e.g. GIT_URL_1, GIT_URL_2
  • GIT_AUTHOR_EMAIL: Committer/Author Email
  • GIT_COMMITTER_EMAIL: Committer/Author Email

Using the Version Number Plugin, I've got a ${GIT_COMMIT} variable which is read as expected.

The problem is, it's the full 32 (?) character SHA hash. I'd like to take a substring of it. Is there a way to take a substring of an environment variable in Jenkins?

Upvotes: 2

Views: 12031

Answers (1)

Akos Bannerth
Akos Bannerth

Reputation: 1984

In bash (I assume that's what you want to know since you tagged the question with it), you can do it by using this syntax: ${string:start_position:length}

You could extract e.g. the first 8 characters of the hash by writing:

${GIT_COMMIT:0:8}

I've taken this solution from here: http://tldp.org/LDP/abs/html/string-manipulation.htm

Upvotes: 9

Related Questions