最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

git - JenkinsSonarqube: Fetch target branch in multibranch pipeline? - Stack Overflow

programmeradmin0浏览0评论

I am running a Jenkins a multibranch pipeline with SonarQube scanner. It looks like the scanner requires target branch of the PR to be fetched in order to run the analysis but Jenkins is only fetching the pull request branch(feature/add-jenkins in below example).

...
 > git fetch --no-tags --force --progress -- .git +refs/heads/feature/add-jenkins:refs/remotes/origin/feature/add-jenkins # timeout=10
...

My Jenkins pipeline looks like this:

pipeline {
    agent any
    environment {
        SONAR_TOKEN = credentials('sonar-token')
    }
    stages {
        stage('Build') {
            steps {
                echo 'Building..'
            }
        }
        stage('Test') {
            steps {
                sh '''
                    printenv
                    ./gradlew sonar -Dsonar.pullrequest.key=4 -Dsonar.pullrequest.branch=feature/add-jenkins -Dsonar.pullrequest.base=main
                '''
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying....'
            }
        }
    }
}

How can I tell Jenkins to pull the PR's target branch also?

I am running a Jenkins a multibranch pipeline with SonarQube scanner. It looks like the scanner requires target branch of the PR to be fetched in order to run the analysis but Jenkins is only fetching the pull request branch(feature/add-jenkins in below example).

...
 > git fetch --no-tags --force --progress -- https://github/my-user/sonar-test.git +refs/heads/feature/add-jenkins:refs/remotes/origin/feature/add-jenkins # timeout=10
...

My Jenkins pipeline looks like this:

pipeline {
    agent any
    environment {
        SONAR_TOKEN = credentials('sonar-token')
    }
    stages {
        stage('Build') {
            steps {
                echo 'Building..'
            }
        }
        stage('Test') {
            steps {
                sh '''
                    printenv
                    ./gradlew sonar -Dsonar.pullrequest.key=4 -Dsonar.pullrequest.branch=feature/add-jenkins -Dsonar.pullrequest.base=main
                '''
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying....'
            }
        }
    }
}

How can I tell Jenkins to pull the PR's target branch also?

Share Improve this question edited Mar 7 at 8:39 seenukarthi 8,68410 gold badges50 silver badges74 bronze badges asked Mar 6 at 2:41 PepriaPepria 4061 gold badge9 silver badges25 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

You don't need to if you configure the multibranch pipeline properly.

From Setting up the pull request analysis section of SonarQube documentation:

The SonarScanner can automatically detect the pull request parameters when running on the following CI services (you don’t need to perform any additional setup):

  • Azure Pipelines
  • Bitbucket Pipelines
  • Cirrus CI
  • Codemagic
  • GitHub Actions
  • GitLab CI/CD
  • Jenkins (with the Branch Source plugin configured)

The documentation also emphasizes that the autodetection doesn't work if you set the parameters by yourself:

Manually setting pull request parameters overrides automatic detection.

Regarding your statement:

Jenkins is only fetching the pull request branch

If it's the only log entry about fetching, chances are that you didn't actually configure yur multibranch pipeline to detect pull requests. This part is also covered by SonarQube docs:

From your Jenkins job, go to Configure > Branch Sources > Behaviors and:

  • Under Discover branches, make sure Exclude branches that are also filed as PRs (or MRs) is selected.
  • Under Discover pull (or merge) requests from origin, make sure The current pull (or merge) request revision is selected.
  • Under Specify ref specs, make sure the Ref Spec value will include any target branches (the default value should be enough).
  • If the Specify ref specs behavior is not active, click on Add and select Specify ref specs.

Your settings could include some other parameters as well, if they don't conflict with the mentioned ones, but these are essential.

发布评论

评论列表(0)

  1. 暂无评论