Maven lifecycle within jenkins pipeline - how to best separate responsibilities? - maven

When working with jenkins 2 (declarative) pipelines and maven I always have a problem with how to organize things within the pipeline to make it resusable and flexible.
On the one side I would like to seperate the pipepline into logical stages like:
pipeline
{
stages
{
stage('Clean') {}
stage('Build') {}
stage('Test') {}
stage('Sanity check') {}
stage('Documentation') {}
stage('Deploy - Test') {}
stage('Selenium tests') {}
stage('Deploy - Production') {}
stage('Deliver') {}
}
}
On the other hand I have maven which runs with
mvn clean deploy site
Simply I could split up maven to
mvn clean
mvn deploy
mvn site
But the 'deploy' includes all lifecycle phases from
validate
compile
test
package
verify
install
deploy
So I saw a lot of pipline examples which do things like
sh 'mvn clean compile'
and
sh 'mvn test'
which results in repeating the validate and compile step a second time and waste "time/resources" in this way.
This could be resolved with doing a
sh 'mvn surefire:test'
instead of running the whole lifecycle again.
So my question is - which is the best way to get a good balance between the jenkins pipline stages and the maven lifecycle?
For me I see two ways:
Split up the maven lifecycles to as much pipeline stages as possible - which will result in better jenkins user feedback (see which stage fails etc.)
Let maven do everything and use the jenkins pipeline only to work with the results of maven (i.e. analyzing unit test results etc.)
Or did I missunderstand something in the CI/CD practice?

Two month later I think I have a well balanced Jenkins pipeline script that is not complete, but works stable on windows and linux. It avoids pitfalls of other examples I have seen.
Jenkinsfile
pipeline
{
agent any
tools
{
maven 'Maven3'
jdk 'JDK8'
}
options
{
buildDiscarder(logRotator(numToKeepStr: '4'))
skipStagesAfterUnstable()
disableConcurrentBuilds()
}
triggers
{
// MINUTE HOUR DOM MONTH DOW
pollSCM('H 6-18/4 * * 1-5')
}
stages
{
stage('Clean')
{
steps
{
script
{
if (isUnix())
{
sh 'mvn --batch-mode clean'
}
else
{
bat 'mvn --batch-mode clean'
}
}
}
}
stage('Build')
{
steps
{
script
{
if (isUnix())
{
sh 'mvn --batch-mode compile'
}
else
{
bat 'mvn --batch-mode compile'
}
}
}
}
stage('UnitTests')
{
steps
{
script
{
if (isUnix())
{
sh 'mvn --batch-mode resources:testResources compiler:testCompile surefire:test'
}
else
{
bat 'mvn --batch-mode resources:testResources compiler:testCompile surefire:test'
}
}
}
post
{
always
{
junit testResults: 'target/surefire-reports/*.xml'
}
}
}
stage('Sanity check')
{
steps
{
script
{
if (isUnix())
{
sh 'mvn --batch-mode checkstyle:checkstyle pmd:pmd pmd:cpd com.github.spotbugs:spotbugs-maven-plugin:spotbugs'
}
else
{
bat 'mvn --batch-mode checkstyle:checkstyle pmd:pmd pmd:cpd com.github.spotbugs:spotbugs-maven-plugin:spotbugs'
}
}
}
}
stage('Packaging')
{
steps
{
script
{
if (isUnix())
{
sh 'mvn --batch-mode jar:jar'
}
else
{
bat 'mvn --batch-mode jar:jar'
}
}
}
}
stage('install local')
{
steps
{
script
{
if (isUnix())
{
sh 'mvn --batch-mode jar:jar source:jar install:install'
}
else
{
bat 'mvn --batch-mode jar:jar source:jar install:install' // maven-jar-plugin falseCreation default is false, so no doubled jar construction here, but required for maven-install-plugin internal data
}
}
}
}
stage('Documentation')
{
steps
{
script
{
if (isUnix())
{
sh 'mvn --batch-mode site'
}
else
{
bat 'mvn --batch-mode site'
}
}
}
post
{
always
{
publishHTML(target: [reportName: 'Site', reportDir: 'target/site', reportFiles: 'index.html', keepAll: false])
}
}
}
stage('Deploy test')
{
steps
{
script
{
if (isUnix())
{
// todo
}
else
{
bat returnStatus: true, script: 'sc stop Tomcat8'
sleep(time:30, unit:"SECONDS")
bat returnStatus: true, script: 'C:\\scripts\\clean.bat'
bat returnStatus: true, script: 'robocopy "target" "C:\\Program Files\\Apache Software Foundation\\Tomcat 9.0\\webapps" Test.war'
bat 'sc start Tomcat8'
sleep(time:30, unit:"SECONDS")
}
}
}
}
stage('Integration tests')
{
steps
{
script
{
if (isUnix())
{
sh 'mvn --batch-mode failsafe:integration-test failsafe:verify'
}
else
{
bat 'mvn --batch-mode failsafe:integration-test failsafe:verify'
}
}
}
}
}
}
Hopefully this is interesting for other developers outside there.
I will update this here when I significantly improve it over time.
For those who also wish to see a maven pom along with a Jenkinsfile please have a look at my small example project at github: TemplateEngine

