Centos7 install jenkins, jenkins release springboot project, jenkins use tutorial, jenkins installation and use
One, jenkins installation
1.jenkins官网
Visit the official website: https://www.jenkins.io/
2.jenkins installation
Click the Download button on the official website .
Excuting an order:
#下载yum源
sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
#yum安装jenkins
yum -y install jenkins
#安装jdk1.8,jenkins需要java环境来运行
yum install -y java-1.8.0-openjdk-devel.x86_64
Complete!
#启动jenkins
[root@iz2ze16q2yp9vb7m7mnbsnz usr]# systemctl start jenkins
The default port of jenkins is 8080, visit http://xxxx:8080 to open the jenkins workbench.
#查看初始化的密码
[root@iz2ze3dj8i3kf6iexu16sgz ~]# cat /var/lib/jenkins/secrets/initialAdminPassword
c767fe2189074d199e49b5c9f4e34075
[root@iz2ze3dj8i3kf6iexu16sgz ~]#
3. Recommended plugin installation
Enter the password to enter jenkins, and click "install recommended plug-ins" in the next step
4. Create a jenkins username and password
Save and finish -> Next -> Start using Jenkins.
5. Install custom plugins
点击“Manage Jenkins”,“Manage Plugins”。
Click "Optional Plugins" and select "Maven Integration".
Enter "SSH"
again in the input box and select "Publish Over SSH" again, and then "Download now and install after restart"; check the automatic restart after installation under jenkins, and manually refresh the browser after a while.
Two, rely on tool installation
1.git installation (for code version management)
yum -y install git
2.maven installation (for Maven project build)
# maven下载
wget https://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.tar.gz
# 解压
tar -xf apache-maven-3.6.3-bin.tar.gz -C /usr/local/
Add environment variables and add a new line at the bottom of the /etc/profile file
export PATH=$PATH:/usr/local/apache-maven-3.6.3/bin
After adding, execute:
source /etc/profile
Execute the command "mvn", if it can be executed, it means there is no problem.
Three, jenkins global tool configuration
1. Maven configuration
huawei_mirror.xml is the mirror of Huawei maven, which is private server.
2.JDK configuration
JDK installation is through yum install java, install openjdk, then where is JAVA_HOME?
which java
ls -lrt /usr/bin/java
ls -lrt /etc/alternatives/java
Get JAVA_HOME as: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.282.b08-1.el7_9.x86_64
3. Git configuration
#得到git位置
[root@iz2ze3dj8i3kf6iexu16sgz ~]# which git
/usr/bin/git
4. Maven configuration
[root@iz2ze3dj8i3kf6iexu16sgz ~]# which mvn
/usr/local/apache-maven-3.6.3/bin/mvn
[root@iz2ze3dj8i3kf6iexu16sgz ~]#
Through which mv, MAVEN_HOME is /usr/local/apache-maven-3.6.3.
Four, jenkins builds a maven project
1. Create a task
New task-"input task name-"build a maven project-"OK.
2. Configuration build history
Description: Project description;
check "Discard old construction": Keep the old construction history, and automatically delete 10 histories before.
3. Configuration parameter construction
Click "Add parameter"-"Option parameter"
name: Status
option:
Deploy
Rollback
description:
ps:只可以恢复10内的备份文件,超过10自动清理
Deploy--发布
--------------------
Rollback--回滚
Add the parameter "character parameter" again
so that the historical version of the build can be restored later.
4. Configure git source code
Enter the git address, add the git username and password, and specify the branch.
5. Build by default, or customize others
Maven build parameter configuration.
Goals and options: clean install -Dmaven.test.skip=true
6.post steps
Select "Run only if build succeeds" to
execute the shell content:
case $Status in
Deploy)
echo "Status:$Status"
#创建备份地址
path="${WORKSPACE}/box_bak/${BUILD_NUMBER}"
if [ -d $path ];
then
echo "文件夹已经创建 "
else
mkdir -p $path
fi
\cp -f /usr/local/jar/boxjar/demo1-0.0.1-SNAPSHOT-exec.jar $path
echo "jar备份成功了"
mv /var/lib/jenkins/workspace/demo1/target/demo1-0.0.1-SNAPSHOT-exec.jar /usr/local/jar/boxjar/
pid=`ps -ef | grep demo1-0.0.1-SNAPSHOT-exec.jar | grep -v grep | awk '{print $2}' `
BUILD_ID=DONTKILLME
if [ -n "$pid" ]; then
kill -9 $pid;
nohup java -jar /usr/local/jar/boxjar/demo1-0.0.1-SNAPSHOT-exec.jar > /usr/local/jar/boxjar/app.log 2>&1 &
else
nohup java -jar /usr/local/jar/boxjar/demo1-0.0.1-SNAPSHOT-exec.jar > /usr/local/jar/boxjar/app.log 2>&1 &
fi
;;
Rollback)
echo "Status:$Status"
echo "Version:$Version"
#进入备份目录
cd ${WORKSPACE}/box_bak/$Version
#并覆盖之前的jar包
\cp -f demo1-0.0.1-SNAPSHOT-exec.jar /usr/local/jar/boxjar/
#获取进程端口
pid=`ps -ef |grep demo1-0.0.1-SNAPSHOT-exec.jar | grep -v grep | awk '{print $2}' `
#组织莫名其妙的自杀行为
BUILD_ID=DONTKILLME
#判断是否启动
if [ -n "$pid" ]; then
kill -9 $pid;
nohup java -jar /usr/local/jar/boxjar/demo1-0.0.1-SNAPSHOT-exec.jar > /usr/local/jar/boxjar/app.log 2>&1 &
else
nohup java -jar /usr/local/jar/boxjar/demo1-0.0.1-SNAPSHOT-exec.jar > /usr/local/jar/boxjar/app.log 2>&1 &
fi
;;
*)
exit
;;
esac
Save.
7.Build with Parameters
Click the 3 button to view the log.
The following Rollback history construction project.
Enter the build number of Buildhistory in Version to restore historical build projects.
5. Jenkins builds remote SSH and starts the script (another jenkins task configuration method)
1. Configure Publish over SSH
key is the private key of the server's login key pair.
SSH Server:
name: Alias
Hostname: Cloud server ip
Username: User name of the cloud server
Remote Directory:/ It is best to enter /, or you can customize other paths. The upload path defined in the jenkins task will be connected to the current directory.
2.post Step configuration
Select "Send files or execute commands over SSH"
SSH Server: Select the server configured above
Source files: in the workspace of the jenkins task, for example, this project is in the /var/lib/jenkins/workspace/demo1 path.
Remove prefix: no more The path that contains the source file.
Remote directory: represents the path of the uploaded remote directory.
Exec command: the sh command executed after the code is uploaded.
The trace log successfully uploaded the file and executed the command.