CLOUDLAUNCH/LAB

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.

1

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.

Connect
ssh -i your-key.pem ubuntu@YOUR_EC2_PUBLIC_IP
2

Install Java 21 and Jenkins

Use the official Jenkins LTS repository, then start Jenkins and retrieve the initial password.

Jenkins installation
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/initialAdminPassword
Open http://YOUR_EC2_PUBLIC_IP:8080, enter the password, install suggested plugins, create an administrator account, and confirm the Jenkins URL.
3

Install Node.js 22, npm, Git and curl

Jenkins needs the build tools available to its service account.

Build tools
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'
4

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.

5

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.

Application service
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
/etc/systemd/system/nodejs-cicd.service
[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.target
6

Install 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.

Deployment script permissions
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-cicd
7

Allow Jenkins to invoke only this script

Create a dedicated sudoers rule; do not grant Jenkins unrestricted passwordless sudo.

/etc/sudoers.d/jenkins-nodejs
jenkins ALL=(root) NOPASSWD: /usr/local/sbin/deploy-nodejs-cicd *
sudo visudo -cf /etc/sudoers.d/jenkins-nodejs
sudo -u jenkins sudo -n -l
8

Run 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.

Verify on EC2
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
Rollback checkpoint: deploy a controlled failing build on a disposable instance, verify the previous release is restored, and confirm the service is healthy again.