Jenkins Pipeline Guide
What You Will Learn
Jenkins automates build, test, and deployment workflows. This guide explains pipelines, agents, stages, credentials, artifacts, and deployment flow.
Prerequisites
- Git basics
- One Java or React project
- Basic CI/CD understanding
Concept Overview
A Jenkins pipeline is usually defined in a Jenkinsfile. It describes what Jenkins should do when code changes: checkout, install dependencies, run tests, build artifacts, create images, and deploy.
Step-by-Step Explanation
- Connect Jenkins to your Git repository.
- Create a
Jenkinsfile. - Define an agent.
- Add stages for checkout, build, test, package, and deploy.
- Store secrets in Jenkins credentials.
- Archive artifacts if needed.
- Add notifications for failed builds.
Code Example
pipeline {
agent any
stages {
stage('Install') {
steps { sh 'npm ci' }
}
stage('Test') {
steps { sh 'npm test -- --watch=false' }
}
stage('Build') {
steps { sh 'npm run build' }
}
}
}
Real-World Use Cases
- Building Spring Boot JAR files
- Running frontend lint and build checks
- Creating Docker images
- Pushing images to a registry
- Deploying to EC2, ECS, or Kubernetes
Best Practices
- Keep pipelines readable.
- Store credentials securely.
- Fail fast when tests or lint fail.
- Use separate stages for visibility.
- Avoid hardcoding environment-specific values.
- Keep deployment approval manual for production when needed.
Common Mistakes
- Putting secrets in the Jenkinsfile
- Running all commands in one large stage
- Ignoring failed tests
- Not cleaning workspaces
- Deploying to production from any branch
Interview Questions
- What is a Jenkinsfile?
- What is the difference between declarative and scripted pipeline?
- How do Jenkins credentials work?
- Why split a pipeline into stages?
- How can Jenkins deploy Dockerized applications?
Summary
Jenkins pipelines make delivery repeatable. Clear stages, secure credentials, and branch-based deployment rules are the foundation of reliable CI/CD.