Jenkins 使用变量 ${GIT_BRANCH} 作为 sonarqube 参数构建,没有“origin/" [英] Jenkins build using variable ${GIT_BRANCH} as sonarqube parameter without "origin/"

查看:66
本文介绍了Jenkins 使用变量 ${GIT_BRANCH} 作为 sonarqube 参数构建,没有“origin/"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大型团队,每个团队都有单独的 git 分支,声纳作为代码质量检查器.

Large team with separate git branches per team and sonar as a code quality checker.

scm 工具设置为拾取与此名称相对应的每个分支:'feature-branch-*' 完美运行,适用于 maven 构建.
我想设置 sonarqube,以便它为每个实际的 git 分支使用不同的分支,但 sonarqube 不允许使用通配符.但是它确实允许变量,所以我尝试了 ${GIT_BRANCH},但这个变量包含:'origin/feature-branch-214',sonarqube 无法识别为有效分店名称.Sonarqube 需要 'feature-branch-214'

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'

所以我需要放一些东西(在 sonarqube 分支中),它会执行 ${GIT_BRANCH} 的子字符串以排除 'origin/' 部分.

So I need something to put there (in sonarqube branch) that does a substring of the ${GIT_BRANCH} to exclude the 'origin/' part.

推荐答案

你可以创建一个新变量并赋值:

You could create a new variable and assign the value:

`echo ${GIT_BRANCH} | cut -d'/' -f 2-`

之后,您将能够按照以下步骤在 Jenkins Git Branch SonarQube 参数中使用新变量.

Afterwards, you will be able to use the new variable in the Jenkins Git Branch SonarQube parameter, by following the below steps.

步骤

在构建部分添加一个带有 commandExecute Shell 步骤:

In build section add an Execute Shell step with command:

echo NEW_VAR=`echo ${GIT_BRANCH} | cut -d'/' -f 2-` > newfile

然后用Properties File Path添加一个Inject an environment variable步骤:

newfile

在 SonarQube 配置中,将 branch 字段 empty 并在 Additional properties field 中添加以下内容:

In SonarQube configuration leave the branch field empty and add the following in the Additional properties field:

-Dsonar.branch=${NEW_VAR}

上面的解决方案不是很干净,但我已经验证它可以工作

这篇关于Jenkins 使用变量 ${GIT_BRANCH} 作为 sonarqube 参数构建,没有“origin/"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