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

Jenkins File Sample

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)
224 views6 pages

Jenkins File Sample

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
You are on page 1/ 6

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="http://192.168.1.77:29000/"

sonar_coverage_exclusions="**/*Representation.java,**/*Commande.java,**/*Query.java
,**/*Dto.java"

sonar_duplication_exclusions="**/*Representation.java,**/*Commande.java,**/*Query.j
ava,**/*Dto.java"
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 [email protected]:rs/api-rs-perf.git'
}
sh 'ls -al'

}
}

stage('Build api') {
steps{
//compilation
sh 'mvn -e -s /var/jenkins_home/mvn/settings.xml -gs
/var/jenkins_home/mvn/settings.xml -Dmaven.test.skip=true clean install -U'

//tests unitaires
sh 'mvn -e -s /var/jenkins_home/mvn/settings.xml -gs
/var/jenkins_home/mvn/settings.xml 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/settings.xml -gs
/var/jenkins_home/mvn/settings.xml -Dmaven.test.skip=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 [email protected] << EOF
cd /etc/ansible

if [ ! -d "deploy" ] ; then
git clone
"[email protected]:digitale/config/ansible.git" "deploy"

cd deploy
else
cd deploy
git pull
fi

cd /etc/ansible/deploy

ansible-playbook -i inventory/integ/inventory -s api/mrh/site.yml


--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/settings.xml -gs
/var/jenkins_home/mvn/settings.xml verify -PUAT -Dmaven.test.failure.ignore=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/settings.xml
-gs /var/jenkins_home/mvn/settings.xml clean install
-Dgatling.simulationClass=ma.wafaassurance.simulations.TarifRapideSimulation
-Dgatling.useOldJenkinsJUnitSupport=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 user.email
[email protected]"

try{
sh "mvn -e -s /var/jenkins_home/mvn/settings.xml -gs
/var/jenkins_home/mvn/settings.xml 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/settings.xml -gs
/var/jenkins_home/mvn/settings.xml 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 [email protected] << EOF
cd /home/preprod

if [ ! -d "ansible" ] ; then
git clone
"[email protected]:digitale/config/ansible.git" "ansible"

cd ansible
else
cd ansible
git pull
fi

cd /home/preprod

if [ ! -d "inventory" ] ; then
git clone
"[email protected]:digitale/config/invontory-preprod.git" "inventory"

cd inventory
else
cd inventory
git pull
fi

cd /home/preprod/ansible

ansible-playbook -i /home/preprod/inventory/invontory -s
api/mrh/site.yml --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/settings.xml -gs
/var/jenkins_home/mvn/settings.xml sonar:sonar -Dsonar.host.url=${sonar_host_url}
-Dsonar.branch=${sonar_branch} -Dsonar.coverage.exclusions=$
{sonar_coverage_exclusions} -Dsonar.cpd.exclusions=${sonar_duplication_exclusions}"

if(sonar_exclusions!=null){
commande = commande + " -Dsonar.exclusions=${sonar_exclusions}"
}

if(sonar_inclusions!=null){
commande = commande + " -Dsonar.inclusions=${sonar_inclusions}"
}
try {
sh commande;
}
catch (exc) {
}

def getCurrentVersion(){

def pom = readMavenPom file: 'pom.xml'


return pom.version

def getReleaseVersion(){

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

def getNewVersion(){

if(params.newVersion.trim() == ''){
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 params.newVersion.trim()
}

You might also like