bash Jenkins 子字符串环境变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24415579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:44:18  来源:igfitidea点击:

Jenkins substring environment variable

bashjenkinsenvironment-variablessubstring

提问by Ben

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

我正在使用 Jenkins 和提供以下环境变量的Git 插件

  • 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
  • GIT_COMMIT:当前的SHA
  • GIT_BRANCH:当前使用的分支名称,例如“master”或“origin/foo”
  • GIT_PREVIOUS_COMMIT:来自同一分支的先前构建提交的 SHA(第一个构建分支的当前 SHA)
  • GIT_URL:存储库远程 URL
  • GIT_URL_N:当有超过 1 个远程时的存储库远程 URL,例如 GIT_URL_1、GIT_URL_2
  • GIT_AUTHOR_EMAIL:提交者/作者电子邮件
  • GIT_COMMITTER_EMAIL:提交者/作者电子邮件

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

使用版本号插件,我有一个${GIT_COMMIT}按预期读取的变量。

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?

问题是,它是完整的 32 (?) 字符 SHA 哈希。我想取它的一个子串。有没有办法在 Jenkins 中获取环境变量的子字符串?

回答by Akos Bannerth

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}

在 bash 中(我假设这是你想知道的,因为你用它标记了问题),你可以使用以下语法来做到这一点: ${string:start_position:length}

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

您可以通过编写以下内容来提取例如散列的前 8 个字符:

${GIT_COMMIT:0:8}

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

我从这里采取了这个解决方案:http: //tldp.org/LDP/abs/html/string-manipulation.htm