Jenkins Declarative Pipeline
Estimated time to read: 4 minutes
Declarative Pipeline presents a more simplified and opinionated syntax on top of the Pipeline sub-systems. In order to use them, install the Pipeline: Declarative Plugin.
All valid Declarative Pipelines must be enclosed within a pipeline
block, for example
Welcome Pipeline
pipeline {
agent any
stages {
stage('First Stage') {
steps {
echo 'Welcome to Opsfusion labs'
}
}
}
}
Input Parameter
This allows you to prompt for user input or pass specific values when triggering the job.
Here’s an example of your pipeline with an input parameter:
pipeline {
agent any
stages {
stage('Example') {
input {
message "Should we continue To Update Docker Image Name?"
ok "Yes, we should."
submitter "alice,bob"
parameters {
string(name: 'DOCKER_IMAGE_NEW_TAG', defaultValue: '12', description: 'Update Webserver Docker Image Tag')
}
}
steps {
echo "docker image tag webserver:${DOCKER_IMAGE_NEW_TAG} opsfusionlabs/webserver:${DOCKER_IMAGE_NEW_TAG}"
}
}
}
}
String Parameter
The string
parameter is used to pass a text input to a Jenkins pipeline. Here's how you can incorporate a string
parameter in your pipeline:
Here’s an example of your pipeline with an string parameter:
pipeline {
agent any
parameters {
string(name: 'USER_NAME', defaultValue: 'Guest', description: 'Enter your name')
}
stages {
stage('Welcome Step') {
steps {
echo "Hello, ${params.USER_NAME}! Welcome to OpsfusionLabs."
}
}
}
}
Choice Parameter
To use a choice parameter in a Jenkins Declarative Pipeline, you can define it using the choice
directive in the parameters
block. A choice parameter allows users to select from a predefined list of options when triggering the build.
pipeline {
agent any
parameters {
choice(name: 'Environment_type', choices: ['dev', 'pre-prod', 'qa'], description: 'Select a Environment')
}
stages {
stage('Environment Step') {
steps {
script {
def greetingenvironmentName
switch (params.Environment_type) {
case 'dev':
greetingenvironmentName = 'Application is going to deply in Dev Env'
break
case 'qa':
greetingenvironmentName = 'Application is going to deply in Qa Env'
break
case 'pre-prod':
greetingenvironmentName = 'Application is going to deply in Pre-prod Env'
break
}
echo greetingenvironmentName
}
}
}
}
}
Environment Variables
In a Jenkinsfile, you can define environment variables that can be used throughout your pipeline. These variables can be set at the global level, stage level, or within specific steps.
pipeline {
agent any
environment {
// Global environment variables
APP_NAME = 'MyApplication'
BUILD_VERSION = '1.0.0'
}
stages {
stage('Build') {
environment {
// Stage-specific environment variable
BUILD_ENV = 'development'
}
steps {
echo "Building ${env.APP_NAME} version ${env.BUILD_VERSION} in ${env.BUILD_ENV} environment."
// You can also set an environment variable dynamically
script {
env.DYNAMIC_VAR = "Dynamic value set during build"
echo "Dynamic variable: ${env.DYNAMIC_VAR}"
}
}
}
stage('Test') {
steps {
echo "Running tests for ${env.APP_NAME} version ${env.BUILD_VERSION}."
}
}
}
}
Tools
-
Maven Tool Configuration
- Go to Dashboard > Manage Jenkins > Global Tools Configuration
- Scroll down to the Maven section & click on Maven Installations. Click on Add Installer button & choose Install from Apache.
we can specify tools in Jenkins pipeline will use by declaring them in the tools
section of in Jenkinsfile. This is especially useful for configuring build tools, such as JDKs, Maven, Gradle, or others, that your pipeline may need.
Here’s an example of how to add tools information into your Jenkins Declarative Pipeline:
pipeline {
agent any
tools {
// Specify the tools required for the pipeline
maven 'maven-3.9.6' // Example of a Maven tool
}
stages {
stage('checking maven Version') {
steps {
sh 'mvn --version' // Running a Maven version command
}
}
}
}
Adding credentials in pipeline
To add credentials securely in a Jenkins pipeline, we can use the environment
block along with the withCredentials
step. This allows you to inject credentials into your pipeline as environment variables, which can then be used in your build steps.
-
Adding Credentials in environment variable
-
Adding Credentials in specific stage