Jenkins
Jenkins — CI/CD
Jenkins is an open-source automation server for building, testing, and deploying software.
The leading open source automation server, Jenkins provides hundreds of plugins to support building, deploying and automating any project.
1. Installation
Jenkins Installation Documentation
Docker
docker run -d --name jenkins \
-p 8080:8080 -p 50000:50000 \
-v jenkins_home:/var/jenkins_home \
-v /var/run/docker.sock:/var/run/docker.sock \
jenkins/jenkins:lts2. Initial Setup
- Access Jenkins:
http://<host>:8080 - Unlock:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword- Install suggested plugins
- Create admin user
- Enable security: Role Strategy Plugin, CSRF protection
- Disable anonymous read access
Jenkins — Quick Start
Jenkins is an open-source automation server for building, testing, and deploying software.
This quick start shows you how to get Jenkins running and create your first pipeline.
Jenkins Pipeline Official Documentation
1. First Pipeline (Hello World)
Once Jenkins is installed and set up, you can create a simple pipeline:
- In Jenkins, click New Item → Pipeline → Name it (e.g.,
hello-pipeline) → OK. - Scroll to Pipeline section and paste this:
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building...'
}
}
stage('Test') {
steps {
echo 'Testing...'
}
}
stage('Deploy') {
steps {
echo 'Deploying...'
}
}
}
}- Save and click Build Now — watch Jenkins execute the stages.
10 Jenkins Declarative Pipeline Examples (Windows-Friendly)
1. Hello World (Basic)
pipeline {
agent any
stages {
stage('Hello') {
steps {
echo 'Hello World'
}
}
}
}2. Multiple Stages
pipeline {
agent any
stages {
stage('Build') {
steps { echo 'Building...' }
}
stage('Test') {
steps { echo 'Testing...' }
}
stage('Deploy') {
steps { echo 'Deploying...' }
}
}
}3. Run Windows Commands
pipeline {
agent any
stages {
stage('List Files') {
steps {
bat 'dir'
}
}
}
}4. Using Environment Variables
pipeline {
agent any
environment {
APP_NAME = 'my-app'
}
stages {
stage('Show Env') {
steps {
echo "App Name: ${env.APP_NAME}"
}
}
}
}5. Checkout from Git
pipeline {
agent any
stages {
stage('Checkout') {
steps {
git branch: 'main', url: 'https://github.com/example/repo.git'
}
}
}
}6. Run Tests & Publish Report
pipeline {
agent any
stages {
stage('Test') {
steps {
bat 'pytest --junitxml=reports\\results.xml'
junit 'reports/results.xml'
}
}
}
}7. Use Credentials for Private Repo
pipeline {
agent any
environment {
GIT_CREDS = credentials('git-creds')
}
stages {
stage('Checkout') {
steps {
git url: "https://${GIT_CREDS_USR}:${GIT_CREDS_PSW}@github.com/org/private-repo.git", branch: 'main'
}
}
}
}8. Build & Push Docker Image (Windows with Docker Desktop)
pipeline {
agent any
environment {
IMAGE_NAME = "myorg/app"
}
stages {
stage('Build & Push') {
steps {
script {
docker.withRegistry('https://registry.hub.docker.com', 'docker-hub-creds') {
def img = docker.build("${IMAGE_NAME}:${env.BUILD_NUMBER}")
img.push()
}
}
}
}
}
}9. Conditional Stage Execution
pipeline {
agent any
stages {
stage('Deploy to Prod') {
when { branch 'main' }
steps {
echo 'Deploying to Production...'
}
}
}
}10. Full CI/CD Example (Windows Commands)
pipeline {
agent any
environment {
IMAGE = "myorg/app:${env.BUILD_NUMBER}"
}
stages {
stage('Checkout') {
steps { checkout scm }
}
stage('Build') {
steps { bat 'make build' }
}
stage('Test') {
steps {
bat 'make test'
junit 'reports/**/*.xml'
}
}
stage('Docker Build & Push') {
when { branch 'main' }
steps {
script {
docker.withRegistry('https://registry.hub.docker.com', 'docker-hub-creds') {
def img = docker.build(env.IMAGE)
img.push()
}
}
}
}
stage('Deploy') {
when { branch 'main' }
steps {
bat 'deploy.bat'
}
}
}
post {
always {
archiveArtifacts artifacts: 'dist/**', fingerprint: true
}
}
}