I think there is no right answer, but the following example worked for us.
stage('Build and Unit Test') {
mvn clean deploy -> with unit tests, without integration tests, deploy local
deploy local:
You can define in a maven profile the distributionManagement like:
<distributionManagement>
<repository>
<id>localFile</id>
<url>file:target/repository/</url>
</repository>
<snapshotRepository>
<id>localFile</id>
<url>file:target/repository/</url>
</snapshotRepository>
</distributionManagement>
}
stage('Pre Integration Tests') {
The binaries are now in target/repository.
From there you can use the binaries as you like.
Copy them to a server, deploy them on an application server, etc.
}
stage('Integration Tests') {
maven failsafe:integration-test failsafe:verify
Already all tests are compiled, just execute them and verify the result.
}
stage('Deploy to Binary Repository (Nexus, Artifactory, etc)') {
Now if everything is ok, finally upload the Binaries.
For that we use wagon-maven-plugin
So from target/repository the files are uploaded to the Binary Repository.
}
So to wrap this up:
Fail fast. If a unit test has errors -> fail the build.
Only build once. Use the same binaries for test, deployment/integration test,
upload to repository, etc.
With that the stages are logical units,
which gives you enough feedback where to look for errors.

Related

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.

Pipeline created by Job-DSL fails to run maven

I am trying to setup Jenkins. I am using the docker image BlueOcean. I am trying to create a Jenkins pipeline using a Job-DSL. When I create the pipeline myself, and run it, it works. But when I run the pipeline created by the Job-DSL, it fails because of maven.
I looked on internet, but I couldn't find a solution proper to my case.
He is the Jenkinsfile
pipeline {
agent {
docker {
image 'maven:3-alpine'
args '-v /root/.m2:/root/.m2'
}
}
stages {
stage('Build') {
steps {
sh 'mvn -B -DskipTests clean package'
}
}
}
and this is the job-DSL
job('PROJ-unit-tests') {
scm {
git('git://github.com/Jouda-Hidri/Transistics.git') { node ->
node / gitConfigName('DSL User')
node / gitConfigEmail('hxxxa#gmail.com')
}
}
triggers {
scm('*/15 * * * *')
}
steps {
maven('-e clean test')
}
}

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'
}
}

How to run pipeline stage only when there are no test failures?

