Pipeline is not working with sonarQube with Jenkins and windows - sonarqube

Good afternoon, friends from Stack !
I am running a SonarQube in my pipeline in a jenkins instance. I have an issue and I'm following the documentation and I am kid of new to this.
https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner+for+Jenkins
But I have a Windows slaves. And everytime I configure it according to the documentation I get an error...
[Sonar-Pipeline] Running batch script
C:\Program Files (x86)\Jenkins\workspace\Sonar-Pipeline>C: \Program Files (x86)\Jenkins\tools \hudson.plugins.sonar.SonarRunnerInstallation\SONAR_RUNNER\bin\sonar-scanner
'C:\Program' not‚ recongnized as an internal or external command.
After which I beleive that there is a space here and Jenkins is trying to execute only 'C:Program ' as the above shows. Does any body now this?
This is my pipeline...
node {
stage('SonarQube analysis') {
// requires SonarQube Scanner 2.8+
def scannerHome = tool 'SONAR_RUNNER';
withSonarQubeEnv('SonarQube') {
bat "${scannerHome}/bin/sonar-scanner"
}
}
}
So this is what I am trying to execute according to the documentation. The only thing that is asked, because I am using only windows is to switch to bat instead of sh on scannerHome execution. Because it is a pipeline also and not a normal option. And I do have all the files too.

