'.' is not recognized as an internal or external command for Gradle command in Jenkins when gradlew script is executed - jenkins

I try to run SonarQube analysis for a Gradle project in a Jenkins Pipeline using the following code:
stage('SonarQube') {
withGradle {
withSonarQubeEnv('SonarQube Env') {
bat "./gradlew sonarqube"
}
}
}
The Gradle plugin is installed in Jenkins but I am getting the following error:
05:15:05 D:\*\*\*\*\*\*>./gradlew sonarqube
05:15:05 '.' is not recognized as an internal or external command,

Two things are incorrect in your code. On Windows machines you have to:
use backslashes instead of slashes in paths (./command → .\command)
execute script written for Windows (gradlew is a Unix script, gradlew.bat is a Windows script)
This code should work:
stage('SonarQube') {
withGradle {
withSonarQubeEnv('SonarQube Env') {
bat '.\\gradlew.bat sonarqube'
}
}
}
Gradle Wtapper by default is provided with two script gardlew and gradlew.bat. If your project doesn't have the gradlew.bat file, execute on your Unix machine ./gradlew wrapper. The missing file will be generated.
Btw. You don't need the Jenkins Gradle plugin, when you use Gradlew Wrapper. The plugin is required when you want to provide Gradle installations for jobs, example:
stage('SonarQube') {
withGradle {
withSonarQubeEnv('SonarQube Env') {
bat "${tool(name: 'toolId', type: 'gradle')}\\bin\\gradle.bat sonarqube"
}
}
}
toolId must much the identifiers used in the Jenkins Global Tool Configuration, examples: gradle-6.X, gradle-6.8.3 etc.

Related

Jenkins build failed due to command not being recognized

I have this build error saying pandoc command is not recognize, when I build my pipeline on Jenkins :
But when I run the exact same command using cmd.exe from the same repository it works perfectly :
So what's wrong here, my command pandoc is well installed and can perfectly be used from cmd.exe, why doesn't it works from Jenkins ?
Here is my Jenkins code (the part causing the error is in the "Build" stage):
pipeline {
agent any
stages {
stage('Prerequisites') {
steps {
//bat 'RMDIR C:\\wamp64\\www\\html\\doc'
bat 'MKDIR C:\\wamp64\\www\\html\\doc'
}
}
stage('Build') {
steps {
bat 'pandoc -s C:\\wamp64\\www\\index.md -o C:\\wamp64\\www\\index.html'
bat 'pandoc -s C:\\wamp64\\www\\index.md -o C:\\wamp64\\www\\index.docx'
}
}
stage('Deploy') {
steps {
bat 'COPY C:\\wamp64\\www\\index.html COPY C:\\wamp64\\www\\html\\index.html'
bat 'COPY C:\\wamp64\\www\\index.docx COPY C:\\wamp64\\www\\html\\doc\\index.docx'
}
}
}
}
Thanks for helping.
Jenkins doesn't automatically take your Windows (path) environment variables. Instead, what you need to do is to go to Jenkins -> Configure System -> Global properties -> Environment variables and add a new variable called Path. For the value, set $Path, and your path variables should start getting registered.
The issue has been discussed extensively in this question.

Maven pipeline fails to find JDK

