gpt4 book ai didi

java - 使用 Jenkinsfile 和 maven 对 pullRequest 的代码启动声纳分析

转载 作者:搜寻专家 更新时间:2023-10-30 20:54:28 24 4
gpt4 key购买 nike

目前在我的项目中,organization-repository 上的每个 pull-Request 都是由 Jenkins 自动构建的,如 jenkinsfile 中指定的那样。当构建结束时,Jenkins 会向 github 发送一条消息,其中包含该项目的构建状态。

我想发送一个 Sonar 分析到 pull-request 的对话,但只针对已被 pull-request 更新的文件/代码。

赏金信息:

  • 它需要使用 jenkinsFile(在您的回复中添加完整的 jenkinsfile 将不胜感激)
  • 结果应该出现在github的pullRequest页面,只有pullRequest更新的代码。

最佳答案

由于您在 10 个月内没有收到答复,我将尽我所能提供帮助这是我在 GitLab 上的工作示例,但您应该能够更改它,因为插件是相似的 (https://wiki.jenkins.io/display/JENKINS/GitHub+Plugin#GitHubPlugin-Settingcommitstatus):

#!groovy

pipeline {
options {
buildDiscarder(
logRotator(artifactDaysToKeepStr: '21', artifactNumToKeepStr: '4', daysToKeepStr: '21', numToKeepStr: '4')
)
gitLabConnection('GitLab')
}

agent any
tools {
maven 'Default Maven'
jdk 'DefaultJDK'
}

stages {
stage('Build') {
steps {
sh "mvn clean install -U"
}
}

stage('Source Code Analysis') {
steps {
withMaven() {
sh "mvn " +
"-Dsonar.branch='${env.BRANCH_NAME}' " +
"-Dsonar.analysis.mode=preview " +
"-Dsonar.gitlab.commit_sha=\$(git log --pretty=format:%H origin/master..'${env.BRANCH_NAME}' | tr '\\n' ',') " +
"-Dsonar.gitlab.ref_name='${env.BRANCH_NAME}' " +
"sonar:sonar"
}
withMaven() {
sh "mvn -Dsonar.branch='${env.BRANCH_NAME}' sonar:sonar"
}
}
}
}

post {
success {
echo 'posting success to GitLab'
updateGitlabCommitStatus(name: 'jenkins-build', state: 'success')
}
failure {
echo 'posting failure to GitLab'
updateGitlabCommitStatus(name: 'jenkins-build', state: 'failed')
}
always {
deleteDir()
}
}
}

这包括各种位,但涵盖了您正在尝试做的事情,声纳分析发生在两部分预览中(提交的评论和这些评论在打开时转移到合并请求),然后是正常的分析后记

在项目 pom 中我还定义了:

<sonar.gitlab.project_id>${gitlab.project_id}</sonar.gitlab.project_id>
<sonar.gitlab.unique_issue_per_inline>true</sonar.gitlab.unique_issue_per_inline>
<sonar.gitlab.user_token>GITLAB_USER_TOKEN</sonar.gitlab.user_token>
<sonar.gitlab.url>${git.hostname.url}</sonar.gitlab.url>

如果您添加这些并替换缺失的部分,我相信这会解决您的问题。

编辑:我相信您需要 github 的以下选项而不是 GitLab 选项:

-Dsonar.analysis.mode=preview \
-Dsonar.github.pullRequest=$PULL_REQUEST_ID \
-Dsonar.github.repository=myOrganisation/myProject \
-Dsonar.github.oauth=$GITHUB_ACCESS_TOKEN \
-Dsonar.host.url=https://server/sonarqube \
-Dsonar.login=$SONARQUBE_ACCESS_TOKEN

关于java - 使用 Jenkinsfile 和 maven 对 pullRequest 的代码启动声纳分析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45404161/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com