I'm using Jenkins in a multi-modular Maven project and I have three stages in my pipeline: build, test and deploy. I want to be able to run all unit tests during test stage and, when there are test failures, deploy stage should be skipped.
For now, I managed to find a workaround (which works as I want it to), but I had to explicitly approve usage of some methods, which are marked by Jenkins as vurnelabilities.
I am wondering if there is any clearer way to achieve my goals?
import hudson.tasks.test.AbstractTestResultAction
pipeline {
agent {
docker {
image 'maven:3-jdk-8-alpine'
args '--name maven3 -v /root/.m2:/root/.m2'
}
}
stages {
stage('Build') {
steps {
sh 'mvn clean install -DskipTests'
}
}
stage('Test') {
steps {
sh 'mvn test --fail-never'
}
post {
always {
junit '**/target/surefire-reports/*.xml'
}
}
}
stage('Deploy') {
steps {
script {
def AbstractTestResultAction testResultAction = currentBuild.rawBuild.getAction(AbstractTestResultAction.class)
if (testResultAction == null) {
error("Could not read test results")
}
def numberOfFailedTests = testResultAction.failCount
testResultAction = null
if (numberOfFailedTests == null || numberOfFailedTests != 0) {
error("Did not deploy. Number of failed tests: " + numberOfFailedTests)
}
sh 'mvn deploy -DskipTests'
}
}
}
}
}
In your test stage you execute: mvn test --fail-never
Now even if you have test failures maven will return with an exit code 0.
Jenkins is checking the exit code. if it is 0 it will continue with the next command.
so get rid of --fail-never

jenkins declarative pipeline not working for xl-deploy using maven command

i wanna create jenkins declarative pipeline for deploying on xl-deploy using maven command. i am not using xl-deploy plugin i am just using maven command for this.
pipeline {
agent {
label 'java8'
}
tools {
maven 'M3'
}
options {
skipDefaultCheckout()
timestamps()
disableConcurrentBuilds()
timeout(time: 1, unit: 'HOURS')
buildDiscarder(logRotator(numToKeepStr: "${env.BRANCH_NAME}"=='master'?'10':''))
}
environment {
ARTIFACTORY = credentials('artifactory-credentials')
CF = credentials('cf-credentials')
SONAR = credentials('Sonar_Credentials')
}
stages {
stage ('Checkout') {
steps {
checkout scm
sh "git rev-parse HEAD > .git/commit-id"
script {
commit_id = readFile('.git/commit-id').trim()
pom = readMavenPom file: 'pom.xml'
currentBuild.displayName = commit_id.take(7) + "-" + pom.version
}
}
}
stage ('Build') {
steps {
sh "mvn -U -s settings.xml -gs settings.xml clean install -DskipTests=true"
}
}
stage('Publish Artifacts') {
when {
branch 'master'
}
steps {
sh "echo 'Publish JAR to Artifactory !'"
sh "mvn -s settings.xml -gs settings.xml versions:set -DnewVersion=$commit_id"
sh "mvn -s settings.xml -gs settings.xml deploy -DskipTests=true"
}
}
stage('Deploy') {
steps {
sh "wget --user ${ARTIFACTORY_USR} --password ${ARTIFACTORY_PSW} -O ${pom.artifactId}.war -nv <repo url>/${pom.artifactId}/${commit_id}/${pom.artifactId}-${commit_id}.war --server-response --"
sh "mvn org.apache.maven.plugins:maven-dependency-plugin:2.8:copy -Dartifact=<app package>-$commit_id:war -DoutputDirectory=target -Dmdep.useBaseVersion=true"
}}
}
post {
always {
deleteDir()
}
}
}
i am getting following exception:
Failed to execute goal com.xebialabs.xldeploy:xldeploy-maven-plugin:5.0.2:generate-deployment-package.
till publish, it is working fine. but it is giving exception while executing deploy stage
I would suggest upgrading to version 6.0.1 of the plugin as that version fixes some connectivity issues.
The problem might also be related to an incorrect pom.xml file, however for that to exclude as the root cause, you should at least share your pom.xml, your XL Deploy version and the loaded plugins in XL Deploy.

Resources