0
0
Jenkinsdevops~5 mins

Groovy syntax in pipelines in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
Groovy is the language used to write Jenkins pipelines. It helps automate tasks like building, testing, and deploying software by defining steps in a simple script.
When you want to automate your software build and deployment process in Jenkins.
When you need to define multiple steps that run in order, like compiling code, running tests, and deploying.
When you want to reuse code blocks or variables inside your Jenkins pipeline.
When you want to add conditions to run certain steps only if specific criteria are met.
When you want to organize your pipeline with stages and parallel tasks.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Building the project'
            }
        }
        stage('Test') {
            steps {
                echo 'Running tests'
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying application'
            }
        }
    }
}

This Jenkinsfile defines a simple pipeline with three stages: Build, Test, and Deploy.

pipeline: The main block that defines the pipeline.

agent any: Runs the pipeline on any available Jenkins agent.

stages: Contains all the stages of the pipeline.

stage('Name'): Defines a step group with a name.

steps: Contains the commands or actions to run in that stage.

echo: Prints a message to the Jenkins console output.

Commands
Check if Jenkins CLI or Jenkins Job Builder is installed to interact with Jenkins pipelines.
Terminal
jenkins-jobs --version
Expected OutputExpected
jenkins-job-builder 3.9.0
View the Jenkinsfile content to understand the pipeline script written in Groovy syntax.
Terminal
cat Jenkinsfile
Expected OutputExpected
pipeline { agent any stages { stage('Build') { steps { echo 'Building the project' } } stage('Test') { steps { echo 'Running tests' } } stage('Deploy') { steps { echo 'Deploying application' } } } }
Trigger the Jenkins pipeline named 'my-pipeline' to run the Groovy script and execute the defined stages.
Terminal
jenkins-cli build my-pipeline
Expected OutputExpected
Started build #1 Waiting for build to complete... Finished: SUCCESS
--wait - Waits for the build to finish before returning control
View the console output of build number 1 to see the Groovy script execution results and messages.
Terminal
jenkins-cli console my-pipeline 1
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] echo Building the project [Pipeline] echo Running tests [Pipeline] echo Deploying application [Pipeline] End of Pipeline Finished: SUCCESS
Key Concept

If you remember nothing else from this pattern, remember: Jenkins pipelines use Groovy scripts to define clear, step-by-step automation for building, testing, and deploying software.

Common Mistakes
Writing pipeline steps without proper indentation or braces.
Groovy syntax requires correct structure; otherwise, Jenkins will fail to parse the Jenkinsfile.
Always use consistent indentation and enclose blocks with braces {} as shown in the example.
Using shell commands directly without the 'sh' step in scripted pipelines.
Jenkins expects shell commands inside 'sh' steps to execute them properly.
Wrap shell commands inside 'sh' blocks, for example: sh 'echo Hello'.
Forgetting to define an agent in the pipeline.
Without an agent, Jenkins does not know where to run the pipeline steps.
Always specify 'agent any' or a specific agent to run the pipeline.
Summary
Jenkins pipelines use Groovy syntax to automate software build, test, and deployment steps.
A Jenkinsfile defines stages and steps with clear structure using pipeline, agent, stages, stage, and steps blocks.
Commands include viewing the Jenkinsfile, triggering the pipeline, and checking the console output to verify execution.