Jenkins introduced a Declarative pipeline which allows one to break the whole process into different stages/jobs which trigger each other in a sequence. The key feature is Pipeline-as-a-code which allows us to define the entire pipeline flow as code in a text file.

JenkinsFile

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' } } } }

The script starts with a pipeline tag that contains the entire process of the workflow. The agent specifies where the entire Pipeline, or a specific stage, will execute in the Jenkins environment depending on where the agent section is placed, it must be defined at the top level. Agents can be defined at stage level also. The next block, stages contain all the work that needs to be carried out. It specifies one or more stage which is executed sequentially.

Each **stage **performs a specific task. The first stage “Build” simply builds the project and creates a JAR file that is deployed to the Cloudhub by the second stage.

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.create a pipeline

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.

In the Pipeline section, select Pipeline script from SCM, select the SCM (selected Git here) and provide the repository URL and credentials. Specify the script path, by default the path is for the root level, change if Jenkinsfile is not at the root level.pipeline definitionClick Save.

Trigger the build.demo-pipeline

Pipelines can be configured for human input or approval before continuing further.

Java

1

 stage('Deploy') { steps { input('Continue to Deploy?') bat 'mvn deploy -DmuleDeploy' } } 

SCMThe pipeline will wait for an unspecified amount of time for user input. This can be changed by introducing a timeout in the step.

Java

1

 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.

proceed or deploy

If the timeout expires, the pipeline status will be marked as Aborted.

#tutorial #devops #integration #jenkins #mule 4 #pipeline as code #declarative pipelines

How to Create a Jenkins Declarative Pipeline
1.90 GEEK