Maven issue while creating Jenkins Pipeline - maven

I am trying to create Jenkins Pipeline for one of my automation job. I created Jenkins file. Code specified below:
pipeline {
agent any
def mvn_version = 'MavenTest'
withEnv( ["PATH+MAVEN=${tool mvn_version}/bin"] ) {
//sh "mvn clean package"
}
stages {
stage('Git checkout') { // for display purposes
steps {
git branch: "ReportTest", url: 'https://github.abc.com/vsing136/testWDM.git'
sh "mvn clean verify"
}
}
stage('Stage 1') {
steps {
echo 'Hello world!'
}
}
}
}
post {
always {
emailext body: "Build URL: ${BUILD_URL}",
subject: "$currentBuild.currentResult-$JOB_NAME",
to: 'vabc1#example.com'
}
}
Screenshot with my job configuration specified below:
I am not sure what am I doing wrong but I am getting error for this configuration -
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 4: Tool type "maven" does not have an install of "Maven 3.3.9" configured - did you mean "Maven"? # line 4, column 11.
maven 'Maven 3.3.9'
^
1 error

Following code worked for me:
agent any
tools {
maven 'MavenTest'
}

Related

Allure reports in parallel test pipeline in Jenkins

trying to generate two allure reports while running jenkins pipeline job in paralled mode.
tried to implement solution found here Can't split allure report into suites in jenkins pipeline, but no luck.
here is my pipeline code that is working. but allure report shows details on test which lasted longer in terms of execution.
is there a possibility to create separate folders for different stages? jenkins is running on remote server and i am unable to create dir for allure-report there. maybe there is a mvn command that i can send in steps where allure-report will store artifacts of test execution or something else? please, advice.
pipeline {
agent any
tools {
maven 'Maven 3.8.6'
}
stages {
stage('Run Tests') {
parallel {
stage('Test on DEV') {
agent {
label 'node_02_jdk_17'
}
steps {
cleanWs()
git branch: 'develop', credentialsId: 'jenkins_git', url: 'https://Jenking_GIT#...-automated-tests.git'
sh "mvn test -P UI -Dtarget.env=develop -Dtest=LogIn"
allure includeProperties: false, jdk: '', properties: [[key: 'allure.results.directory', value: 'target/allure-results']], report: 'target/allure-report', results: [[path: 'target/allure-results']]
}
}
stage('Test on QA') {
agent {
label 'node_02_jdk_17'
}
steps {
cleanWs()
git branch: 'develop', credentialsId: 'jenkins_git', url: 'https://Jenking_GIT#...-automated-tests.git'
sh "mvn test -P UI -Dtarget.env=qa -Dtest=LogIn2"
allure includeProperties: false, jdk: '', properties: [[key: 'allure.results.directory', value: 'target/allure-results']], report: 'target/allure-report', results: [[path: 'target/allure-results']]
}
}
}
}
}
}

Is there a way to fix the 'Expected a step # line' error from a jenkins file

I created a very basic jenkinsfile to run run a maven project. I added a configuration to read from the project's pom.xml file during the build stage, but the build fails with the error: Expected a step # line 32. This is the code on that line: pom = readMavenPom file: 'pom.xml
I read on SO from some users that had a similar issue that the step should be enclosed in script{}, so I tried this, but I am still getting the same error. What am I doing wrong here:
pipeline {
agent any
options{
timestamps()
}
stages {
stage ('Compile Stage') {
steps {
script {
pom = readMavenPom file: 'pom.xml'. //error is thrown here
withMaven(maven: 'MAVEN_HOME') {
sh 'mvn clean install'
}
}
}
}
stage ('Testing Stage') {
steps {
withMaven(maven : 'MAVEN_HOME') {
sh 'mvn test'
}
}
}
stage ('Cucumber Reports') {
steps {
cucumber buildStatus: "UNSTABLE",
fileIncludePattern: "**/cucumber.json",
jsonReportDirectory: 'target'
}
}
}
}
Started by user unknown or anonymous
Obtained Demo/jenkinsfile.groovy from git https://repo#bitbucket.org/my-repo/bitbucketfiles.git
Running in Durability level: MAX_SURVIVABILITY
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 32: Expected a step # line 32, column 17.
pom = readMavenPom file: 'Demo/pom.xml'
^
1 error
at org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)
at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1085)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:603)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:581)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:558)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:298)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:268)
at groovy.lang.GroovyShell.parseClass(GroovyShell.java:688)
at groovy.lang.GroovyShell.parse(GroovyShell.java:700)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.doParse(CpsGroovyShell.java:142)
at org.jenkinsci.plugins.workflow.cps.CpsGroovyShell.reparse(CpsGroovyShell.java:127)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.parseScript(CpsFlowExecution.java:571)
at org.jenkinsci.plugins.workflow.cps.CpsFlowExecution.start(CpsFlowExecution.java:523)
at org.jenkinsci.plugins.workflow.job.WorkflowRun.run(WorkflowRun.java:337)
at hudson.model.ResourceController.execute(ResourceController.java:97)
at hudson.model.Executor.run(Executor.java:429)
Finished: FAILURE

