Class path not recognized in jenkins when using command "mvn test" - jenkins

I have a project that I want to run via Jenkins and one of the steps in my JenkinsFile is like this (example):
stage ('Testing Stage') {
steps {
sh 'mvn -Dtest=com\folder1\folder1\myClass test'
}
}
If I run the command in the IDE terminal, the tests run without problem but in Jenkins, the slashes are trimmed so I get an error.
How should be the syntax in the Jenkins file?
EDIT:
This is the error I get:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 37: unexpected char: '\' # line 37, column 35.
sh "mvn -Dtest=com\folder1\folder1\myClass test"
^

The solution is to use the following syntax:
sh "mvn -Dtest=com/folder1/folder1/myClass test"

Related

What is a rule to make target in Docker container?

To learn how to manipulate Docker images from within Jenkins, I am reading this link.
What is a "rule to make a target" in Docker? The simple example below is failing because there is "no rule to make a target". What does this mean in Docker?
The Error And The Code That Triggers The Error
The sh 'make test' line of code in a Jenkinsfile from the link above is throwing an error when run inside the following block:
testImage.inside {
sh "whoami"
sh 'make test'
}
The actual error that Jenkins throws when trying to interpret the sh 'make test' line of code is:
make test— Shell Script<1s
[ple-dockere-containered-app-WWNVRTE6XFKMI4JPEVK2F2U3HOGDZICATW6XBFM2IQUW5PAG5FWA] Running shell script
+ make test
make: *** No rule to make target 'test'. Stop.
The complete Jenkinsfile is:
node {
// Clean workspace before doing anything
deleteDir()
try {
stage ('Clone') {
checkout scm
}
stage ('Build') {
def testImage = docker.build("test-image", "./app")
testImage.inside {
sh "whoami"
sh 'make test'
}
}
} catch (err) {
currentBuild.result = 'FAILED'
throw err
}
}
Note that the make test command is being run inside the container.

How to use jenkins version number plugin in Jenkinsfile?

Trying this step in Jenkinsfile with "version number plugin" installed:
stage("Build") {
echo "Building..."
TAG = ${BUILD_DATE_FORMATTED, "yyyyMMdd"}-develop-${BUILDS_TODAY}
sh "docker build -t $IMAGE:$TAG ."
}
And getting this error:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 15: unexpected token: BUILD_DATE_FORMATTED # line 15, column 15.
TAG=${BUILD_DATE_FORMATTED, "yyyy-MM-dd"}-develop-${BUILDS_TODAY}
^
1 error
What is correct way to use this plugin in Jenkinsfile?
You need to use it as a step.
tag = VersionNumber (versionNumberString: '${BUILD_DATE_FORMATTED, "yyyyMMdd"}-develop-${BUILDS_TODAY}')
Take a look at https://your_jenkins_url.com/pipeline-syntax/ and check all the options for the VersionNumber step in the snipped generator.
in Jenkinsfile.. try this
env.BN = VersionNumber([
versionNumberString :'${BUILD_MONTH}.${BUILDS_TODAY}.${BUILD_NUMBER}',
projectStartDate : '2017-02-09',
versionPrefix : 'v1.'
])
In stage use the script block to run for version numbering
stage("Build") {
script {
TAG = VersionNumber(versionNumberString: '${BUILD_DATE_FORMATTED, "yyyyMMdd"}-develop-${BUILDS_TODAY}')
echo "Building..."
sh "docker build -t $IMAGE:$TAG ."
}
}

Jenkins: Unable to create directory using the date

