Master CI/CD with Jenkins: From Server Setup to Automated React Deployments
This guide walks you through setting up a CI/CD environment on a CentOS server using Docker, Git, Java, and Jenkins, covering installation, configuration, plugin management, and practical examples such as building Docker images, deploying a React app with Nginx, and automating the entire pipeline.
Prerequisite Concepts
Meaning of CI
CI stands for Continuous Integration. It pulls code from a repository, runs predefined scripts to compile the code, produces an artifact, and pushes the artifact to an artifact repository (e.g., GitLab CI, GitHub CI, Jenkins). This step does not deploy the code.
Meaning of CD
CD has two meanings: Continuous Delivery and Continuous Deployment. Continuous Delivery deploys the artifact to a test environment for verification, while Continuous Deployment deploys directly to production.
Initialize Environment
Prepare a new server (or local machine) with the following tools:
docker
git
java (required for Jenkins)
jenkins
Docker Installation
yum install -y yum-utils device-mapper-persistent-data lvm2
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install docker-ce -y
systemctl start docker
systemctl enable dockerConfigure Docker to use the Aliyun mirror:
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<'EOF'
{
"registry-mirrors": ["https://fwvjnv59.mirror.aliyuncs.com"]
}
EOF sudo systemctl daemon-reload
sudo systemctl restart dockerGit Installation
yum install git -yGenerate SSH keys for Git access:
ssh-keygen -t rsa -C "your_email"
cat ~/.ssh/id_rsa.pubJava Installation
yum install -y javaJenkins Installation
# Add Jenkins repo
wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
# Install Jenkins
yum install jenkins -yStart and enable Jenkins service:
systemctl start jenkins
systemctl enable jenkinsFirst Look at Jenkins
Start Jenkins Service
Jenkins is a Java application that provides a platform for continuous integration.
Access Jenkins Service
Open a browser and navigate to http://<server_ip>:8080. Ensure the port is open in the security group.
Initialize Jenkins
During the first start, Jenkins writes an admin password to a file. Retrieve it with:
cat /var/lib/jenkins/secrets/initialAdminPasswordInstall Recommended Plugins
Before installing plugins, change the update site to a faster mirror:
sed -i 's|http://updates.jenkins-ci.org/download|https://mirrors.tuna.tsinghua.edu.cn/jenkins/g' /var/lib/jenkins/updates/default.json && \
sed -i 's|http://www.google.com|https://www.baidu.com/g' /var/lib/jenkins/updates/default.jsonConfigure User
After completing the initial setup, Jenkins is ready for use.
Applying Jenkins
Jenkins can integrate multiple tools (e.g., Git, Node, Nginx) and orchestrate them as a single task.
Integrate various applications such as Git, Node, Nginx.
Manage multiple application steps within one Jenkins job.
Example: Docker Node Task
Create a job that runs the following commands to check Docker version and pull the latest Node image:
docker -v
docker pull node:latestCreating the Job
Adding the Script
Building the Job
Trigger the build and verify the log ends with STATUS:SUCCESS.
If the job fails due to Docker permission, add the Jenkins user to the Docker group:
gpasswd -a jenkins docker
newgrp dockerExample: Compile React Project
The goal is to build a React project, generate a build directory, and serve it with Nginx.
Install Node Plugin
After installation, configure the Node version in Jenkins global settings.
Test Node Environment
Create a job that runs node -v using the configured Node tool.
Create React Project
Generate a React app locally and push it to a Git repository.
npx create-react-app ci-pro
# or for older npm versions
create-react-app ci-pro
# install globally if needed
npm install -g create-react-appProject structure after creation:
.
├── README.md
├── package.json
├── public
│ ├── favicon.ico
│ ├── index.html
│ ├── logo192.png
│ ├── logo512.png
│ ├── manifest.json
│ └── robots.txt
├── src
│ ├── App.css
│ ├── App.js
│ ├── App.test.js
│ ├── index.css
│ ├── index.js
│ ├── logo.svg
│ ├── reportWebVitals.js
│ └── setupTests.js
└── yarn.lockIntegrate Nginx
Create an Nginx configuration that serves files from /etc/nginx/html:
server {
listen 80;
server_name _;
root /etc/nginx/html;
}Build a custom Docker image that copies the React build output into the Nginx image:
FROM nginx:1.15
COPY build /etc/nginx/html
COPY conf /etc/nginxConfigure Git Credentials in Jenkins
Add an SSH private key credential (Credentials → SSH Username with private key) so Jenkins can clone the repository.
Job Build Script
The build steps performed by Jenkins:
#!/bin/sh
npm install --registry=https://registry.npm.taobao.org
npm run build
docker build -t react-project .
# Stop any running containers of the same image
docker kill $(docker ps | awk '/ci-project/ {print $1}')
# Run the new container
docker run -d -p 3000:80 react-projectBuild and Verify
Trigger the job, then access http://<server_ip>:3000. A successful React UI indicates the pipeline works.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
WeDoctor Frontend Technology
Official WeDoctor Group frontend public account, sharing original tech articles, events, job postings, and occasional daily updates from our tech team.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
