0
0
Jenkinsdevops~5 mins

Build, test, deploy stages concept in Jenkins - Commands & Configuration

Choose your learning style9 modes available
Introduction
When creating software, you need to build the code, check it works, and then send it to users. Jenkins helps automate these steps in order: build, test, and deploy. This makes sure your software is reliable and ready to use.
When you want to automatically compile your code after every change to catch errors early
When you need to run tests to verify your software works before sharing it
When you want to send your software to a server or cloud automatically after tests pass
When you want to save time by automating repetitive steps in software delivery
When you want to keep your team updated with the latest working version of your app
Config File - Jenkinsfile
Jenkinsfile
pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        echo 'Building the application...'
        sh 'echo Build step simulated'
      }
    }
    stage('Test') {
      steps {
        echo 'Running tests...'
        sh 'echo Test step simulated'
      }
    }
    stage('Deploy') {
      steps {
        echo 'Deploying the application...'
        sh 'echo Deploy step simulated'
      }
    }
  }
}

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

agent any means the pipeline can run on any available Jenkins worker.

Each stage has steps that run shell commands simulating the real work. The echo commands show messages in the Jenkins console.

Commands
Check that Jenkins job CLI tool is installed and working before creating jobs.
Terminal
jenkins-jobs --version
Expected OutputExpected
jenkins-jobs 3.3.1
Create a Jenkinsfile with build, test, and deploy stages to define the pipeline steps.
Terminal
echo 'pipeline { agent any; stages { stage("Build") { steps { echo "Building the application..."; sh "echo Build step simulated" } } stage("Test") { steps { echo "Running tests..."; sh "echo Test step simulated" } } stage("Deploy") { steps { echo "Deploying the application..."; sh "echo Deploy step simulated" } } } }' > Jenkinsfile
Expected OutputExpected
No output (command runs silently)
Start the Jenkins pipeline job named 'my-pipeline' which runs the build, test, and deploy stages.
Terminal
jenkins-cli build my-pipeline
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] echo Building the application... [Pipeline] sh + echo Build step simulated Build step simulated [Pipeline] echo Running tests... [Pipeline] sh + echo Test step simulated Test step simulated [Pipeline] echo Deploying the application... [Pipeline] sh + echo Deploy step simulated Deploy step simulated [Pipeline] End of Pipeline Finished: SUCCESS
View the console output of the last run of the 'my-pipeline' job to verify each stage ran correctly.
Terminal
jenkins-cli console my-pipeline
Expected OutputExpected
[Pipeline] Start of Pipeline [Pipeline] echo Building the application... [Pipeline] sh + echo Build step simulated Build step simulated [Pipeline] echo Running tests... [Pipeline] sh + echo Test step simulated Test step simulated [Pipeline] echo Deploying the application... [Pipeline] sh + echo Deploy step simulated Deploy step simulated [Pipeline] End of Pipeline Finished: SUCCESS
Key Concept

If you remember nothing else from this pattern, remember: organizing your pipeline into build, test, and deploy stages helps catch problems early and deliver software reliably.

Common Mistakes
Skipping the test stage or not failing the pipeline when tests fail
This lets broken code reach users, causing errors and bad experience.
Always include tests and configure the pipeline to stop if tests fail.
Running deploy before tests finish or succeed
Deploying untested code risks introducing bugs to production.
Make deploy stage depend on successful completion of test stage.
Not using stages in Jenkinsfile and putting all steps in one block
Makes it hard to see progress and debug failures in the pipeline.
Use separate stages for build, test, and deploy for clarity and control.
Summary
Create a Jenkinsfile with separate build, test, and deploy stages to organize your pipeline.
Run the pipeline job to automate building, testing, and deploying your application.
Check the pipeline console output to verify each stage completed successfully.