I want to create a directory using jenkins with the days date.
I'm using the new jenkins declarative syntax.
When i run the build as described in the job below it fails.
The mkdir command though, works perfectly well on the console.
pipeline {
agent any
stages {
stage('Prepare') {
steps {
echo "Checking for the existence of a debian packages directory for this package"
sh "mkdir -p {env.JENKINS_HOME}/workspace/debian_packages/api-config/$(date +"%d-%m-%Y")"
}
}
}
}
This is the error I get (I've tried escaping the $ characters but it still fails)
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup
failed:
WorkflowScript: 19: illegal string body character after dollar sign;
solution: either escape a literal dollar sign "\$5" or bracket the
value expression "${5}" # line 19, column 17.
sh "mkdir -p
{env.JENKINS_HOME}/workspace/debian_packages/api-
config/$(date +'%d-%m-%Y')"
What could be the issue? Isn't the jenkins "sh" meant to take commands as they would have been issued directly on the console?
This is workaround that I could come up:
stage('Prepare') {
steps {
script{
sh '(date +"%d-%m-%Y") > outFile'
def curDate = readFile 'outFile'
echo "The current date is ${curDate}"
sh "mkdir -p {env.JENKINS_HOME}/workspace/${curDate}"
}
}
}

Jenkins fails running MSBuild command

This is my Jenkinsfile, and I have MSBuild plugin installed in Jenkins. The msbuild command below is correct as I can run it from the command line, yet Jenkins keeps failing on it. If I remove the parameter it's complaining about then it fails on next one, etc...
Jenkinsfile (saved in git repository):
pipeline {
agent {
docker 'node:7.7.3'
}
stages {
stage('Build') {
steps {
bat echo 'Checking node.js version..'
bat echo 'node -v'
bat echo 'Restore nugets..'
bat 'nuget restore mySolution.sln'
bat echo 'Building..'
bat "C:\\Program Files (x86)\\MSBuild\\14.0\\Bin\\msbuild.exe" mySolution.sln /noautorsp /ds /nologo /t:clean,rebuild /p:Configuration=Debug /v:m /p:VisualStudioVersion=14.0 /clp:Summary;ErrorsOnly;WarningsOnly /p:ProductVersion=1.0.0.${env.BUILD_NUMBER}
}
}
stage('Test') {
steps {
bat echo 'Testing..'
}
}
stage('Deploy') {
steps {
bat echo 'Deploying....'
}
}
}
post {
success {
bat echo 'Pipeline Succeeded'
}
failure {
bat echo 'Pipeline Failed'
}
unstable {
bat echo 'Pipeline run marked unstable'
}
}
}
Error:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 14: expecting '}', found '/' # line 14, column 84.
\msbuild.exe" mySolution.sln /noautorsp
^
The issue is that the entire bat argument needs to be in quotes, so:
bat "'C:\\Program Files (x86)\\MSBuild\\14.0\\Bin\\msbuild.exe' mySolution.sln /noautorsp /ds /nologo /t:clean,rebuild /p:Configuration=Debug /v:m /p:VisualStudioVersion=14.0 /clp:Summary;ErrorsOnly;WarningsOnly /p:ProductVersion=1.0.0.${env.BUILD_NUMBER}"
Otherwise, it's treating mySolution.sln as a Groovy keyword, then /noautorsp, etc. You could also avoid the full path to MSBuild.exe here by defining it as a tool in Jenkins (via the MSBuild plugin), then doing what I describe at https://stackoverflow.com/a/45592810/466874.

Jenkins Pipeline Error

I am using the pipeline plugin in Jenkins, but unable to run shell commands. I am receiving the following error:
[develop - pipeline] Running shell script
nohup: failed to run command ‘sh’: No such file or directory
The node is an Ubuntu instance.
node ('aws-ondemand') {
//println env.BUILD_NUMBER
try {
stage 'Checkout and Build'
git url: 'git#github.com:MyAndroidRepo.git',
branch: 'develop'
sh 'git submodule init'
sh 'git submodule update'
sh './gradlew clean build'
}catch (e) {
//currentBuild.result = "FAILED"
//notifyFailed()
throw e
}
}
Nevermind. Script is fine. I was injecting env variables in the build step. I removed it and now its working.

Resources