How to Create a Jenkins Declarative Pipeline
Jenkins introduced a Declarative pipeline which allows one to break the whole process into different stages/jobs which trigger each other in a sequence.
Join the DZone community and get the full member experience.
Join For FreeJenkinsFile
A text file that contains the entire workflow as code that can be checked into SCM/VCS just like the rest of the code and triggers the pipeline if present otherwise the pipeline will be created.Go to the application and create a new file (right-click on the project -> New -> File) at the same level as the POM file, give the name as JenkinsfilePaste the code given below in that.
pipeline { agent any stages { stage('Build') { steps
{ bat 'mvn clean package' } } stage('Deploy') { steps
{ bat 'mvn deploy -DmuleDeploy' } } } }
Use sh for shell scripts and bat for Windows Batch scripts
Check-in the file along with the rest of the code.
Creating the Pipeline
Go to Jenkins -> New item.Select Pipeline and click OK.
On the next screen provide Description (optional). Select GitHub hook trigger for GITScm polling under Build Triggers and configure webhook in Github as shown in here.
Trigger the build.
stage('Deploy') { steps { input('Continue to Deploy?') bat 'mvn deploy -DmuleDeploy' } }
xxxxxxxxxx
stage('Deploy') { steps { timeout(time: 100, unit: 'SECONDS') { input('Continue to Deploy?') } bat 'mvn deploy -DmuleDeploy' } }\
The pipeline will wait for 100 seconds before aborting the pipeline if no input is provided.
If the timeout expires, the pipeline status will be marked as Aborted. To mark the build as Success even if the timeout expires or the user chooses to abort, add try/catch to suppress the error.xxxxxxxxxx
stage('Deploy') { steps { script { def proceed = true try { timeout(time: 100, unit: 'SECONDS') { input('Continue to Deploy?') } } catch (err) { proceed = false } if(proceed) { bat 'mvn deploy -DmuleDeploy' } } } }
The script block defines a variable proceed, then inside try block it sets the timeout for 100 seconds and asks for the input and if the timeout expires or pipeline is aborted it will go the catch block, set proceed to false and come out. If input provided is Proceed, the pipeline will continue as normal otherwise it will stop but the build status will be marked as Success.
Post Conditions
Pipelines can define one or more steps to be run after the pipeline's or stage's completion using post block. This is useful to run cleanup processes, send notifications depending on the stage's or pipeline's status. The condition blocks run in the following orderCondition | Description |
---|---|
always | Run the enclosed step(s) regardless of pipeline's or stage's completion status |
changed | Run the enclosed step(s) only when the pipeline's or stage's completion status is different than the previous run. |
fixed | Run the enclosed step(s) only when the pipeline's or stage's completion status is success and the previous run failed or was unstable. |
regression | Run the enclosed step(s) only when the pipeline's or stage's completion status is a failure, aborted or unstable, and the previous run was successful. |
aborted | Run the enclosed step(s) only when the pipeline's or stage's completion status is aborted. |
failure | Run the enclosed step(s) only when the pipeline's or stage's completion status is failed. |
success | Run the enclosed step(s) only when the pipeline's or stage's completion status is a success. |
unstable | Run the enclosed step(s) only when the pipeline's or stage's completion status is unstable. |
unsuccessful | Run the enclosed step(s) only when the pipeline's or stage's completion status is not a success. |
cleanup | Run the enclosed step(s) after all the postconditions have been evaluated. |
xxxxxxxxxx
post { always { echo 'Always Condition!' } changed { echo 'Changed Condition!' } fixed { echo 'Fixed Condition!' } regression { echo 'Regression Condition!' } aborted { echo 'Aborted Condition!' } failure { echo 'Failure Condition!' } success { echo 'Success Condition!' } unstable { echo 'Unstable Condition!' } unsuccessful { echo 'Unsuccessful Condition!' } cleanup { echo 'Cleanup Condition!' } }
Check in the code, the build will be triggered automatically. Cancel the pipeline run, go to console logs for the build.
Since the previous build was a success and this one is Aborted, postconditions always, changed, regression, aborted, unsuccessful, and cleanup were executed.Trigger the build again and let the pipeline finish, timeout or abort it by passing Abort when asked for input, the pipeline status will be Success as we have configured it for the same using try-catch
The declarative pipeline has many other directives like parallel executions, triggers when conditions, and more. For further reading, refer here
Published at DZone with permission of Abhay Yadav. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments