Chapter 09 · Jenkins Node.js CI/CD lab
Complete Jenkins CI/CD lab
A reproducible student lab: install Jenkins on Ubuntu, connect the public Nerd503/nodejs-cicd-demo repository, run CI manually, deploy with systemd, verify /health, and test rollback.
Checkpoint A
Jenkins CI
Checkout, npm ci, tests, artifact.
Checkpoint B
EC2 deploy
systemd service and health check.
Checkpoint C
Reliability
Release retention and rollback.
Create the EC2 instance
Use Ubuntu Server 24.04 LTS, 64-bit x86, t3.small or larger, and 20 GB gp3. Add SSH port 22 from your IP and custom TCP 8080 from your IP. Keep application port 5000 private.
ssh -i your-key.pem ubuntu@YOUR_EC2_PUBLIC_IPInstall Java 21 and Jenkins
Use the official Jenkins LTS repository, then start Jenkins and retrieve the initial password.
sudo apt update
sudo apt upgrade -y
sudo apt install -y fontconfig openjdk-21-jre
java -version
sudo mkdir -p /etc/apt/keyrings
sudo wget -O /etc/apt/keyrings/jenkins-keyring.asc \
https://pkg.jenkins.io/debian-stable/jenkins.io-2026.key
echo "deb [signed-by=/etc/apt/keyrings/jenkins-keyring.asc] https://pkg.jenkins.io/debian-stable binary/" \
| sudo tee /etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt update
sudo apt install -y jenkins
sudo systemctl enable jenkins
sudo systemctl start jenkins
sudo systemctl status jenkins --no-pager
sudo cat /var/lib/jenkins/secrets/initialAdminPasswordhttp://YOUR_EC2_PUBLIC_IP:8080, enter the password, install suggested plugins, create an administrator account, and confirm the Jenkins URL.Install Node.js 22, npm, Git and curl
Jenkins needs the build tools available to its service account.
curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
sudo apt install -y nodejs git curl
node --version
npm --version
git --version
curl --version | head -n 1
sudo -u jenkins bash -c 'node --version && npm --version && git --version'Create NODE-PIPELINE from public GitHub
In Jenkins choose New Item → NODE-PIPELINE → Pipeline. Use Pipeline script from SCM, Git, repository https://github.com/Nerd503/nodejs-cicd-demo.git, credentials None, branch */main, and script path Jenkinsfile. Leave triggers unchecked, save, click Build Now, and inspect Console Output.
Prepare the Node.js deployment service
Create a non-root application user, versioned release directories, and a systemd service. Do not start it before the first release exists.
sudo adduser --system --group --no-create-home nodeapp
sudo install -d -o nodeapp -g nodeapp -m 755 /opt/nodejs-cicd
sudo install -d -o nodeapp -g nodeapp -m 755 /opt/nodejs-cicd/releases
sudo systemctl daemon-reload
sudo systemctl enable nodejs-cicd
sudo systemctl cat nodejs-cicd
sudo systemctl is-enabled nodejs-cicd[Unit]
Description=Node.js CI/CD Demo Application
After=network.target
[Service]
Type=simple
User=nodeapp
Group=nodeapp
WorkingDirectory=/opt/nodejs-cicd/current
Environment=NODE_ENV=production
Environment=PORT=5000
ExecStart=/usr/bin/node /opt/nodejs-cicd/current/app.js
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.targetInstall and validate the deployment script
Create /usr/local/sbin/deploy-nodejs-cicd using the tested archive, release, health-check, and rollback script from the lab repository. Then secure and syntax-check it.
sudo chown root:root /usr/local/sbin/deploy-nodejs-cicd
sudo chmod 750 /usr/local/sbin/deploy-nodejs-cicd
sudo bash -n /usr/local/sbin/deploy-nodejs-cicdAllow Jenkins to invoke only this script
Create a dedicated sudoers rule; do not grant Jenkins unrestricted passwordless sudo.
jenkins ALL=(root) NOPASSWD: /usr/local/sbin/deploy-nodejs-cicd *
sudo visudo -cf /etc/sudoers.d/jenkins-nodejs
sudo -u jenkins sudo -n -lRun and verify the complete pipeline
The shared Jenkinsfile must checkout, check tools, run npm ci, test, create an archive excluding .git, node_modules, and environment files, archive it, deploy it, and check http://127.0.0.1:5000/health.
sudo systemctl status nodejs-cicd --no-pager
curl --fail http://127.0.0.1:5000/
curl --fail http://127.0.0.1:5000/health
sudo journalctl -u nodejs-cicd -n 100 --no-pager
readlink -f /opt/nodejs-cicd/current