Table of Contents
- Prerequisites
- Understanding Jenkins Pipelines
- Setting Up Your First Pipeline: Step-by-Step
- Troubleshooting Common Issues
- Advanced Tips for Beginners
- Conclusion
- References
Prerequisites
Before diving in, ensure you have the following:
- Jenkins Installed: Follow the official installation guide for your OS (Windows, macOS, or Linux). Jenkins requires Java 11 or later, so install Java first if needed.
- A Code Repository: Host your project on a version control system like GitHub, GitLab, or Bitbucket. We’ll use GitHub in this example.
- Basic Command-Line Knowledge: Familiarity with
gitcommands (e.g.,clone,commit) and your project’s build tools (e.g.,npm,maven,pip). - Jenkins Plugins: After installing Jenkins, install these essential plugins via Manage Jenkins > Plugins:
- Pipeline
- Git
- NodeJS (if your project uses Node.js; install plugins for your build tool, e.g., Maven Integration for Java projects).
Understanding Jenkins Pipelines
What is a Jenkins Pipeline?
A Jenkins Pipeline is a suite of plugins that lets you define your entire build, test, and deployment workflow as code. Instead of manually configuring jobs in the Jenkins UI, you write a script (called a Jenkinsfile) that lives in your project’s repo. This “pipeline-as-code” approach ensures version control, reusability, and consistency across teams.
Declarative vs. Scripted Pipelines
Jenkins supports two pipeline syntaxes:
| Declarative Pipeline | Scripted Pipeline |
|---|---|
| Structured, opinionated syntax. | Flexible, unstructured Groovy syntax. |
| Easier for beginners (we’ll use this!). | Better for complex, custom workflows. |
Uses pipeline block as the root. | Uses node block as the root. |
For this guide, we’ll focus on Declarative Pipeline because it’s simpler and more readable for beginners.
Key Pipeline Concepts
Let’s define core terms you’ll see in your Jenkinsfile:
- Agent: Specifies where the pipeline runs (e.g., on the Jenkins server itself, a Docker container, or a remote “agent” machine).
- Stages: Major phases of your pipeline (e.g., “Checkout Code,” “Build,” “Test,” “Deploy”).
- Steps: Individual tasks within a stage (e.g.,
git clone,npm install,npm test). - Post: Actions to run after the pipeline completes (e.g., sending Slack notifications on success/failure).
Setting Up Your First Pipeline: Step-by-Step
Step 1: Prepare Your Project
For this example, we’ll use a simple Node.js project with the following structure:
my-node-app/
├── src/
│ └── index.js
├── tests/
│ └── test.js
├── package.json
└── Jenkinsfile <-- We’ll create this!
If you don’t have a project, fork this sample Node.js repo (replace your-username with your GitHub handle) to follow along.
Step 2: Write Your First Jenkinsfile
The Jenkinsfile is the heart of your pipeline. It defines your workflow as code. Let’s create one in your project root.
Example Jenkinsfile (Node.js Project)
pipeline {
agent any // Run on any available agent (we’ll use the Jenkins server for simplicity)
tools {
nodejs 'NodeJS_18' // Name of the Node.js installation configured in Jenkins (see note below)
}
stages {
stage('Checkout Code') { // Stage 1: Pull code from Git
steps {
git url: 'https://github.com/your-username/sample-node-app.git', branch: 'main' // Replace with your repo URL
}
}
stage('Install Dependencies') { // Stage 2: Install project dependencies
steps {
sh 'npm install' // Runs `npm install` in the project directory
}
}
stage('Run Tests') { // Stage 3: Run unit tests
steps {
sh 'npm test' // Runs tests defined in package.json
}
}
stage('Build Project') { // Stage 4: Build the project (e.g., bundle JS)
steps {
sh 'npm run build' // Runs the build script in package.json
}
}
stage('Deploy to Local Folder') { // Stage 5: Deploy (simple example)
steps {
sh 'mkdir -p /var/www/my-app && cp -r dist/* /var/www/my-app/' // Copy build output to a local directory
}
}
}
post {
success {
echo '🎉 Pipeline succeeded! Your app is deployed.'
// Add Slack/email notifications here (e.g., slackSend channel: '#dev-team', message: 'Build passed!')
}
failure {
echo '❌ Pipeline failed. Check logs for details.'
}
}
}
Key Explanations:
agent any: Runs the pipeline on the Jenkins server (the “master” node). For production, use dedicated agents, but this works for testing.tools { nodejs 'NodeJS_18' }: Links to a Node.js installation in Jenkins. To configure this:- Go to Manage Jenkins > Global Tool Configuration.
- Under “NodeJS,” click “Add NodeJS,” name it
NodeJS_18, select version 18, and save.
- Stages: Each stage represents a logical step in your workflow. Jenkins will display these stages visually in the UI.
sh 'command': Executes a shell command (usebatfor Windows agents).
Step 3: Configure the Pipeline in Jenkins
Now, tell Jenkins to use your Jenkinsfile from your Git repo.
-
Create a New Pipeline Job:
- Go to Jenkins homepage > New Item.
- Name it (e.g., “My First Pipeline”) and select Pipeline. Click OK.
-
Configure Pipeline Source:
- Under “Pipeline,” select Pipeline script from SCM (Source Control Management).
- Choose Git as the SCM.
- Enter your repo URL (e.g.,
https://github.com/your-username/sample-node-app.git). - Specify the branch to build (e.g.,
*/main). - Under “Script Path,” enter
Jenkinsfile(the path to your pipeline script in the repo).
-
Save the Configuration: Click Save.
Step 4: Run and Monitor the Pipeline
-
Trigger a Build:
- On your pipeline job page, click Build Now to manually trigger the pipeline.
-
View the Pipeline Progress:
- Jenkins will display a Pipeline Stage View (a visual timeline of stages).
- Click on a stage (e.g., “Run Tests”) to see logs for that step.
- For detailed logs, click Console Output in the left menu.
-
Verify the Deployment:
- If all stages succeed, check the deployment folder (e.g.,
/var/www/my-appon Linux) to confirm your build output was copied.
- If all stages succeed, check the deployment folder (e.g.,
Troubleshooting Common Issues
Even with careful setup, you might run into problems. Here are fixes for common issues:
Issue 1: “npm: command not found”
Why: Jenkins can’t find Node.js on the agent.
Fix: Ensure the NodeJS_18 tool is configured in Global Tool Configuration (as in Step 2). Verify the tool name matches what’s in your Jenkinsfile.
Issue 2: “Permission denied” when deploying to /var/www
Why: The Jenkins user (e.g., jenkins on Linux) lacks write access to the target directory.
Fix:
- Run
sudo chown -R jenkins:jenkins /var/www/my-appto grant Jenkins ownership. - Or, use a directory Jenkins can access (e.g.,
~/my-app-deploy).
Issue 3: “Failed to connect to Git repo”
Why: Jenkins can’t authenticate with your Git repo.
Fix:
- Use HTTPS with a personal access token (PAT):
- In your repo URL, use
https://your-username:[email protected]/your-username/sample-node-app.git. - Store the PAT securely using Jenkins Credentials (see Advanced Tips).
- In your repo URL, use
Advanced Tips for Beginners
Once you have a basic pipeline working, try these enhancements:
1. Use Docker Agents for Consistency
Avoid “it works on my machine” issues by running your pipeline in a Docker container. Update your agent block:
agent {
docker {
image 'node:18' // Use an official Node.js 18 image
reuseNode true
}
}
2. Manage Secrets with Jenkins Credentials
Never hardcode passwords or tokens in Jenkinsfile. Store them in Jenkins:
- Go to Manage Jenkins > Credentials > System > Global credentials > Add Credentials.
- Choose a credential type (e.g., “Username with password” for Git PAT).
- Reference it in your
Jenkinsfile:stage('Checkout Code') { steps { git url: 'https://github.com/your-username/sample-node-app.git', credentialsId: 'github-pat' // ID of your stored credential } }
3. Add Triggers for Automation
Instead of manually clicking “Build Now,” trigger pipelines automatically:
- Webhook: Trigger on Git pushes (e.g., GitHub webhooks).
- Scheduled: Run nightly with
cron('0 0 * * *')(add to thepipelineblock).
4. Parallelize Stages
Speed up pipelines by running stages in parallel (e.g., test and lint at the same time):
stage('Quality Checks') {
parallel {
stage('Lint') {
steps { sh 'npm run lint' }
}
stage('Test') {
steps { sh 'npm test' }
}
}
}
Conclusion
You’ve now built your first Jenkins pipeline! You learned how to define a workflow as code with Jenkinsfile, configure Jenkins to use it, and troubleshoot common issues.
Pipelines are powerful—start small, then experiment with advanced features like parallel stages, Docker integration, and notifications. The Jenkins ecosystem is vast, so explore plugins and tools to tailor your pipeline to your project’s needs.
References
- Jenkins Pipeline Documentation
- Sample Node.js Project with Jenkinsfile (replace with your repo)
- Jenkins Plugins Directory
- GitHub PAT Setup Guide
Happy automating! 🚀