Operations 20 min read

Build a Multi‑Branch Jenkins Pipeline for Node.js & React Using Docker

This guide walks you through running Jenkins in Docker, unlocking and configuring it, creating development and production Git branches, writing a Jenkinsfile with build, test, deliver and deploy stages, and executing the pipeline for both environments using Blue Ocean.

Full-Stack DevOps & Kubernetes
Full-Stack DevOps & Kubernetes
Full-Stack DevOps & Kubernetes
Build a Multi‑Branch Jenkins Pipeline for Node.js & React Using Docker

Running Jenkins in Docker

Start a Jenkins Blue Ocean container:

docker run \
  --rm \
  -u root \
  -p 8080:8080 \
  -v jenkins-data:/var/jenkins_home \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -v "$HOME":/home \
  jenkinsci/blueocean

Give the container a name (e.g., --name jenkins-tutorials) and access its shell with:

docker exec -it jenkins-tutorials bash

Unlock Jenkins

Open http://localhost:8080. Copy the automatically generated admin password from the terminal, paste it into the *Administrator password* field, and click *Continue*.

Customize Jenkins

On the *Customize Jenkins* page, click *Install suggested plugins*, wait for installation, create the first admin user, and finish the setup.

Stop and Restart Jenkins

Stop the container with Ctrl-C. To restart, run the same docker run … command again; Docker will pull a newer jenkinsci/blueocean image if one is available.

Fork and Clone Example Repository

Fork the building-a-multibranch-pipeline-project repository on GitHub and clone it to a local directory.

Create Development and Production Branches

git branch development
git branch production
# Ensure you are on master before branching
git checkout master
git branch   # verify branches

Create Pipeline in Blue Ocean

Open Blue Ocean at http://localhost:8080/blue, click *Create a new Pipeline*, choose *Git* (not *GitHub*), and set *Repository URL* to the local path of the cloned repository (e.g.,

/home/youruser/Documents/GitHub/building-a-multibranch-pipeline-project

). Save the pipeline.

Add Build and Test Stages

pipeline {
    agent {
        docker {
            image 'node:6-alpine'
            args '-p 3000:3000 -p 5000:5000'
        }
    }
    environment { CI = 'true' }
    stages {
        stage('Build') { steps { sh 'npm install' } }
        stage('Test')  { steps { sh './jenkins/scripts/test.sh' } }
    }
}

Commit and push the Jenkinsfile:

git add Jenkinsfile
git commit -m "Add initial Jenkinsfile with Build and Test stages"
git push

Add Deliver and Deploy Stages

stage('Deliver for development') {
    when { branch 'development' }
    steps {
        sh './jenkins/scripts/deliver-for-development.sh'
        input message: 'Finished using the web site? (Click "Proceed" to continue)'
        sh './jenkins/scripts/kill.sh'
    }
}
stage('Deploy for production') {
    when { branch 'production' }
    steps {
        sh './jenkins/scripts/deploy-for-production.sh'
        input message: 'Finished using the web site? (Click "Proceed" to continue)'
        sh './jenkins/scripts/kill.sh'
    }
}

Commit and push the updated Jenkinsfile:

git add Jenkinsfile
git commit -m "Add Deliver and Deploy stages"
git push

Synchronize Jenkinsfile to Other Branches

# Update development branch
git checkout development
git pull . master

# Update production branch
git checkout production
git pull . master

Run Pipeline on Development Branch

In Blue Ocean select the development branch and click the run icon. After the build finishes, open the *Deliver for development* stage; the application is reachable at http://localhost:3000. Click *Proceed* to complete the pipeline.

Run Pipeline on Production Branch

Select the production branch, run the pipeline, and after the build completes open the *Deploy for production* stage. The production build is served at http://localhost:5000. Click *Proceed* to finish.

Optional: Clear Service Workers

Chrome: Open chrome://serviceworker-internals/, locate http://localhost:5000, and click Unregister .

Firefox: Open about:serviceworkers, locate http://localhost:5000, and click Unregister .

Reference

Jenkins documentation: https://www.jenkins.io/

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Dockerci/cdReactNode.jsJenkinsBlue OceanMultibranch Pipeline
Full-Stack DevOps & Kubernetes
Written by

Full-Stack DevOps & Kubernetes

Focused on sharing DevOps, Kubernetes, Linux, Docker, Istio, microservices, Spring Cloud, Python, Go, databases, Nginx, Tomcat, cloud computing, and related technologies.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.