I have a problem running a simple Java Maven pipeline. The used maven command fails using the expected JDK for an unknown reason.
freshly installed Jenkins running from the docker image jenkinsci/blueocean:latest
configured Oracle JDK 9 and Maven 3.5.4 at the Jenkins Tools settings pane
Jenkinsfile content:
pipeline {
agent any
tools {
jdk 'java9'
maven 'Maven3.5'
}
stages {
stage ('Initialize') {
steps {
sh '''
echo "PATH = ${PATH}"
echo "M2_HOME = ${M2_HOME}"
echo "JAVA_HOME = ${JAVA_HOME}"
'''
}
}
stage ('Build') {
steps {
sh 'java -version'
sh 'mvn --version'
}
}
}
}
Output of the "Initialize" stage script
PATH = /var/jenkins_home/tools/hudson.model.JDK/java9/bin:/var/jenkins_home/tools/hudson.tasks.Maven_MavenInstallation/Maven3.5/bin:/var/jenkins_home/tools/hudson.model.JDK/java9/bin:/var/jenkins_home/tools/hudson.tasks.Maven_MavenInstallation/Maven3.5/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/lib/jvm/java-1.8-openjdk/jre/bin:/usr/lib/jvm/java-1.8-openjdk/bin
M2_HOME = /var/jenkins_home/tools/hudson.tasks.Maven_MavenInstallation/Maven3.5
JAVA_HOME = /var/jenkins_home/tools/hudson.model.JDK/java9
This looks valid to me. M2_HOME and JAVA_HOME point to the expected path of the Jenkins tools.
Output of the "Build" "java -version" script
+ java -version
openjdk version "1.8.0_191"
OpenJDK Runtime Environment (IcedTea 3.10.0) (Alpine 8.191.12-r0)
OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)
This seems to be the installed Java of the Jenkins docker host system
Failure output of the "Build" "mvn -version" script:
mvn --version
/var/jenkins_home/tools/hudson.tasks.Maven_MavenInstallation/Maven3.5/bin/mvn: exec: line 191: /var/jenkins_home/tools/hudson.model.JDK/java9/bin/java: not found
script returned exit code 127
If I bash into the Jenkins docker container, I find the java bin at the mentioned path.
Where is my mistake? Why can't Maven find the Java?
Is it a permission problem? Jenkins docker container uses a docker volume:
-v jenkins-data:/var/jenkins_home.
Best,
Lars
Not sure tools + docker in Jenkins Pipeline is working so good.
See: https://issues.jenkins-ci.org/browse/JENKINS-36159
Only way so far I've managed to get this to work was by mounting the docker node's tools directory into the docker slave.
e.g.
agent { docker
args '-v $HOME/tools:/var/lib/jenkins/tools'
stage { step {
sh "${MAVEN_HOME}/bin/mvn -version"
EDIT: Just found this too: https://issues.jenkins-ci.org/browse/JENKINS-48050
You don't need both tools. It is enough with Maven tool. It includes Java. Your pipeline should be:
pipeline {
agent any
tools {
maven 'Maven3.5'
}
stages {
stage ('Initialize') {
steps {
sh '''
echo "PATH = ${PATH}"
echo "M2_HOME = ${M2_HOME}"
echo "JAVA_HOME = ${JAVA_HOME}"
'''
}
}
stage ('Build') {
steps {
sh 'java -version'
sh 'mvn --version'
}
}
}
}
And you can delete Java Tool in Jenkins configuration tool.
This problem occurs when you're using an alpine based docker image for jenkins and your jdk is glibc based. You can solve this in two ways:
Use a jdk that is musl based. You can find them on adoptium by filtering out the OS as Alpine Linux.
Use a non-alpine based docker image.

Running SonarQube Scanner on a Jenkins remote slave

I have a Docker container running Jenkins (2.150.1) and another Docker container running SonarQube (7.4). Jenkins is using the SonarQube Scanner for Jenkins plugin and the scanning is done on the Jenkins container. The Jenkinsfile for the project looks like this:
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'building...'
}
}
stage('Test') {
steps {
echo 'testing...'
withSonarQubeEnv ('SonarQube') {
sh '/var/jenkins_home/sonar-scanner/sonar-scanner-3.2.0.1227-linux/bin/sonar-scanner'
}
echo 'really finished testing2'
}
}
stage("Quality Gate") {
steps {
timeout(time: 1, unit: 'MINUTES') {
waitForQualityGate abortPipeline: true
}
}
}
stage('Deployment') {
steps {
echo 'deploying...'
}
}
}
}
To get the scanning to work as part of a Jenkins pipeline job, I manually installed sonar-scanner on the Jenkins container by downloading the zip file and unzipping it to: /var/jenkins_home/sonar-scanner/sonar-scanner-3.2.0.1227-linux
This is working well, but I want to improve it by:
taking out the harcoded path to sonar-scanner from my Jenkinsfile
specify a non local location of sonar-scanner because I now need to run the scan on another VM/container instead of on the Jenkins container
I tried using Manage Jenkins > Global Tool Configuration > SonarQube Scanner and updated my Jenkinsfile to use SONAR_RUNNER_HOME instead of the hard coded path, but that didn't work and I got an error that sonar-scanner can't be found.
In Manage Jenkins > Global Tool Configuration > SonarQube Scanner check install automatically.
Then go to Manage Jenkins > Configure System and add the following
The Name should be the same as the parameter in the line in your Jenkinsfile: withSonarQubeEnv('SonarQube')

