Jenkins pipeline DSL Maven Error due to #tmp dir - java

I just created a Jenkins Pipeline DSL job where I cloned a Java code from SCM and tried to run mvn clean. But the pipeline continuously throwing an error saying:
mvn clean install -Dmaven.test.skip=true -Dfindbugs.skip=true
/var/lib/jenkins/workspace/<project>#tmp/durable-77d8d13c/script.sh: 2:
/var/lib/jenkins/workspace/<project>#tmp/durable-77d8d13c/script.sh: mvn: not found
Seems like it tries to find pom.xml inside the <project>#tmp directory which is empty. Actual code is cloned successfully inside the <project> directory. Below is my Jenkinsfile:
node {
stage ("Clean Workspace") {
echo "${WORKSPACE}"
cleanWs()
}
stage ("Get Code") {
git branch: "${params.branch}", url: 'git#bitbucket.org:xx/xxxxxxx.git'
}
stage ("mvn clean") {
sh "mvn clean install -Dmaven.test.skip=true -Dfindbugs.skip=true"
}
}
I also tried with ${WORKSPACE} env variable but still does not work.

The issue was resolved, I modified the Pipeline DSL jobs by including Maven tool step as:
stage ("mvn clean") {
withEnv( ["PATH+MAVEN=${tool name: 'mvn', type: 'maven'}/bin"] ) {
sh "mvn clean install -Dmaven.test.skip=true -Dfindbugs.skip=true"
}
}

Instead of doing like that you can directly add mvn to path itself. for that, open your ~/.bash_profile and add these lines.
MAVEN_HOME="<path to maven folder>"
export MAVEN_HOME
PATH=$PATH:$MAVEN_HOME/bin
Don't forget to do source ~/.bash_profile to get the changes. This will provide mvn to path so that you can access mvn directly.
So if you do that your code can be now like...
stage ("mvn clean") {
sh "mvn clean install -Dmaven.test.skip=true -Dfindbugs.skip=true"
}

The error message mvn: not found means that mvn command could not be found by Jenkins pipeline.
You should use a withMaven pipeline step to provide a valid maven environnement.
For example :
node {
stage ("Clean Workspace") {
echo "${WORKSPACE}"
cleanWs()
}
stage ("Get Code") {
git branch: "${params.branch}", url: 'git#bitbucket.org:xx/xxxxxxx.git'
}
stage ("mvn clean") {
withMaven {
sh "mvn clean install -Dmaven.test.skip=true -Dfindbugs.skip=true"
}
}
}
Check that Jenkins is properly configured to use maven (Under 'Manage Jenkins > Configure System') and the 'Pipeline Maven Plugin' is installed (Under 'Manage Jenkins > Manage Plugins').
More information here : https://wiki.jenkins.io/display/JENKINS/Pipeline+Maven+Plugin

Related

Spring Boot Microservices - Jenkinsfile "The goal you specified requires a project to execute but there is no POM in this directory"

