0% found this document useful (0 votes)
52 views6 pages

Jenkins Pipeline with Boolean Parameters

This Jenkinsfile defines a CI/CD pipeline for a Java project. The pipeline contains stages for checking out code, building, running tests, deploying to integration and pre-production environments, releasing to production, and more. Conditionals are used to optionally skip certain stages. Environment variables and parameters are used to configure the builds.

Uploaded by

aramis
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views6 pages

Jenkins Pipeline with Boolean Parameters

This Jenkinsfile defines a CI/CD pipeline for a Java project. The pipeline contains stages for checking out code, building, running tests, deploying to integration and pre-production environments, releasing to production, and more. Conditionals are used to optionally skip certain stages. Environment variables and parameters are used to configure the builds.

Uploaded by

aramis
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

pipeline {

agent { label 'agent1' }

parameters{

booleanParam(
defaultValue: false,
description: 'Skip deploy integ',
name: 'skip_deploy_integ'
)
booleanParam(
defaultValue: false,
description: 'Skip perf',
name: 'skip_perf'
)
booleanParam(
defaultValue: true,
description: 'Skip release',
name: 'skip_release'
)
booleanParam(
defaultValue: false,
description: 'Skip sonar',
name: 'skip_sonar'
)
booleanParam(
defaultValue: true,
description: 'Skip deploy preprod',
name: 'skip_deploy_preprod'
)

string(
defaultValue: '',
description: 'nouvelle version projet exemple : 0.0.1-SNAPSHOT',
name: 'newVersion'
)

environment {

sonar_host_url="[Link]

sonar_coverage_exclusions="**/*[Link],**/*[Link],**/*[Link]
,**/*[Link]"

sonar_duplication_exclusions="**/*[Link],**/*[Link],**/*Query.j
ava,**/*[Link]"
def release_version = getReleaseVersion()

options {
buildDiscarder(logRotator(numToKeepStr: '6', artifactNumToKeepStr: '6'))
}
triggers {
cron('H 01 * * 1-7')
}
stages {
stage ('Checkout Perf'){
steps{
sh 'ls -al'
sshagent(['robotops']) {
sh 'git clone git@[Link]:rs/[Link]'
}
sh 'ls -al'

}
}

stage('Build api') {
steps{
//compilation
sh 'mvn -e -s /var/jenkins_home/mvn/[Link] -gs
/var/jenkins_home/mvn/[Link] -[Link]=true clean install -U'

//tests unitaires
sh 'mvn -e -s /var/jenkins_home/mvn/[Link] -gs
/var/jenkins_home/mvn/[Link] test -PTest-All'
junit(allowEmptyResults: true, testResults: '**/surefire-
reports/*.xml')

script {
if (params.skip_sonar == false) {

//sonar couverture TU sans TI


sonar_exec('tu',null,null)

}
}

//déploiement nexus
sh 'mvn -e -s /var/jenkins_home/mvn/[Link] -gs
/var/jenkins_home/mvn/[Link] -[Link]=true deploy'
}
}

/* stage('Notify the world') {


steps{
echo 'notify'
//build job: 'flotte-app-frontend', propagate: true, wait: false
}
}*/

stage('Deploy API integ') {


when {
expression { params.skip_deploy_integ == false }
}
steps {

script {
def build_version = getCurrentVersion()

sh """ssh -i /var/jenkins_home/ssh_keys/id_rsa_root_87 -T -o
StrictHostKeyChecking=no root@[Link] << EOF
cd /etc/ansible

if [ ! -d "deploy" ] ; then
git clone
"git@[Link]:digitale/config/[Link]" "deploy"

cd deploy
else
cd deploy
git pull
fi

cd /etc/ansible/deploy

ansible-playbook -i inventory/integ/inventory -s api/mrh/[Link]


--extra-vars "mvn_repo_type=snapshots mvn_version=${build_version}"

exit
EOF"""

}
}
}

stage('Api cucumber & perf') {


parallel {
stage('Api cucumber') {
steps {

//Cucumber
sh 'mvn -e -s /var/jenkins_home/mvn/[Link] -gs
/var/jenkins_home/mvn/[Link] verify -PUAT -[Link]=true'
cucumber fileIncludePattern: 'target/cucumber-report/*.json',
sortingMethod: 'ALPHABETICAL'

script {
if (params.skip_sonar == false) {
//sonar api
sonar_exec('',null,null)
}
}
}
}

stage('Api Perf') {
when {
expression { params.skip_perf == false }
}
steps {
script {
try {
dir(path: 'api-rs-perf') {
sh 'pwd'
sh 'mvn -U -e -s /var/jenkins_home/mvn/[Link]
-gs /var/jenkins_home/mvn/[Link] clean install
-[Link]=[Link]
-[Link]=true'
}
}
catch (exc) {
}
}
gatlingArchive()
step([$class: 'JUnitResultArchiver', testResults:
'**/target/gatling/assertions-*.xml'])
}
}
}
}

stage('Release') {
when {
expression { params.skip_release == false }
}
steps{
script {
def new_version = getNewVersion()

sshagent(['robotops']) {
sh "git config --global [Link]
robotops@[Link]"

try{
sh "mvn -e -s /var/jenkins_home/mvn/[Link] -gs
/var/jenkins_home/mvn/[Link] release:clean release:prepare release:perform
-DreleaseVersion=${release_version}-Final -DdevelopmentVersion=${new_version}-
SNAPSHOT -DignoreSnapshots=true"
}catch(exc){
sh "mvn -e -s /var/jenkins_home/mvn/[Link] -gs
/var/jenkins_home/mvn/[Link] release:rollback"
throw new Exception(exc)
}
}
}
}
}

stage('Deploy preprod') {
when {
expression { params.skip_deploy_preprod == false }
}
steps {
echo "Pass ${currentVersion} to Preprod Stage"
sh """ssh -T -o StrictHostKeyChecking=no preprod@[Link] << EOF
cd /home/preprod

if [ ! -d "ansible" ] ; then
git clone
"git@[Link]:digitale/config/[Link]" "ansible"

cd ansible
else
cd ansible
git pull
fi

cd /home/preprod

if [ ! -d "inventory" ] ; then
git clone
"git@[Link]:digitale/config/[Link]" "inventory"

cd inventory
else
cd inventory
git pull
fi

cd /home/preprod/ansible

ansible-playbook -i /home/preprod/inventory/invontory -s
api/mrh/[Link] --extra-vars "mvn_repo_type=releases mvn_version=$
{release_version}-Final"

exit
EOF"""
}
}

stage('Clean') {
steps {
cleanWs()
}
}
}
tools {
maven 'apache-maven-3.5.2'
jdk 'jdk8'
}

def sonar_exec(sonar_branch,sonar_exclusions,sonar_inclusions) {
commande = "mvn -e -s /var/jenkins_home/mvn/[Link] -gs
/var/jenkins_home/mvn/[Link] sonar:sonar -[Link]=${sonar_host_url}
-[Link]=${sonar_branch} -[Link]=$
{sonar_coverage_exclusions} -[Link]=${sonar_duplication_exclusions}"

if(sonar_exclusions!=null){
commande = commande + " -[Link]=${sonar_exclusions}"
}

if(sonar_inclusions!=null){
commande = commande + " -[Link]=${sonar_inclusions}"
}
try {
sh commande;
}
catch (exc) {
}

def getCurrentVersion(){

def pom = readMavenPom file: '[Link]'


return [Link]

def getReleaseVersion(){

return getCurrentVersion().minus('-SNAPSHOT')

def getNewVersion(){

if([Link]() == ''){
def value = getReleaseVersion().tokenize('.')
//return value[0] + value[1] + value[2]++
def value1 = value[0]
def value2 = value[1]
def increment = value[2].toInteger()+1

return "${value1}.${value2}.${increment}"

}else {
return [Link]()
}

You might also like