Allure reports in parallel test pipeline in Jenkins - maven

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

Related

Springboot Microservices Continuous Delivery With Docker And Jenkins Pipeline

I can't find any example or article how can the continuous delivery pipeline look like when we are using Spring Boot +Docker + Jenkins.
In our new project , We would like to deploy application as docker container in one of our VM’s . I written shell script for this in Jenkins and it's working fine.
The shell script has below steps
checkout branch from git
Zip the project folder
Move it to VM using SCP
Login to VM using ssh
Unzip the project folder
docker-compose stop
docker-compose build
But, I we would like to use Jenkins pipeline . Could anyone help with article or Jenkinsfile steps to achieve the same.
Thanks ,
Madhu.
You can use jenkins declarative pipeline to achive your goal.
Here is one sample example that will give you a little bit idea
pipeline {
agent {
label 'some_build_node'
}
stages {
stage ('Git Checkout') {
steps {
checkout scm
}
}
stage ('Build & Push Docker Image') {
steps {
// You can use this section to build and push image to registry
}
}
stage ('Deploy') {
steps {
// Your steps to perform the deployment here
}
}
}
}
post {
always {
// this block is to perform post build actions such as publishing the test report
publishHTML target: [
allowMissing: false,
alwaysLinkToLastBuild: false,
keepAll: true,
reportDir: 'build/reports/tests/test',
reportFiles: 'index.html',
reportName: 'Junit Report'
]
}
success {
// action you would want to do upon success. For example notifying about the build on slack/email
}
failure {
// action you would want to do upon failure. For example notifying about the build on slack/email
}
}
}

Maven issue while creating Jenkins Pipeline

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

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.

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

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.

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