Scripted Pipeline
- Directly allows users to write Groovy
- Learning curve towards Groovy
- Main blocks/steps are
- Node
- Stage
- git
- sh
- bat
- Example
node('mvn') {
stage('scm') {
git 'https://github.com/springpetclinic.git'
}
stage('build') {
if env.branch == 'master'
sh 'mvn package'
else
sh 'mvn clean package'
}
}
Declarative Pipeline
- Created Declarative Syntax for Jenkins (DSL)
- Simpler learning Curve
- Declarative Steps are
- pipeline
- stages
- stage
- label
- when
- agent
- sh
- git
- Example
pipeline {
agent {
label 'MVN'
}
stages {
stage ('git') {
....
}
stage ('build') {
......
when branch == 'master'
....
}
}
}
- Refer Here for specifics
Configuring Tools to Jenkins
- Configure Tools like
- git
- Maven
- Docker
- Ant
- Gradle
- Artifactory
- SonarQube
Multi Branch Pipeline
- Ensure every branch has a Jenkinsfile and Jenkins automatically indexes all the branches of your project.
- Multi branches builds can be triggered at one shot.
- Navigate to New-Item => Multi Branch Pipeline
- Best practice is to implement git hooks to scan multi branch pipeline.
Writing Jenkins Declarative Pipelines
- Refer Here for official docs
- Basic Pipeline Sytnax
pipeline {
// pipeline stuff
}
- Agent: Where the job has to run
// run anywhere
agent any
// run on the agents with label MAVEN
agent {
label 'MAVEN'
}
- Stages: configure sequence of stages. Example – do git clone and then mvn package
pipeline {
agent any
stages {
stage('git') {
git 'https://github.com/asquarezone/game-of-life.git'
}
stage('mvn') {
sh 'mvn clean package'
}
}
}
- Parallel execution of stages
parallel {
stage('act1') {
steps {
echo 'act1'
}
}
stage('act2') {
steps {
echo 'act2'
}
}
}
- when: Conditional check to execute stage or not
- env: To access jenkins environmental variables use
env.<variable name>
in scripted pipeline - parameters: To set parameters in declarative pipelines. Refer Here
pipeline {
agent any
parameters {
string(name: 'learning' )
}
stages {
stage('test') {
steps {
echo '${params.learning} is the parameter'
}
}
}
}
- Build Triggers: Refer Here
- Input: Refer Here
- Also refer Globals Section from Pipeline Syntax Page
- Also refer Steps
pipeline {
agent any
parameters {
string(name: 'purpose',default: 'learning')
}
stages {
stage('git'){
git url: 'https://github.com/spc.git', branch: 'sprint-2'
}
stage('build the code'){
sh script: 'mvn package', label: 'build'
}
stage('archive the artifacts'){
archiveArtifacts archive: 'target/*.jar', onlyIfSuccessful: true
}
}
}