Operations 17 min read

Master Jenkins: A Step‑by‑Step Guide to Automated Deployment and CI/CD

This comprehensive tutorial explains what Jenkins is, why continuous integration matters, and provides detailed, platform‑agnostic instructions for installing JDK, setting up Jenkins, integrating Git and Maven, configuring credentials, enabling SSH keyless login, and creating deployment scripts to automate Java application builds and releases.

Raymond Ops
Raymond Ops
Raymond Ops
Master Jenkins: A Step‑by‑Step Guide to Automated Deployment and CI/CD

Jenkins Tutorial (Automated Deployment)

1. What is Jenkins?

Jenkins is an open‑source continuous integration (CI) tool written in Java that runs on servlet containers such as Tomcat or as a standalone service. It automates building, testing, and deploying projects and is typically used together with source‑code management and build tools.

2. What is Continuous Integration (CI/CD)?

Because development teams maintain multiple versions and releases, a professional CI tool is needed to automate repetitive tasks such as code testing, packaging, and deployment, allowing developers to focus on writing code and pushing it to Git.

3. Installing Jenkins

(1) Prerequisites

Install JDK

Download the JDK tarball and extract it to a directory on Linux.

① Configure JDK environment variables

# Enter /etc/profile configuration file
vim /etc/profile

② Add the following lines to the profile file

export JAVA_HOME=/usr/wubin/jdk11
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$JAVA_HOME/bin:$PATH

③ Make the configuration effective source /etc/profile ④ Verify Java installation javac Successful output indicates the JDK is installed.

JDK installation result
JDK installation result

(2) Install Jenkins

1. Download Jenkins war

wget http://mirrors.jenkins.io/war-stable/latest/jenkins.war

2. Start Jenkins with nohup (logs are written to jenkins.log)

nohup java -jar /usr/wubin/jenkins.war --httpPort=8777 --httpsPort=8778 > /usr/wubin/jenkins.log 2>&1 &

3. View the startup log to obtain the admin password

4. Access Jenkins at http://<your_ip>:8777 and install the recommended plugins.

Jenkins initial UI
Jenkins initial UI
Plugin installation screen
Plugin installation screen

(3) Configure JDK Path in Jenkins

Navigate to Manage Jenkins → Global Tool Configuration → JDK → Add JDK and set the JDK home directory.

JDK configuration in Jenkins
JDK configuration in Jenkins

(4) Recover Forgotten Jenkins Password

Password recovery steps
Password recovery steps

4. Integrate Git

To allow Jenkins to pull code, install Git on the build server and the Jenkins Git plugin.

(1) Install Git on CentOS 7

# Install
yum install git -y
# Verify version
git --version

(2) Install Jenkins Git plugin

Jenkins Git plugin installation
Jenkins Git plugin installation

(3) Configure Git in Jenkins

The default Git configuration is sufficient; no additional settings are required.

(4) Create a repository on Gitee (or any Git service)

Create repository on Gitee
Create repository on Gitee

(5) Test credentials

Create a freestyle job, configure the repository URL, and run a build to verify that Jenkins can clone the code.

Jenkins job configuration
Jenkins job configuration

5. Credential Management

Credentials store passwords, tokens, and other secrets needed by Jenkins to interact with external services.

1. Install the Credentials Binding plugin (usually pre‑installed).

Credentials plugin UI
Credentials plugin UI

After installation, go to Manage Jenkins → Configure System → Manage Credentials to add username/password, secret text, or SSH keys.

Add new credentials
Add new credentials

6. Maven Integration

To build Java projects, Jenkins needs Maven (or Gradle for other projects).

(1) Download and install Maven

# Choose a directory for Maven
cd /usr/wubin/
# Download Maven 3.6.3 from Alibaba Cloud mirror
wget https://mirrors.aliyun.com/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
# Extract
tar -zxvf apache-maven-3.6.3-bin.tar.gz

Maven is now located at /usr/wubin/apache-maven-3.6.3.

(2) Configure environment variables

vi /etc/profile
export MAVEN_HOME=/usr/wubin/apache-maven-3.6.3
export PATH=$JAVA_HOME/bin:$MAVEN_HOME/bin:$PATH

