0
0
Jenkinsdevops~5 mins

Steps within stages in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you automate software tasks with Jenkins, you organize work into stages. Each stage has steps that tell Jenkins exactly what to do. Steps are the small actions inside a stage that make your automation happen.
When you want to run commands like building code or running tests inside a specific part of your pipeline
When you need to break down a big task into smaller, clear actions inside a stage
When you want to see detailed progress and logs for each action Jenkins performs
When you want to add multiple commands or scripts to run one after another in a stage
When you want to control the order of operations inside your automation pipeline
Config File - Jenkinsfile
Jenkinsfile
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        echo 'Starting build process'
        sh 'echo Building the project'
      }
    }
    stage('Test') {
      steps {
        echo 'Running tests'
        sh 'echo Tests executed successfully'
      }
    }
  }
}

This Jenkinsfile defines a pipeline with two stages: Build and Test.

Each stage has a steps block that lists the commands Jenkins will run.

The echo commands print messages to the console for clarity.

The sh commands run shell commands on the agent machine.

Commands
Check that Jenkins job CLI tools are installed and ready to use.
Terminal
jenkins-jobs --version
Expected OutputExpected
jenkins-jobs 3.3.1
Start the Jenkins pipeline named 'my-pipeline' which uses the Jenkinsfile with stages and steps.
Terminal
jenkins-cli build my-pipeline
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] stage [Pipeline] { (Build) [Pipeline] echo Starting build process [Pipeline] sh + echo Building the project Building the project [Pipeline] } [Pipeline] stage [Pipeline] { (Test) [Pipeline] echo Running tests [Pipeline] sh + echo Tests executed successfully Tests executed successfully [Pipeline] } [Pipeline] End of Pipeline
View the console output of the pipeline run to see the steps executed inside each stage.
Terminal
jenkins-cli console my-pipeline
Expected OutputExpected
Starting build process Building the project Running tests Tests executed successfully
Key Concept

Steps are the individual commands inside a stage that tell Jenkins exactly what to do, making your automation clear and organized.

Common Mistakes
Putting commands directly inside a stage without wrapping them in a steps block
Jenkins requires steps to be inside the steps block; otherwise, the pipeline will fail to run.
Always put commands inside a steps { } block within each stage.
Using shell commands without the sh step in a declarative pipeline
Jenkins does not recognize raw shell commands outside the sh step, causing errors.
Wrap shell commands inside sh 'command' to run them properly.
Summary
Define stages in Jenkinsfile to organize your pipeline into parts.
Inside each stage, use the steps block to list commands Jenkins should run.
Use echo for messages and sh for shell commands inside steps.