Compile groovy project and run JUnit tests via Jenkins

I googled for ages now and I give up, the buzz word Groovy + Jenkins is bringing up so many false flags...
I have a Groovy project I developed in IntelliJ, it contains also a JUnit.groovy with unit tests. Now this is a script for SoapUI, no need for Maven, Ant nor Grails, but I would like to be able to compile those files on Jenkins and run the unit tests after. Is it possible to build and test those files on Jenkins? So far all solutions seem to be me manually running groovyc (commited with my repository) and then running JUnit on the JUnit.class.
So before I start to dig deeper and write a Maven, Grails or Ant file, is there another way that does not involve me pushing the GroovySDK on my git? Or is there may be a simple build script, not involving 20 libraries and steps that would build the groovy sources and run the JUnit tests :) ?
I'm new to Jenkins obviously ;), thanks for your input.
Update:
So for all as newbie as me, what was required? First I changed my local source code to a gradle project (remember to activate AutoImport in IntelliJ) and also activate the creation of the JUnit xml and since I do not use Maven and the system is "offline" we have the libs in git anyway so my build.gradle is:
version '2.5-SNAPSHOT'
apply plugin: 'groovy'
dependencies {
compile fileTree(dir: '../Library', include: ['*.jar'])
}
test {
reports {
junitXml.enabled = true
html.enabled = true
}
}
sourceCompatibility = 1.8
set up gradle wrapper for the project via gradle wrapper for the gradlew.bat
then I added a post-commit in my git-/.hooks/ so my Jenkins is triggered upon commit via curl http://jenkins:8080/git/notifyCommit?url=https://git.git&branches=dev
finally set up a pipeline on jenkins:
#!groovy
node {
stage('Checkout') {
git branch: 'dev', credentialsId: 'youwish', url: 'https://git.git'
}
stage('Build') {
dir('./Modules') {
gradle('clean')
gradle('compileTestGroovy')
}
}
stage('UnitTest') {
dir('./Modules') {
gradle('test')
junit '/build/test-results/**/TEST-*.xml'
}
}
stage('IntegrationTest') {
stage('CodeTableDownload') {
dir('./SoapUi') {
bat 'AutoRun.bat'
junit '/results/**/*-JUNIT.xml'
}
}
}
}
def gradle(command) {
bat "./gradlew.bat $command"
}
There's a Groovy plugin for Jenkins that will let you execute Groovy scripts on Jenkins.
But, why not let something like Gradle do the build and run the test for you? A minimal Gradle build file for Groovy that will do both is:
apply plugin: 'groovy'
repositories {
jcenter()
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.4.12'
testCompile 'junit:junit:4.12'
}
You don't have to commit the GDK, just declare a dependency.

Running shell scripts in a Jenkins DSL mavenJob

I'm writing a Jenkins (version 2.6) DSL that will allow me to check out and build a Maven project, run a shell script and deploy it to Artifactory.
mavenJob("test-build") {
multiscm {
...
}
steps {
shell ("bash build-scripts/script.sh")
}
goals("clean install")
configure{ project ->
project/publishers << 'org.jfrog.hudson.ArtifactoryRedeployPublisher' {
details {
artifactoryUrl('<url>')
artifactoryName('<name>')
repositoryKey('libs-release-local')
snapshotsRepositoryKey('libs-snapshot-local')
}
deployBuildInfo(true)
deployArtifacts(true)
evenIfUnstable(false)
}
publishers {
archiveJunit('target/*/.xml')
publishBuilder {
discardOldBuilds(7,10)
}
}
}
The job only works if I remove the steps{} block as steps aren't permitted in a mavenJob. I've tried using a freeStyleJob, but ArtifactoryRedeployPublisher doesn't work.
What do I have to do to run my shell script?
If you want shell script to be run ahead of maven goal, you can use this
preBuildSteps {
// Runs a shell script.
shell(String command)
}
Or if you want the shell script run after the maven goal, you can use this one
mavenJob('example-1') {
postBuildSteps {
shell("echo 'run after Maven'")
}
}
For detail, you can check the job dsl api viewer
https://jenkinsci.github.io/job-dsl-plugin/#method/javaposse.jobdsl.dsl.jobs.MavenJob.postBuildSteps
It is very easy and handy tool.
Br,
Tim

Resources