Because I don't know if Sonar is doing something strange, I just wanted to confirm that it is indeed scanning the master branch and not the feature branch.
People on my team are saying SonarCube ran the test against the feature branch. As near as I can figure, it ran the test on the master branch. We are using Sonar 7.9.1 and my intent is to run against the feature branch.
Jenkinsfile snippet:
stage('Send sonarqube reports') {
sh """
sonar-scanner -Dsonar.host.url=${SONAR_CIRRUS} -Dsonar.java.libraries=/home/bcjenkins/.m2/repository/org/projectlombok/lombok/1.18.8/lombok-1.18.8.jar
"""
JSON_STRING_SONAR_METRICS = sh (
script: "curl -X GET -H \'Content-Type: application/json\' -H \'Accept: application/octet-stream\' \'${SONAR_CIRRUS}/api/qualitygates/project_status?projectKey=cirrus-bluecost-ssc-file-generator\'",
returnStdout: true
).trim()
println "The JSON_STRING_SONAR_METRICS=${JSON_STRING_SONAR_METRICS}";
def sonarQube_Results = readJSON text: "${JSON_STRING_SONAR_METRICS}";
def CodeCoverageEntryThreshold = sonarQube_Results.projectStatus.status;
if(CodeCoverageEntryThreshold == "ERROR"){
error("[cirrus-bluecost-ssc-file-generator] Code qualitygates did not PASSED. Please check the results in our Sonarqube server.\n" +
"\tTo view the sonarqube results-> ${SONAR_CIRRUS}/dashboard?id=cirrus-bluecost-ssc-file-generator\n")
}
else if(CodeCoverageEntryThreshold == "FAILED"){
error("[cirrus-bluecost-ssc-file-generator] Code qualitygates did not PASSED. Please check the results in our Sonarqube server.\n" +
"\tTo view the sonarqube results-> ${SONAR_CIRRUS}/dashboard?id=cirrus-bluecost-ssc-file-generator\n")
}
else {
CodeCoverage_resultMsg = "[cirrus-bluecost-ssc-file-generator] Code qualitygates PASSED. Please check the results in our Sonarqube server.\n" +
"\tTo view the sonarqube results-> ${SONAR_CIRRUS}/dashboard?id=cirrus-bluecost-ssc-file-generator\n";
slackSend channel: "${SONAR_SLACK_CHANNEL}", color: 'good', failOnError: false, message: "${CodeCoverage_resultMsg}", teamDomain: 'ibm-ic2e-sprint', tokenCredentialId: SLACK_DOMAIN_CREDENTIALS
}
}
Regarding to the https://docs.sonarqube.org/latest/branches/overview/
Master / Main Branch
This is the default branch and typically corresponds to what's being
developed for your next release. This branch is usually known within a
development team as "master" or "head" and is analyzed when no
specific branch parameters are provided. SonarQube labels this branch
as Main Branch, and, with Community Edition, this is the only branch
you can analyze.
If we use the SonarQube Community Edition, we are able to analyze the main or mater branch only.
If we need to analyze another branches, we should use at least the SonarQube Developer Edition as the following mentioned: -
Branch Analysis
Branch analysis is available starting in Developer Edition.
Edit 1:
The SonarQube provide the SonarLint (https://www.sonarlint.org/) as an IDE extension, including with Eclipse, IntelliJ IDEA and so on.
We are able to configure our IDE to connect to the in-house SonarQube server and use this plugin to perform a local analyzing at the developer machine before merging them to the master or main branch.
Edit 2:
The example for configuring the SonarLint with Eclipse is at https://github.com/SonarSource/sonarlint-eclipse/wiki/Connected-Mode
Related
This is my Jenkinsfile:
pipeline {
agent { docker { image 'node:10.16' } }
stages {
stage('PR To Dev') {
when {
changeRequest target: 'dev'
}
steps {
sh 'npm install'
sh 'npm run lint'
}
}
}
}
I'm trying to run linting upon every PR made (on Github). This pipeline works and runs as intended when I make the initial PR to the dev branch. However, subsequent commits to the open PR are ignored by Jenkins, which defeats the usefulness of the initial lint check. How can I configure Jenkins to lint upon any updates to a branch that has an open PR to the dev (or any arbitrary) branch?
Achieving this goal is possible. It depends greatly on the plugin that you are using to integrate GitHub with Jenkins, and how you configure GitHub to use Jenkins' webhooks.
On the GitHub end, you can configure to trigger the webhook on different events. Default config is Push events (to any branch, whether on PR or not), All events (these can have many false positives), and the option to Select individual events (find your right balance between events coverage and false positives)
On the Jenkins end, some plugins will offer more customization options to discard unnecessary triggers, for example to avoid triggering a project on PR updates of the title or description (instead of code), etc.
I, personally, use the Generic Webhook Plugin on the Jenkins end and then I analyze the json of the webhook to determine whether to run a job or not
We are currently using the Jenkins/Groovy pipeline method for CI.
I'm trying to create a single pipeline for building packages and running unit tests on a branch (and let me know if this is bad practice).
The problem is on a commit I don't want to execute my test steps (due to the large number of commits and time to execute the full pipeline), but I still want the packaging stage to run for our manual testers to be able to pull and install on instances.
Is there any way to distinguish between a run for a PR vs a commit in the pipeline steps or in the job configuration?
Using the (assuming github, but theres bitbucket equiv as well)
https://wiki.jenkins.io/display/JENKINS/GitHub+Branch+Source+Plugin to discover Repositories to build branches and PR's will allow you to fall back on Jenkins ENV variables.
This allows simple if statement to determine if the build is for a branch, or for a PR, as PR's are built on a 'branch' of PR-n
Once a PR is open however, all commits would be built.
https://go.cloudbees.com/docs/cloudbees-documentation/cje-user-guide/index.html#github-branch-source
you can use the changeRequest built-in condition from Jenkins.
Executes the stage if the current build is for a "change request" (a.k.a. Pull Request on GitHub and Bitbucket, Merge Request on GitLab, Change in Gerrit, etc.). When no parameters are passed the stage runs on every change request, for example: when { changeRequest() }.
stage('Run only for pull requests to master branch or at the master branch') {
when {
anyOf {
branch 'master'
changeRequest target: 'master'
}
}
steps {
// this is a very long step for integration test that we don't want to execute often, but we need to execute before to merge to master
sh "${mvn} " +
"clean " +
"test-compile " +
"failsafe:integration-test failsafe:verify"
}
}
I'm currently having a hard time figuring out how to check out a specific changeset using the build in cm command in a Jenkins Pipeline build. It seems that the changeset = 1234 parameter is ignored.
I tried the statement:
cm repository: item['Repo'], changeset: Stable_CS, server: item['server'], useUpdate: false, workspaceName: item['Repo']
Stable_CS is a variable that is filled with another cm command using the bat pipeline statement. It contains a string like 1234
Thanks very much in advance
I'm afraid that Plastic SCM plugin for Jenkins doesn't currently support checking out specific changesets, neither in freestyle projects nor pipelines. It can only target branches, as seen in the Plugin wiki page.
For one of my projects that I have on GitHub, I wanted to build it as a docker image and push it to my docker hub. The project is a sbt one with a Scala codebase.
Here is how my JenkinsFile is defined:
#!groovy
node {
// set this in Jenkins server under Manage Jenkins > Credentials > System > Global Credentials
docker.withRegistry('https://hub.docker.com/', 'joesan-docker-hub-credentials') {
git credentialsId: '630bd271-01e7-48c3-bc5f-5df059c1abb8', url: 'https://github.com/joesan/monix-samples.git'
sh "git rev-parse HEAD > .git/commit-id"
def commit_id = readFile('.git/commit-id').trim()
println comit_id
stage "build" {
def app = docker.build "Monix-Sample"
}
stage "publish" {
app.push 'master'
app.push "${commit_id}"
}
}
}
When I tried to run this from my Jenkins server, I get the following error:
java.io.FileNotFoundException
at jenkins.plugins.git.GitSCMFile$3.invoke(GitSCMFile.java:167)
at jenkins.plugins.git.GitSCMFile$3.invoke(GitSCMFile.java:159)
at jenkins.plugins.git.GitSCMFileSystem$3.invoke(GitSCMFileSystem.java:161)
at org.jenkinsci.plugins.gitclient.AbstractGitAPIImpl.withRepository(AbstractGitAPIImpl.java:29)
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.withRepository(CliGitAPIImpl.java:65)
at jenkins.plugins.git.GitSCMFileSystem.invoke(GitSCMFileSystem.java:157)
at jenkins.plugins.git.GitSCMFile.content(GitSCMFile.java:159)
at jenkins.scm.api.SCMFile.contentAsString(SCMFile.java:338)
at org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition.create(CpsScmFlowDefinition.java:101)
at org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition.create(CpsScmFlowDefinition.java:59)
at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:232)
at hudson.model.ResourceController.execute(ResourceController.java:98)
at hudson.model.Executor.run(Executor.java:404)
Finished: FAILURE
Since this is running inside a VM on Azure, I thought the VM was not able to reach outside, but that seems not to be the case as I was able to ssh into the VM and git pull from the Git repo. So what is the problem here? How could I make this work?
for me unchecking "lightweight checkout" fixed the issue
I experienced the exact same error. My setting:
Pipeline build inside a dockerized Jenkins (version 2.32.3)
In the configuration of the job, I specified a check out into a subdirectory: Open the configuration, e.g. https://myJenkins/job/my-job/configure. At the bottom, see section Pipeline -> Additional Behaviours -> Check out into a sub-directory with Local subdirectory for repo set to, e.g., my-sub-dir.
Expectation: Upon check out, the Jenkinsfile ends up in my-sub-dir/Jenkinsfile.
Via the option Script path, you configure the location of the Jenkinsfile so that Jenkins can start the build. I put my-sub-dir/Jenkinsfile as value.
I then received the exception you pasted in your question. I fixed it by setting Script Path to Jenkinsfile. If you don't specify a sub-directory for check out, then still try double checking values for Script Path.
Note: I have another Jenkins instance at work. There I have to specify Script Path including the custom check out sub-directory (as mentioned in Expectation above).
GO TO Job-->Config-->Pipline and uncheck checkbox lightweight checkout"
lightweight checkout : selected, try to obtain the Pipeline script contents >directly from
the SCM without performing a full checkout. The advantage of this mode
is its efficiency; however, you will not get any changelogs or polling
based on the SCM. (If you use checkout scm during the build, this will
populate the changelog and initialize polling.) Also build parameters
will not be substituted into SCM configuration in this mode. Only
selected SCM plugins support this mode.
I installed the Feature Branch Notifier Plugin in my instance of Jenkins.
I have checked the "Show full length branch name in the build history view" checkbox at jenkins:8080/configure
I am expecting to see the branch names in build history view, but even after restarting Jenkins I am not seeing the branch names in the build history, as can be seen in the enclosed image.
The project issue queue lists no open issues, and when I try to log in to post an issue, I get the message "Proxy Error - The proxy server received an invalid response from an upstream server. The proxy server could not handle the request POST /account/doSignup. Reason: Error reading from remote server Apache/2.2.14 (Ubuntu) Server at jenkins-ci.org Port 443"
Does anyone know how to go about seeing the branch name of builds in the build history view of Jenkins? Thanks!
Albert.
You can use Build Name Setter Plugin, and set Set Build Name something like #${BUILD_NUMBER} - ${GIT_BRANCH}.
Build-Name-setter-plugin no longer works. I tried on 2.319.1, and the setting never appears in the pipline.
The solution I found is to use the build environment variables to apply to your display name for the build in a step script.
Adjust your Jenkinsfile to pull the branch name as a environmental variable (I am using CURRENT_BRANCH_NAME). Then I created a new stage / step, that runs before any other, and ran a script to adjust the displayname there:
pipeline {
agent {any}
environment {
CURRENT_BRANCH_NAME = "${GIT_BRANCH.split('/').size() > 1 ? GIT_BRANCH.split('/')[1..-1].join('/') : GIT_BRANCH}"
}
stages {
stage('Set branch name') {
steps {
script{
currentBuild.displayName = "#"+currentBuild.number+": "+CURRENT_BRANCH_NAME
}
}
}
stages {
stage('Ok now start doing testing') {
steps {
sh '''#!/bin/bash
echo "Im gona test everything"
'''
}
}
}
}
Now when your Jenkins test starts to build, the name will update once the step is complete.
Note: this solution was tested in a single pipeline (not multi-pipeline), and was for a SCM repo integration.
Sources:
Get git branch name in Jenkins Pipeline/Jenkinsfile
https://sleeplessbeastie.eu/2021/01/29/how-to-define-build-name-and-description-in-jenkins/