I wanted to translate some jobs to the new Jenkins 2.0 declarative pipelines. At the moment they are 3 different jobs:
For this purpose, I have a small project in spring with maven that will be the best example for me to start (Simple, easy and fast to be built).
At the moment I have already a Multibranch pipeline for the CI build but I want to integrate into this job a Nightly and Sprintly build.
At the moment I have this JenkinsFile
pipeline {
agent {
label 'master'
}
tools {
maven "Apache Maven 3.3.9"
jdk "Java JDK 1.8 U102"
}
triggers {
cron ('H(06-08) 01 * * *')
pollSCM('H/5 * * * *')
}
stages {
stage('Build') {
steps {
sh 'mvn -f de.foo.project.client/ clean package'
}
post {
always {
junit allowEmptyResults: true, testResults: '**/target/surefire-reports/*.xml'
archiveArtifacts allowEmptyArchive: true, artifacts: '**/target/*.war'
}
}
}
stage('Deploy') {
sh 'echo "Deploy only master"'
}
}
It runs every branch when something is pull to Git and also Run around 1 oclock in the night (but still running all the branches).
Any idea or hint to do that? The code to do the deployment is not required I only want to know how to filter/split this branches being in the same JenkinsFile
Many thanks to all!
Edit: I can also use but It will run all the branches in the night (can I made this filter only for the Cron job?)
stage('DeployBranch') {
when { branch 'story/RTS-525-task/RTS-962'}
steps {
sh 'echo "helloDeploy in the branch"'
}
}
stage('DeployMaster') {
when { branch 'master'}
steps {
sh 'echo "helloDeploy in the master"'
}
}
After reading my own question four months later I realize that I was totally wrong in how I try setup the triggers and the jobs. We should have 3 different jobs:
The pipeline should be kept at simple like :
pipeline {
agent {
label 'master'
}
tools {
maven "Apache Maven 3.3.9"
jdk "Java JDK 1.8 U102"
}
stages {
stage('Build') {
steps {
sh 'mvn -f de.foo.project.client/ clean package'
}
post {
always {
junit allowEmptyResults: true, testResults: '**/target/surefire-reports/*.xml'
archiveArtifacts allowEmptyArchive: true, artifacts: '**/target/*.war'
}
}
}
stage('Deploy') {
when (env.JOB_NAME.endsWith('nightly')
sh 'echo "Deploy on nighlty"'
when (env.JOB_NAME.endsWith('sprintly')
sh 'echo "Deploy only sprintly"'
}
}
If you have any questions let me know and I will be happy to help :)