简体   繁体   中英

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, eg "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, eg 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?

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 eg 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM