Chapter 10 · Jenkins lab
Build, test, deploy, recover
A reproducible student lab for deploying a Node.js app from a public GitHub repository to Ubuntu with Jenkins, systemd, health checks, and 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.
Prepare the EC2 lab host
Use Ubuntu 24.04 on a t3.small or larger instance. Allow SSH on port 22 from your IP and Jenkins on port 8080 from your IP. Keep the app on port 5000 private for local health checks.
sudo apt update && sudo apt upgrade -y
sudo apt install -y fontconfig openjdk-21-jre git curl
curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
sudo apt install -y nodejs
node --version && npm --version && git --versionInstall and open Jenkins
Add the official Jenkins LTS repository, start the service, then open http://YOUR_EC2_PUBLIC_IP:8080. Install the suggested plugins and create an administrator account.
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
sudo apt update && sudo apt install -y jenkins
sudo systemctl enable --now jenkins
sudo cat /var/lib/jenkins/secrets/initialAdminPasswordCreate the Pipeline job
Create NODE-PIPELINE as a Pipeline job. Choose Pipeline script from SCM, SCM Git, repository https://github.com/Nerd503/nodejs-cicd-demo.git, branch */main, and script path Jenkinsfile. Run it with Build Now.
Prepare an isolated app user
Run the Node.js service as nodeapp, not root. Version each release and point the current symlink at the active release.
sudo adduser --system --group --no-create-home nodeapp
sudo install -d -o nodeapp -g nodeapp -m 755 /opt/nodejs-cicd/releases
sudo tee /etc/systemd/system/nodejs-cicd.service > /dev/null <<'EOF'
[Unit]
Description=Node.js CI/CD Demo Application
After=network.target
[Service]
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
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable nodejs-cicdAdd the deployment and health stages
The shared Jenkinsfile must run tests, create an archive with app.js, package.json, and package-lock.json at its root, call a validated deployment script, then check http://127.0.0.1:5000/health.
sudo systemctl status nodejs-cicd --no-pager
curl --fail http://127.0.0.1:5000/health
sudo journalctl -u nodejs-cicd -n 100 --no-pagerValidate rollback
Record readlink -f /opt/nodejs-cicd/current, deploy a new build, simulate a controlled health-check failure, and verify the previous release becomes active again. Test on a disposable instance.
.env files, private keys, or tokens.