I have a problem about running Jenkinsfile in Jenkins for my Spring Boot Microservices example.
I run Jenkins on Docker.
After implementing a required settings for Jenkins and creating Jenkinsfile in my Spring Boot Microservices example, I run the pipeline in Jenkins but I got an error.
Here is the error message shown below.
+ mvn clean install -D skipTests
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.053 s
[INFO] Finished at: 2023-01-29T01:07:58Z
[INFO] ------------------------------------------------------------------------
[ERROR] The goal you specified requires a project to execute but there is no POM in this directory (/var/jenkins_home/workspace/Spring Boot Microservices Pipeline). Please verify you invoked Maven from the correct directory. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MissingProjectException
How can I fix the issue?
Here is my repo : Link
Here is git backend system : Link
In your repo (https://github.com/Rapter1990/springbootmicroservicedailybuffer) you have several pom.xml, but they are in subfolders, so you should enter them before running maven.
dir("apigateway") {
sh "mvn clean install -DskipTests"
}
dir("auth-service") {
sh "mvn clean install -DskipTests"
}
...
I also guess you could delete the "git" line in your Jenkinsfile, as Jenkins is already downloading the project to run it (you have your jenkinsfile and your source code in the same git repo)
Here is the answer shown below.
pipeline {
agent any
tools {
maven "Maven 3.6.3"
}
stages {
stage("build project") {
steps {
// git 'https://github.com/Rapter1990/springbootmicroservicedailybuffer'
echo "Java VERSION"
sh 'java --version'
echo "Maven VERSION"
sh 'mvn --version'
echo 'building project...'
dir("service-registry") {
sh "mvn clean install"
}
dir("configserver") {
sh "mvn clean install"
}
dir("apigateway") {
sh "mvn clean install -DskipTests"
}
dir("auth-service") {
sh "mvn clean install -DskipTests"
}
dir("productservice") {
sh "mvn clean install -DskipTests"
}
dir("orderservice") {
sh "mvn clean install -DskipTests"
}
dir("paymentservice") {
sh "mvn clean install -DskipTests"
}
}
}
}
}

TestNG Listener that clears tmp folder even for an aborted run via Jenkins?

I use Java Selenium WebDriver with TestNG for running my tests.
I am calling driver.quit() in my final test to make sure the files created in /tmp/ folder are deleted.
However, if there is an aborted run (via Jenkins), the contents in /tmp/ folder are not deleted.
Is there a testng listener or any other way I can make sure that tmp folder is cleared even if the run is aborted mid-run?
If the issue introduced on Jenkins level (aborting action), I believe, more effective will be solve this also with Jenkins, not with TestNG Listener.
For declarative pipeline
pipeline {
agent any
stages {
...
}
post {
aborted {
script {
echo 'cleanup on abort'
// this will clean workspace
// cleanWs()
//or just delete 'tmp' directory
dir ('tmp') {
deleteDir()
}
}
}
}
}
Using Plugin
https://plugins.jenkins.io/postbuild-task/
Install plugin and setup shell script execution for the job like
rm -rf tmp

Gitlab CI/CD running fine on local but not on gitlab

I am trying to build a gitlab CI/CD pipeline on my java maven project. I tried all the stages on my local machine and they are passing successfully but when I try the same things on gitlab repo it is throwing error. I tried almost every solution and gone through numerous posts to resolve the errors but to no use. Here is my yml content:
image: maven:3.5-jdk-8
variables:
MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode"
MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"
include:
- template: Security/SAST.gitlab-ci.yml
cache:
paths:
- .m2/settings.xml
# Define stages
# Stages group various steps into one block,
# if any step fails, the entire stage fails
stages:
- validate
- compile
- SonarQube
- test
validate:
stage: validate
script:
- mvn validate
compile:
stage: compile
script:
- mvn $MAVEN_CLI_OPTS compile
sonarqube-check:
image: maven:3.6.3-jdk-11
stage: SonarQube
variables:
SONAR_USER_HOME: "${CI_PROJECT_DIR}/.sonar"
script:
- mvn sonar:sonar \ -Dsonar.projectKey=XX.XX.XXX.XXX \ -Dsonar.host.url=http://XX.XX.XXX.XXX \ -Dsonar.login=XX.XX.XXX.XXX -X
allow_failure: true
spotbugs-sast:
variables:
COMPILE: "false"
SECURE_LOG_LEVEL: "debug"
artifacts:
reports:
sast: gl-sast-report.json
#spotbugs-sast:
# variables:
# SECURE_LOG_LEVEL: "debug"
#FAIL_NEVER: 1
test:
stage: test
script:
- mvn $MAVEN_CLI_OPTS test -B
The stage validate and compile are running fine but I am getting warnings in sonar check and spotbugs sast. The same is running fine on my local.
In the sonarqube check the error is:
Error resolving version for plugin ' -Dsonar.host.url=http://XX.XX.XXX.XXX' from the repositories [local (/builds/XX.XX.XXX.XXX/.m2/repository), central (https://repo.maven.apache.org/maven2)]: Plugin not found in any plugin repository -> [Help 1]
org.apache.maven.plugin.version.PluginVersionResolutionException: Error resolving version for plugin ' -Dsonar.host.url=http://XX.XX.XXX.XXX' from the repositories [local (/builds/XXXX/XXXXX/.m2/repository), central (https://repo.maven.apache.org/maven2)]: Plugin not found in any plugin repository
and for the spotbugs-sast warning the error is:
[FATA] [Find Security Bugs] ▶ lstat /root/.m2/repository: no such file or directory
I have spent numerous hours resolving this and waiting for the project to compile successfully with all the stages but every time I was disappointed. Please help. I appreciate your time.
Maven recognized the String -Dsonar.host.url=http://XX.XX.XXX.XXX as a plugin/goal that it needs to resolve.
This is caused by the \ in your script.
Instead of:
mvn sonar:sonar \ -Dsonar.projectKey=XX.XX.XXX.XXX \ -Dsonar.host.url=http://XX.XX.XXX.XXX \ -Dsonar.login=XX.XX.XXX.XXX -X
try:
mvn sonar:sonar -Dsonar.projectKey=XX.XX.XXX.XXX -Dsonar.host.url=http://XX.XX.XXX.XXX -Dsonar.login=XX.XX.XXX.XXX -X
I assume that you copied that command from a format like this:
mvn sonar:sonar \
-Dsonar.projectKey=XX.XX.XXX.XXX \
-Dsonar.host.url=http://XX.XX.XXX.XXX \
-Dsonar.login=XX.XX.XXX.XXX -X
In bash the \ must be at the end of the line if the command contains a linebreak so it's recognized as one command.
But you need to remove it if you convert it to one line.

Jenkinsfile Parameters Issue : Issue while running with multiple parameters to bat maven

I have an issue while running my job through the Jenkins file, whatever parameters I gave its updated in the place where I called it, but when it runs with bat mvn command it tool goal as that of the parameter itself. Suppose I Specified the parameter name is QA from my Jenkins build with parameter and run it ..it complete all the steps except the last one where i have build, bat mvn, it tried to run taking goal and lifecycle as QA instead of clean test-compile.
I have attached the Jenkinfile herewith.
pipeline {
agent { label 'TESTING' }
tools {
maven 'maven'
}
parameters{
choice(name: 'ENVIRONMENT' , choices: ['QA' , 'UAT' , 'PREPROD'] , description: '')
choice(name: 'SUITE' , choices: ['Regression' , 'Smoke'] , description: '')
}
stages {
stage('Clone') {
steps {
git branch: 'master' , credentialsId: 'xyzabc' , url: "https://git.xyz.com/project/automation.git"
}
}
stage('Git Checkout') {
steps {
git(branch: 'development',
credentialsId: 'xyzabc',
url: "https://git.xyz.com/project/automation.git")
}
}
stage('Build') {
steps {
echo "parameter name from jenkinsfile Environment is ${params.ENVIRONMENT}"
echo "parameter name from jenkinsfile SUITE is ${params.SUITE}"
bat "mvn clean test-compile exec:java -Dexec.mainClass='com.test.mainClass' -Dexec.args='${params.ENVIRONMENT} ${params.SUITE}' -Dexec.classpathScope=test"
}
}
}
}
ERROR MESSAGE IN JENKINS RUN,

Jenkinsfile maven plugin inside a docker container

I'm using Jenkins with a Jenkinsfile that runs my builds inside a docker container. I have a simple Java application that I'd like to build and deploy to artifactory using the Jenkins Artifactory plugin.
My Jenkinsfile is below -
node {
def server = Artifactory.server "my-artifactory"
def rtMaven = Artifactory.newMavenBuild()
stage("Prepare environment"){
docker.image('driv/docker-maven-java-oracle').inside {
checkout scm
stage("Artifactory configuration") {
rtMaven.deployer releaseRepo:'libs-release-local', snapshotRepo:'libs-snapshot-local', server: server
rtMaven.resolver releaseRepo:'libs-release', snapshotRepo:'libs-snapshot', server: server
}
stage("Maven build") {
def buildInfo = rtMaven.run pom: 'pom.xml', goals: 'clean install'
}
}
}
}
However, when I run builds with the above Jenkinsfile I get the error -
java.lang.RuntimeException: Couldn't find maven installation
I know that I can add a line to my Jenkinsfile like this -
rtMaven.tool = MAVEN_TOOL
...that I can use to specify a pre-configured Jenkins Tool to point the Artifactory plugin at Maven. However, it seems to me that such a pre-configured tool would have to be on the Jenkins machine, or a build node, and not inside my docker container.
So, is it possible to point the Artifactory plugin at a maven installation inside my docker container?
Thanks.
This should probably be fixed in their jenkins plugin itself but as "Christopher Orr" suggested mapping environment vars at the pipeline stage level to the same ones set in the container work
For example
stage('mystage') {
environment {
// If your using the official maven image, these are probably where it puts it
MAVEN_HOME = '/usr/share/maven'
JAVA_HOME= '/usr/local/openjdk-8'
}
steps {
sh """
# confirm location of MAVEN_HOME and JAVA_HOME in container and set in environment directive
printenv
"""
...
// call rtMavenRun()

Categories

Resources