Please use below code to run the sonar-scanner on windows
node {
stage('SonarQube analysis') {
// requires SonarQube Scanner 2.8+
def scannerHome = tool 'SONAR_RUNNER';
withSonarQubeEnv('SonarQube') {
bat "\"${scannerHome}\\bin\\sonar-scanner.bat\""
}
}

Related

ElasTest Jenkins Plugin does not send test logs

Currently I am evaluating ElasTest. I also try the Jenkins Plguin, but the console output is not sent to ElasTest.
I have tried the example code snippet:
elastest(surefireReportsPattern: '**/target/surefire-reports/TEST-*.xml', project: 'Jenkins Examples') {
stage ('Executing Test') {
echo 'Set up test environment'
mvnHome = tool 'maven-3.3.9'
echo 'Cloning repository'
git 'https://github.com/elastest/demo-projects'
echo 'Run test'
sh "cd ./unit/junit5-unit-test;'${mvnHome}/bin/mvn' -B -DforkCount=0 test"
step([$class: 'JUnitResultArchiver', testResults: '**/target/surefire-reports/TEST-*.xml'])
}
}
I need to know a few things:
Which version of ElasTest you are using and where you are deploying it.
Are you using your own Jenkins instance or the Jenkins instance integrated into ElasTest?
If you are using a Jenkins instance of your own, make sure that Jenkins can communicate with ElasTest and configure the plugin correctly. If ElasTest is on a server, open the necessary ports as indicated in the documentation: https://elastest.io/docs/deploying/ubuntu/
You can find more information on how to use the ElasTest plugin here: https://elastest.io/docs/tutorials/e2e-testing/

Jenkins Pipeline emailext: How to access build object in pre-send script

I'm using Jenkins ver. 2.150.1 and have some freestyle jobs and some pipeline jobs.
In both job types I am using the emailext plugin, with template and pre-send scripts.
It seems that the build variable, which is available in the freestyle projects, is null in the pipeline projects.
The pre-send script is the following (just an example, my script is more complex):
msg.setSubject(msg.getSubject() + " [" + build.getUrl() + "]")
There is no problem with the msg variable.
In the freestyle job, this script adds the build url to the mail subject.
In the pipeline job, the following is given in the job console:
java.lang.NullPointerException: Cannot invoke method getUrl() on null object
The invocation of emailext in the pipeline job is:
emailext body: '${SCRIPT, template="groovy-html.custom.pipeline.sandbox.template"}',
presendScript: '${SCRIPT, template="presend.sandbox.groovy"}',
subject: '$DEFAULT_SUBJECT',
to: 'user#domain.com'
I would rather find a general solution to this problem (i.e. Access the build variable in a pipeline pre-send script), but would also appreciate any workarounds to my current needs:
Access job name, job number, and workspace folder in a pipeline pre-send script.
I have finally found the answer -
Apparently for presend script in pipeline jobs, the build object does not exist, and instead the run object does. At the time I posted this question this was still undocumented!
Found the answer in this thread
Which got the author to update the description in the wiki:
run - the build this message belongs to (may be used with FreeStyle or Pipeline jobs)
build - the build this message belongs to (only use with FreeStyle jobs)
You can access the build in a script like this:
// findUrl.groovy
def call(script) {
println script.currentBuild.rawBuild.url
// or if you just need the build url
println script.env.BUILD_URL
}
and would call the script like this from the pipeline:
stage('Get build URL') {
steps {
findUrl this
}
}
The currentBuild gives you a RunWrapper object and the rawBuild a Run. Hope this helps.

Jenkins loading external groovy script: powershell and bat steps hanging

I'm generating a Jenkins pipeline using a script. This pipeline is then loaded and executed. This works fine on any linux node and with sh statements etc.
I have now encountered an issue executing this on a windows node with the powershell and bat steps:
The checkout scm works fine, but any steps with the powershell or bat steps hang indefinitely. Copying the data from the generated file and replaying everything in a single Jenkinsfile works as expected.
An example of what is running (excluding the library):
"Main" Jenkinsfile:
def pod_label = "my_linux_node"
node(pod_label) {
stage("Checkout") {
checkout scm
}
stage("Pipeline generation") {
// genarate pipeline and save in "gen_pipeline.groovy"
}
stage("Run Pipeline") {
pipeline = load "gen_pipeline.groovy"
pipeline.run_pipeline()
}
}
Script1:
def run_pipeline(){
node('my_win_node') {
checkout scm
bat "echo foobar"
}
}
return this

Deploy Files to Windows Host Machines from Jenkins

I've Jenkins installed on a Windows server and I wishes to copy the published VisualStudio files to multiple Windows Hosts [LoadBalanced environment].
What is the most recommended way to copy files from Jenkins [hosted on windows] to multiple windows hosts, running on a LB farm. Is there any direct plugin for this?
If there any direct ways of copying the build files to destination servers, apart from multiple jenkins build steps?
If you use pipelines you can split a task into multiple tasks which can then be executed in parallel. You could do something like this:
pipeline {
agent none
stages {
stage('build') {
parallel {
stage('build-1') {
agent {
label "windows"
}
steps {
bat "build-1.bat"
}
}
stage('build-2') {
agent {
label "windows"
}
steps {
bat "build-2.bat"
}
}
stage('build-3') {
agent {
label "windows"
}
steps {
bat "build-3.bat"
}
}
}
}
}
}
The files would be copied from your SCM (Subversion, Git, etc) and the build scripts would be launched to perform whatever build actions you need to accomplish.

Jenkins ver. 2.121.3 - Delete file from workspace

In Jenkins ver. 2.121.3 using pipeline trying to delete the file. Its giving script not permitted error message.
Is there a alternate way to delete the file in Jenkins with-out using OS command?
Scripts not permitted to use method java.io.File delete. Administrators can decide whether to approve or reject this signature.
[Pipeline] End of Pipeline
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use method java.io.File delete
Pipeline code
stage('Delete test.zip file') {
if (fileExists('test.zip')) {
new File('test.zip').delete()
} else {
println "test.zip file not found"
}
}
There are several alternative ways:
By means of jenkins shared library you can wrap this code up to function or class:
#!/usr/bin/groovy
package org.utils
class PipelineUtils {
static def deleteFile(String name) { new File(name).delete() }
}
in your pipeline script, you need to import the library:
#Library('your-jenkins-library')_
import static org.utils.PipelineUtils.*
deleteFile('test.zip')
As #Sean has suggested to approve the script via "Manage Jenkins > In-process Script Approval".
There is File Operations Plugin:
fileOperations([script.fileDeleteOperation(excludes: '', includes: 'test.zip')])
There is Workspace Cleanup Plugin, but you need to find suitable exclude-patterns, otherwise this will clean all files:
def new_exclude_patterns = [[pattern: ".git/**", type: 'EXCLUDE']]
cleanWs deleteDirs: false, skipWhenFailed: false, patterns: new_exclude_patterns
If you are running pipeline on linux slave (or windows slave with sh in path), you may use the below call to avoid interactive prompts.
sh(""" rm -rf "$directory" """)
Navigate to /scriptApproval/ (Manage Jenkins > In-process Script Approval) and approve the script.
Another way since Java 1.7/Groovy ?.? is:
Files.delete(Path.of(FQFN))

Resources