0
0
Jenkinsdevops~5 mins

Pipeline stages and steps in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you want to automate your software build and deployment, you use a pipeline. Pipelines break the work into stages and steps. Stages group related tasks, and steps are the individual commands or actions inside those stages.
When you want to build your code, run tests, and deploy automatically in order.
When you want to see clear progress and results for each part of your automation.
When you want to stop the process early if a test or build fails.
When you want to organize your automation so it is easy to read and maintain.
When you want to run different tasks on different machines or environments.
Config File - Jenkinsfile
Jenkinsfile
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Building the application'
                sh 'echo Build commands go here'
            }
        }
        stage('Test') {
            steps {
                echo 'Running tests'
                sh 'echo Test commands go here'
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying the application'
                sh 'echo Deploy commands go here'
            }
        }
    }
}

pipeline: Defines the whole pipeline.

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

stages: Groups the different stages of the pipeline.

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

steps: Lists the commands or actions to run inside the stage.

Commands
Check if Jenkins CLI or Jenkins Job Builder is installed and accessible.
Terminal
jenkins-jobs --version
Expected OutputExpected
jenkins-job-builder 3.10.0
Start the Jenkins pipeline named 'my-pipeline' to run all stages and steps.
Terminal
jenkins-cli build my-pipeline
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] stage [Pipeline] { (Build) Building the application Build commands go here [Pipeline] } [Pipeline] stage [Pipeline] { (Test) Running tests Test commands go here [Pipeline] } [Pipeline] stage [Pipeline] { (Deploy) Deploying the application Deploy commands go here [Pipeline] } [Pipeline] End of Pipeline
View the console output of the last run of the 'my-pipeline' to check logs for each stage and step.
Terminal
jenkins-cli console my-pipeline
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] stage [Pipeline] { (Build) Building the application Build commands go here [Pipeline] } [Pipeline] stage [Pipeline] { (Test) Running tests Test commands go here [Pipeline] } [Pipeline] stage [Pipeline] { (Deploy) Deploying the application Deploy commands go here [Pipeline] } [Pipeline] End of Pipeline
Key Concept

If you remember nothing else from this pattern, remember: stages organize your pipeline into clear parts, and steps are the commands inside those parts.

Common Mistakes
Putting commands directly under stages without steps block
Jenkins pipeline syntax requires steps inside stages; missing steps causes syntax errors.
Always wrap commands inside a steps block within each stage.
Not naming stages clearly
Without clear stage names, it is hard to understand pipeline progress and logs.
Give each stage a descriptive name that explains its purpose.
Running long commands directly in steps without scripts
Long commands can be hard to read and maintain inside steps.
Use shell scripts or separate files for complex commands and call them from steps.
Summary
Define a Jenkinsfile with pipeline, agent, stages, and steps blocks.
Use stages to group related tasks like build, test, and deploy.
Inside each stage, use steps to run commands or scripts.
Run the pipeline and check console output to see each stage and step run.