Operations 16 min read

Master Jenkins CI/CD: Step-by-Step Automated Deployment Guide

This comprehensive tutorial walks you through Jenkins fundamentals, installation, JDK and Git integration, credential management, Maven setup, SSH password‑less login, and deployment scripts, providing all the commands and screenshots needed to automate Java project builds and releases.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Jenkins CI/CD: Step-by-Step Automated Deployment Guide

Jenkins Tutorial (Automated Deployment)

1. What is Jenkins?

Jenkins is an open‑source CI (Continuous Integration) tool with a friendly UI, widely used for automated building, testing, and deployment. It is written in Java, can run in servlet containers like Tomcat or standalone, and typically integrates with SCM and build tools.

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

CI/CD automates code testing, packaging, and deployment, reducing manual effort and errors while allowing developers to focus on coding and committing to Git.

3. Jenkins Installation

(1) Prepare Conditions

Install JDK

Download the JDK tarball and extract it 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 confirms JDK installation.

(2) Install Jenkins

1. Download Jenkins

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

2. Start Jenkins

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

3. View startup logs to obtain the admin password

4. Access Jenkins via browser at

http://<em>your_ip</em>:8777

5. Install recommended plugins

(3) Configure JDK Path in Jenkins

Navigate to Global Tool Configuration → JDK → Add JDK and set the path.

(4) Jenkins Password Reset

4. Integrate Git

Jenkins needs Git installed on the host and the Git plugin.

(1) Install Git on CentOS 7

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

(2) Install Git plugin in Jenkins

(3) Configure Git in Jenkins (default settings are sufficient)

5. Credential Configuration

Credentials store passwords for Gitee, Docker registries, etc., enabling Jenkins to interact with external services.

1. Install Credentials Binding plugin (pre‑installed in recent Jenkins versions).

After installation, the Manage Credentials option appears under System → Security.

6. Maven Integration

To build Java projects, install Maven and configure it in Jenkins.

(1) Download and Install Maven

# Create a directory for Maven
cd /usr/wubin/
# Download 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 installed at /usr/wubin/apache-maven-3.6.3.

(2) Environment Configuration

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

(3) Apply Configuration and Verify

source /etc/profile
mvn -version

(4) Configure Maven in Jenkins

(5) Install Maven Plugin

(6) Create Local Repository Directory

$ cd /usr/wubin
$ mkdir repository

(7) Edit settings.xml (root user) to point to 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>

(8) Maven Project Build Test

Create a Maven project, push it to Gitee, and configure a Jenkins freestyle job to pull and build it.

After building, the generated JAR appears in Jenkins workspace and the local Maven repository.

7. Configure SSH Password‑less Login

Generate an RSA key pair on the Jenkins server (root user) and copy the public key to the application server.

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

Append the public key to ~/.ssh/authorized_keys on the target server and set proper permissions:

$ chmod 700 ~/.ssh
$ chmod 600 ~/.ssh/authorized_keys

Test the password‑less connection with ssh [email protected] and file copy via scp.

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 to backup existing jar"
  ssh -Tq -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 new jar to /tmp on target"
  scp -q -o StrictHostKeyChecking=no ${WORKSPACE}/target/*.jar root@${server_ip}:/tmp/${projectName}.jar
  echo "Deploy jar on target"
  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 &
if [[ $? -eq 0 ]]; then
  sleep 3
  tail -n 10 release_out.log
  sleep 5
  tail -n 50 release_out.log
fi
started=$(grep "Started" release_out.log | awk '{print $1}')
if [[ -n "${started}" ]]; 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 firewall to allow port 9010 on the application server:

$ firewall-cmd --zone=public --add-port=9010/tcp --permanent
$ firewall-cmd --reload

After committing changes to Gitee, trigger a Jenkins build, and verify the application is reachable at http://192.168.223.129:9010.

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/cdmavenJenkinsshell script
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.