Jenkins pipeline, mvn not reconegize as an internal command or external, an operable program or a batch file

I'm doing a pipeline, when doing a mvn clean install command in my project, this error mvn not reconegize as an internal command or external, an operable program or a batch file.
My project is a Maven java project, my operating system is windows 10
JenkinsFile
pipeline {
agent any
stages {
stage('Unit tests') {
steps {
// Run the maven build
// if (isUnix()) {
// sh "'${mvnHome}/bin/mvn' clean test -Dtest=TestRunner"
// } else {
bat 'mvn clean test'
// }
}
}
stage("build & SonarQube analysis"){
agent any
steps {
withSonarQubeEnv('My SonarQube Server') {
bat 'C:/Users/ANTONIO/Documents/sonar-scanner-4.2.0.1873-windows/bin/sonar-scanner'
}
}
}
stage("Quality Gate") {
steps {
timeout(time: 1, unit: 'HOURS') {
error "Pipeline aborted due to quality gate failure: ${qg.status}"
waitForQualityGate abortPipeline: true
}
}
}
}
}
It seems your agent does not have maven installed on it. Also, check if maven is installed on the path mentioned here.

Execute certain stages based on job

I am still confused about how to execute stage, based on Jenkin's job.
Let's say I created the following jobs:
buildJob : this will just execute the prepare + build stages, triggered when the code is commited into git/svn
integrationJob : prepate + build + test + deploy into integration server
stagingJob : build + test + deploy into staging server
nexusJob : deploy into nexus
prodJob : deploy from nexus into production server, based on user's input
This is the Jenkinsfile :
pipeline {
agent any
tools {
maven 'maven.1.8.0'
}
options {
skipStagesAfterUnstable()
}
environment {
INTEGRATION_URL = "https://"
NEXUS_URL = "https://"
STAGING_URL = "https://"
PROD_URL = "https://"
}
stages {
stage('=== Prepare ...') {
steps {
echo 'git checkout master + git pull ...'
}
}
stage('=== Build ...') {
steps {
echo 'build ...'
sh 'mvn clean compile'
}
}
stage('=== Test ...') {
steps {
echo 'test ...'
sh 'mvn clean verify'
}
}
stage('=== Release to Nexus...') {
steps {
echo 'mvn release:prepare release:perform ...'
}
}
stage('=== Deploy to ...') {
steps {
echo 'Deploy ...'
}
}
}
}
One of the solutions is by implementing the 'when' condition, based on inout parameters (I saw this somewhere). But, is there any solutions WITHOUT human input ?
Thanks.
pipeline script follow groovy syntax, you could try using if-else condition where you could pass the conditions for a specific stage execution, if that's easy for you.

Require Jenkins Pipeline script for Maven build and Maven Test from Pom.xml?

I am new to Jenkins and I have created the Maven+TestNG project in eclipse I which I have executed the sample test case to print hello world from POM.xml,which is running fine. I have pushed the project in GIT as well Now I need to execute this from Jenkins using Pipeline script for CI/CD.
I have created below Pipleline script in Jenkins pipeline tab but I am getting error which executing the script. Can you please guide me or provide me the tutorial link where I can get Jenkins pipeline example.
The script is as follow:
node {
def mvnHome
stage('Preparation') { // for display purposes
// Get some code from a GitHub repository
git 'https://github.com/jitsolution19/RestAssured_Framework.git'
// Get the Maven tool.
// ** NOTE: This 'M3' Maven tool must be configured
// ** in the global configuration.
mvnHome = tool 'Maven'
}
stage('Build') {
// Run the maven build
if (isUnix()) {
sh "'${mvnHome}/bin/mvn' -Dmaven.test.failure.ignore clean package"
} else {
bat(/"${mvnHome}\bin\mvn" -Dmaven.test.failure.ignore clean package/)
}
}
stage('Test') {
//Perform test
echo 'Execute the script'
if (isUnix()) {
sh "'${mvnHome}/bin/mvn' -Dmaven.test.failure.ignore test"
} else {
bat(/"${mvnHome}\bin\mvn" -Dmaven.test.failure.ignore test/)
}
}
stage('Results') {
junit '**/target/surefire-reports/TEST-*.xml'
archive 'target/*.jar'
}
}

Resources