0
0
JenkinsHow-ToBeginner · 4 min read

How to Build, Test, and Deploy Using Jenkins Pipeline

Use a Jenkins Pipeline with stages for Build, Test, and Deploy. Define these stages in a Jenkinsfile using declarative syntax to automate your software delivery process.
📐

Syntax

A Jenkins Pipeline uses a Jenkinsfile to define stages like Build, Test, and Deploy. Each stage contains steps that run commands or scripts. The pipeline runs these stages in order.

  • pipeline: Defines the pipeline block.
  • agent: Specifies where the pipeline runs (e.g., any available agent).
  • stages: Groups all stages.
  • stage: Defines a single stage like Build, Test, or Deploy.
  • steps: Commands executed in each stage.
groovy
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Building the application'
                // Add build commands here
            }
        }
        stage('Test') {
            steps {
                echo 'Running tests'
                // Add test commands here
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying the application'
                // Add deploy commands here
            }
        }
    }
}
💻

Example

This example Jenkinsfile shows a simple pipeline that builds, tests, and deploys a Node.js app. It runs shell commands to install dependencies, run tests, and deploy.

groovy
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Installing dependencies'
                sh 'npm install'
            }
        }
        stage('Test') {
            steps {
                echo 'Running unit tests'
                sh 'npm test'
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying to server'
                sh 'scp -r ./dist user@server:/var/www/app'
            }
        }
    }
}
Output
[Pipeline] echo Installing dependencies [Pipeline] sh + npm install ... (npm install output) ... [Pipeline] echo Running unit tests [Pipeline] sh + npm test ... (test output) ... [Pipeline] echo Deploying to server [Pipeline] sh + scp -r ./dist user@server:/var/www/app ... (deploy output) ...
⚠️

Common Pitfalls

  • Not specifying an agent causes the pipeline to fail because Jenkins doesn't know where to run.
  • Forgetting to add sh or bat before shell commands results in errors.
  • Running deploy commands without proper credentials or SSH keys causes deployment to fail.
  • Not separating build, test, and deploy steps into stages makes troubleshooting harder.
groovy
pipeline {
    stages {
        stage('Build') {
            steps {
                // Missing agent and sh command - wrong
                echo 'Building'
                'npm install'
            }
        }
    }
}

// Corrected version:
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Building'
                sh 'npm install'
            }
        }
    }
}
📊

Quick Reference

Remember these tips for Jenkins build-test-deploy pipelines:

  • Use pipeline and agent any to start.
  • Define clear stages for Build, Test, and Deploy.
  • Use sh or bat to run shell commands.
  • Keep credentials secure for deployment steps.
  • Check console output for errors after each stage.

Key Takeaways

Use a Jenkinsfile with declarative pipeline syntax to automate build, test, and deploy stages.
Always specify an agent and use proper shell commands inside steps.
Separate your pipeline into clear stages for easier debugging and maintenance.
Secure deployment credentials and verify connectivity before deploying.
Check Jenkins console output to quickly identify and fix errors.