Springboot Microservices Continuous Delivery With Docker And Jenkins Pipeline - spring-boot

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

Related

Running Jenkins job with different maven opts in parallel

community.
I would like to run Jenkins job, same job but with diferent maven opts values and in parallel. How can i achieve that? I was trying to use different Jenkins plugins, but with no luck.
Trying to configure pipelines using groovy scripts, but i am so amateur that i can't figure out how to achieve what i want. The goal is to run same jenkins job in parallel, but the only thing that must be different is environment where my tests should run.
Maybe there is already a solution so you could point me to that.
You should be able to use parallel blocks for this. Following is a sample.
pipeline {
agent none
stages {
stage('Run Tests') {
parallel {
stage('Test On Dev') {
agent {
label "IfYouwantToChangeAgent"
}
steps {
sh "mvn clean test -Dsomething=dev"
}
post {
always {
junit "**/TEST-*.xml"
}
}
}
stage('Test On QA') {
agent {
label "QA"
}
steps {
sh "mvn clean test -Dsomething=qa"
}
post {
always {
junit "**/TEST-*.xml"
}
}
}
}
}
}
}

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

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

How to use nested docker.image('test_image').inside() in a Jenkins pipeline

I have a jenkins container which triggers a maven container to build a java code.
Now I want to test the same code with selenium.
I have another container for selenium server.
But the command to run the test using selenium is a mvn command which needs Maven.
Since I am executing this inside the selenium container, mvn is not recognized.
How can I use maven container inside the selenium container ? Is nesting possible ?
I don't want to install everything in the same container.
This is the Jenkinsfile :
node {
stage('checkout') {
git credentialsId: 'Gitlab_cred', url: 'urlhere'
}
stage('Build') {
docker.image('mvn_custom_image').inside('-u root'){
//mvn commands to build the project
}
}
stage('Archive and Packaging') {
archiveArtifacts '**/*.war'
}
stage('Integration Test') {
docker.image('selenium/node-firefox:3.4.0-dysprosium').inside{
//I want to run mvn command using selenium here
// This image doesn't have mvn
}
}
}
Can I use "docker.image('mvn_custom_image').inside" inside another container (in my case selenium) ?
Or any other way which has a similar result.
There is good example on https://www.jenkins.io/doc/book/pipeline/docker/
node {
checkout scm
docker.image('mysql:5').withRun('-e "MYSQL_ROOT_PASSWORD=my-secret-pw"') { c ->
docker.image('mysql:5').inside("--link ${c.id}:db") {
/* Wait until mysql service is up */
sh 'while ! mysqladmin ping -hdb --silent; do sleep 1; done'
}
docker.image('centos:7').inside("--link ${c.id}:db") {
/*
* Run some tests which require MySQL, and assume that it is
* available on the host name `db`
*/
sh 'make check'
}
}
}

Resources