git Jenkins 构建使用变量 ${GIT_BRANCH} 作为 sonarqube 参数而没有“origin/”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21936733/
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
Jenkins build using variable ${GIT_BRANCH} as sonarqube parameter without "origin/"
提问by Jonas
Large team with separate git branches per team and sonar as a code quality checker.
大型团队,每个团队都有单独的 git 分支,声纳作为代码质量检查器。
The scm tool is set up to pick up every branch that corresponds to this name: 'feature-branch-*'
which works perfectly, for the maven build.
I wanted to set up sonarqube so it would use a different branch for every actual git branch, but sonarqube does not allow wildcards.
It does however allow variables so I tried ${GIT_BRANCH}
, but this variable holds: 'origin/feature-branch-214'
, which sonarqube does not recognise as a valid branch name. Sonarqube expects 'feature-branch-214'
scm 工具设置为选取与此名称对应的每个分支:'feature-branch-*'
对于 Maven 构建,它完美地工作。
我想设置 sonarqube,以便它为每个实际的 git 分支使用不同的分支,但 sonarqube 不允许使用通配符。然而,它确实允许变量,所以我尝试了${GIT_BRANCH}
,但是这个变量包含:'origin/feature-branch-214'
,它的声纳无法识别为有效的分支名称。Sonarqube 预计'feature-branch-214'
So I need something to put there (in sonarqube branch) that does a substring of the ${GIT_BRANCH}
to exclude the 'origin/'
part.
所以我需要一些东西放在那里(在 sonarqube 分支中),它执行的子字符串${GIT_BRANCH}
以排除该'origin/'
部分。
回答by Apostolos Emmanouilidis
You could create a new variable and assign the value:
您可以创建一个新变量并分配值:
`echo ${GIT_BRANCH} | cut -d'/' -f 2-`
Afterwards, you will be able to use the new variable in the Jenkins Git Branch SonarQube parameter, by following the below steps.
之后,您将能够按照以下步骤在 Jenkins Git Branch SonarQube 参数中使用新变量。
Steps
脚步
In build section add an Execute Shell
step with command
:
在构建部分添加一个Execute Shell
步骤command
:
echo NEW_VAR=`echo ${GIT_BRANCH} | cut -d'/' -f 2-` > newfile
Then add an Inject an environment variable
step with Properties File Path
:
然后添加一个Inject an environment variable
步骤Properties File Path
:
newfile
In SonarQube configuration leave the branch
field empty
and add the following in the Additional properties field
:
在 SonarQube 配置中离开该branch
字段empty
并在其中添加以下内容Additional properties field
:
-Dsonar.branch=${NEW_VAR}
The above solution is not very clean but I've verified that it works
上面的解决方案不是很干净,但我已经验证它有效
回答by Du?an
Or you could:
或者你可以:
echo NEW_VAR=${GIT_BRANCH#*/} > newfile
回答by victor1ee
git show-ref | grep $(git rev-parse HEAD) | grep remotes | grep -v HEAD | sed -e 's/.*remotes.origin.//'
回答by kingchad1989
The easiest way would be to use the Jenkins environmental variable GIT_LOCAL_BRANCH instead of GIT_BRANCH. You can see the full list of Jenkins environmental variables at http://[jenkins-url]/env-vars.html
最简单的方法是使用 Jenkins 环境变量 GIT_LOCAL_BRANCH 而不是 GIT_BRANCH。您可以在http://[jenkins-url]/env-vars.html查看 Jenkins 环境变量的完整列表