Apply the changes:

source /etc/profile
mvn -version

(3) Configure Maven in Jenkins

Jenkins Maven configuration
Jenkins Maven configuration

(4) Install Maven plugin

Maven plugin installation
Maven plugin installation

(5) Create a local Maven repository

# Create repository directory
cd /usr/wubin
mkdir repository

(6) Configure settings.xml to use the local repository and Alibaba Cloud mirror

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
  <localRepository>/data/software/repository</localRepository>
  <mirrors>
    <mirror>
      <id>aliyun-maven</id>
      <mirrorOf>central</mirrorOf>
      <name>aliyun maven mirror</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
    </mirror>
  </mirrors>
  ...
</settings>

7. Configure SSH Keyless Login

Jenkins (IP 192.168.223.128) needs password‑less SSH access to the application server (IP 192.168.223.129) to copy JAR files.

Generate SSH key on Jenkins server

$ ssh-keygen -t rsa   # press Enter three times

The keys are stored in /root/.ssh/ as id_rsa (private) and id_rsa.pub (public).

Copy the public key to the application server

# Append public key to authorized_keys on Jenkins server
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
# Ensure correct permissions
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
# Transfer authorized_keys to the application server
scp -p ~/.ssh/id_rsa.pub [email protected]:/root/.ssh/authorized_keys

Test the connection:

$ ssh [email protected]
Last login: Sun Sep 20 21:53:03 2020

8. Write Jenkins Deployment Script

#!/bin/bash

echo "Deployment directory and project name"
DIR="/data/app"
projectName="my-boot"

echo "Target application servers (can be multiple)"
server_ips=("192.168.223.139")

for server_ip in "${server_ips[@]}"; do
  echo "SSH backup on $server_ip"
  ssh -o StrictHostKeyChecking=no root@${server_ip} <<EOF
    mkdir -p $DIR/backup/${projectName}
    mkdir -p $DIR/${projectName}
    if [ -f "$DIR/${projectName}/${projectName}.jar" ]; then
      mv $DIR/${projectName}/${projectName}.jar $DIR/backup/${projectName}/${projectName}-$(date "+%Y%m%d_%H%M%S").jar
    fi
EOF

  echo "Copy JAR to remote /tmp"
  scp -q -o StrictHostKeyChecking=no ${WORKSPACE}/target/*.jar root@${server_ip}:/tmp/${projectName}.jar

  echo "Remote deployment"
  ssh -q -o StrictHostKeyChecking=no root@${server_ip} <<EOF
    mv /tmp/${projectName}.jar $DIR/${projectName}/${projectName}.jar
EOF

done

echo "success"

9. Write Application Start/Stop Scripts

Start script ( start.sh) placed in /data/app/my-boot:

#!/bin/bash
set -e
set -o pipefail

APP_ID=my-boot
APP_DIR="/data/app"

nohup java -jar ${APP_DIR}/${APP_ID}/${APP_ID}.jar > release_out.log 2>&1 &

# Verify start
if [[ $? -eq 0 ]]; then
  sleep 3
  tail -n 10 release_out.log
  sleep 5
  tail -n 50 release_out.log
fi

if grep -q "Started" release_out.log; then
  echo "Application started ok"
  exit 0
else
  echo "Application started error"
  exit 1
fi

Stop script ( stop.sh) placed in the same directory:

#!/bin/bash
APP_ID=my-boot
ps aux | grep ${APP_ID} | grep -v grep | awk '{print "kill -9 "$2}' | sh

Configure the Jenkins job to execute sh $DIR/${projectName}/stop.sh followed by sh $DIR/${projectName}/start.sh after a successful build.

10. Open Firewall Port

If the application runs on a virtual machine, open port 9010:

# Become root
su root
# Add firewall rule
firewall-cmd --zone=public --add-port=9010/tcp --permanent
# Reload firewall
firewall-cmd --reload

After deployment, access the service at http://192.168.223.129:9010 to verify the new version is running.

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.

ci/cdGitmavenJenkins
Raymond Ops
Written by

Raymond Ops

Linux ops automation, cloud-native, Kubernetes, SRE, DevOps, Python, Golang and related tech discussions.

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.