一、CentOS 安装
1. 使用该存储库
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-2023.key
2. 安装 Java
yum install fontconfig java-11-openjdk
vim /etc/profile
`
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.13.0.8-4.el8_5.x86_64
export PATH=$JAVA_HOME/bin:$PATH
`
source /etc/profile
3. 安装 Jenkins
- 直接安装
yum install jenkins
- 手动安装
rpm -ivh jenkins-2.190.3-1.1.noarch.rpm
4. 卸载 Jenkins
yum remove jenkins
rm -rf /etc/sysconfig/jenkins.rpmsave
rm -rf /var/cache/jenkins/
rm -rf /etc/init.d/jenkins.rpmsave
5. 启动 Jenkins
systemctl status jenkins.service 或 journalctl -xe
systemctl start jenkins.service
systemctl stop jenkins.service
systemctl restart jenkins.service
systemctl daemon-reload
systemctl enable jenkins
vim /etc/syscofig/jenkins
`
JENKINS_USER="root"
JENKINS_PORT="8888"
`
vim /usr/lib/systemd/system/jenkins.service
`
Environment="JENKINS_PORT=8888"
`
- http://127.0.0.1:8888
- /var/lib/jenkins/secrets/initialAdminPassword
- http://127.0.0.1:8888/restart 重启
[root@qs ~]
Job for jenkins.service failed because the control process exited with error code.
See "systemctl status jenkins.service" and "journalctl -xe" for details.
[root@qs ~]
● jenkins.service - Jenkins Continuous Integration Server
Loaded: loaded (/usr/lib/systemd/system/jenkins.service; disabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Thu 2023-07-20 14:28:03 CST; 3s ago
Process: 418710 ExecStart=/usr/bin/jenkins (code=exited, status=1/FAILURE)
Main PID: 418710 (code=exited, status=1/FAILURE)
7月 20 14:28:03 renyun systemd[1]: jenkins.service: Service RestartSec=100ms expired, scheduling restart.
7月 20 14:28:03 renyun systemd[1]: jenkins.service: Scheduled restart job, restart counter is at 5.
7月 20 14:28:03 renyun systemd[1]: Stopped Jenkins Continuous Integration Server.
7月 20 14:28:03 renyun systemd[1]: jenkins.service: Start request repeated too quickly.
7月 20 14:28:03 renyun systemd[1]: jenkins.service: Failed with result 'exit-code'.
7月 20 14:28:03 renyun systemd[1]: Failed to start Jenkins Continuous Integration Server.
[root@qs ~]
-- Support: https://access.redhat.com/support
--
-- jenkins.service 单元已结束停止操作。
7月 20 14:36:35 renyun systemd[1]: jenkins.service: Start request repeated too quickly.
7月 20 14:36:35 renyun systemd[1]: jenkins.service: Failed with result 'exit-code'.
-- Subject: Unit failed
-- Defined-By: systemd
-- Support: https://access.redhat.com/support
--
-- The unit jenkins.service has entered the 'failed' state with result 'exit-code'.
7月 20 14:36:35 renyun systemd[1]: Failed to start Jenkins Continuous Integration Server.
-- Subject: jenkins.service 单元已失败
-- Defined-By: systemd
-- Support: https://access.redhat.com/support
--
-- jenkins.service 单元已失败。
--
-- 结果为“failed”。
lines 3456-3473/3473 (END)
6. Jenkins 启动报错
1. Jdk 版本
vim /etc/init.d/jenkins
echo $JAVA_HOME
candidates="
/etc/alternatives/java
/usr/lib/jvm/java-1.8.0/bin/java
/usr/lib/jvm/jre-1.8.0/bin/java
/usr/lib/jvm/java-11-openjdk-amd64
/usr/lib/jvm/java-11.0/bin/java
/usr/lib/jvm/jre-11.0/bin/java
/usr/bin/java
/usr/lib/jvm/java-11-openjdk-11.0.13.0.8-4.el8_5.x86_64/bin/java
"
2. 用户名
vim /etc/sysconfig/jenkins
`
JENKINS_USER="root"
`
vim /usr/lib/systemd/system/jenkins.service
`
User=root
Group=root
`
systemctl daemon-reload
systemctl start jenkins.service
systemctl status jenkins.service
3. log 权限
cd /var/log
chmod 777 -R jenkins/
4. 启动方法问题
cp /etc/rc.d/init.d/functions /etc/init.d/
cd /etc/init.d/
./jenkins status
./jenkins stop
./jenkins start
5. 无法使用 systemctl 启动 jenkins
- 原因是因为 systemctl 启动时调用的 jenkins.service 文件在 /usr/lib/systemd/system 目录下,无法调用 /etc/init.d/ 下的 jenkins。
- 所以如果遇到服务方式无法启动 jenkins,则需要修改 jenkins.service 来进行适配。
vim /usr/lib/systemd/system/jenkins.service
`
Environment="JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.13.0.8-4.el8_5.x86_64"
`
systemctl daemon-reload
systemctl start jenkins.service
systemctl status jenkins.service
6. 端口占用
netstat -ntap | grep 8086
cat /etc/sysconfig/jenkins
JENKINS_PORT="8086"
cat /usr/lib/systemd/system/jenkins.service
Environment="JENKINS_PORT=8086"
systemctl daemon-reload
systemctl start jenkins.service
systemctl status jenkins.service
二、Jenkins 插件
1. 替换国内插件地址
cd /var/lib/jenkins/updates
grep -oi "www.google.com" default.json
sed -i 's/http:\/\/updates.jenkinsci.org\/download/https:\/\/mirrors.tuna.tsinghua.edu.cn\/jenkins/g' default.json && sed -i 's/http:\/\/www.google.com/https:\/\/www.baidu.com/g' default.json
- Dashboard > Manage Jenkins > Plugins > Advanced settings
- 把 Update Site 改为国内插件下载地址:
- 原 https://updates.jenkins.io/update-center.json
- 新 https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json
2. Chinese 中文
3. Role-based Authorization Strategy 管理用户权限
1. 选择授权策略
- Dashboard > Manage Jenkins > Security
2. 创建角色
- Dashboard > Manage Jenkins > Manage and Assign Roles > Manage Roles
- Global roles:全局角色
- Item roles:项目角色
- dev.*:正则表达式匹配 dev 开头的项目。
- test.*:正则表达式匹配 test 开头的项目。
3. 创建用户
- Dashboard > Manage Jenkins > Jenkins’ own user database > 新建用户
4. 分配角色
- Dashboard > Manage Jenkins > Manage and Assign Roles > Assign Roles
4. Credentials Binding 使用凭证管理功能
- Username with password:用户名和密码。
- SSH Username with private key:使用 SSH 用户和密钥。
- Secret file:需要保密的文本文件,使用时 Jenkins 会将文件复制到一个临时目录中,再将文件路径设置到一个变量中,等构建结束后,所复制的 Secret file 就会被删除。
- Secret text:需要保存的一个加密的文本串,如钉钉机器人或 Github 的 api token。
- Certificate:通过上传证书文件的方式。
1. 创建凭证
- Dashboard > Manage Jenkins > Credentials > System > Global credentials (unrestricted)
5. Git 版本管理
yum install git -y
git --version
Started by user wy
Running as SYSTEM
Building in workspace /var/lib/jenkins/workspace/dev
The recommended git tool is: NONE
using credential 1d8dad31-e432-45b7-9aea-5482f5ab7096
Cloning the remote Git repository
Cloning repository http://192.168.31.110:85/jenkins/jenkins-admin.git
> git init /var/lib/jenkins/workspace/dev
Fetching upstream changes from http://192.168.31.110:85/jenkins/jenkins-admin.git
> git --version
> git --version
using GIT_ASKPASS to set credentials gitlab-Username
> git fetch --tags --force --progress -- http://192.168.31.110:85/jenkins/jenkins-admin.git +refs/heads/*:refs/remotes/origin/*
> git config remote.origin.url http://192.168.31.110:85/jenkins/jenkins-admin.git
> git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/*
Avoid second fetch
> git rev-parse refs/remotes/origin/master^{commit}
Checking out Revision 7ad80d4a04677aa88db3cc47f1f41d7538085658 (refs/remotes/origin/master)
> git config core.sparsecheckout
> git checkout -f 7ad80d4a04677aa88db3cc47f1f41d7538085658
Commit message: "初始化"
First time build. Skipping changelog.
Finished: SUCCESS
1. 配置 SSH
ssh-keygen -t rsa
id_rsa
id_rsa.pub
- 把 公钥 放在 Gitlab 中:
以 root 账户登录 > 点击头像 > Settings > SSH Keys - 在 Jenkins 中配置私钥:
添加一个新的凭证,类型为"SSH Username with private key"。
6. Deploy to container 部署
- 添加 Tomcat 用户凭证
- Dashboard > Manage Jenkins > Credentials > System > Global credentials (unrestricted)
7. Maven Integration 构建 Maven 项目
8. Pipeline 流水线构建
9. Gitlab Hook 和 GitLab 通过 Gitlab 触发
10. SonarQube Scanner 代码审查
11. Publish Over SSH 实现远程发送 Shell 命令
- 添加远程服务器 Dashboard > Manage Jenkins > System > Publish over SSH
12. NodeJS 前端打包
13. Extended Choice Parameter 多选框
14. Kubernetes 和 Kubernetes Continuous Deploy
三、Jenkins 工具
1. JDK
- Dashboard > Manage Jenkins > Tools > JDK
2. Maven
1. 安装 Maven
tar -xzf apache-maven-3.6.2-bin.tar.gz
mkdir -p /opt/maven
mv apache-maven-3.6.2/* /opt/maven
vi /etc/profile
`
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk export MAVEN_HOME=/opt/maven export PATH=$PATH:$JAVA_HOME/bin:$MAVEN_HOME/bin
`
source /etc/profile
mvn -v
<localRepository>/root/repo</localRepository>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
2. 配置 Maven
- Dashboard > Manage Jenkins > Tools > Maven
3. Tomcat
1. 安装 Tomcat
yum install java-1.8.0-openjdk* -y
tar -xzf apache-tomcat-8.5.47.tar.gz
mkdir -p /opt/tomcat
mv /root/apache-tomcat-8.5.47/* /opt/tomcat
/opt/tomcat/bin/startup.sh
2. 配置 Tomcat 用户角色权限
- http://127.0.0.1:8080/
- 用户及权限 /opt/tomcat/conf/tomcat-users.xml
- 用户名:tomcat
- 密码:tomcat
<tomcat-users>
<role rolename="tomcat"/>
<role rolename="role1"/>
<role rolename="manager-script"/>
<role rolename="manager-gui"/>
<role rolename="manager-status"/>
<role rolename="admin-gui"/>
<role rolename="admin-script"/>
<user username="tomcat" password="tomcat" roles="manager-gui,manager-script,tomcat,admin-gui,admin-script"/>
</tomcat-users>
- 注释这行配置 /opt/tomcat/webapps/manager/META-INF/context.xml
/opt/tomcat/bin/shutdown.sh
/opt/tomcat/bin/startup.sh
- 访问 http://127.0.0.1:8888/manager/html
四、Jenkins 全局属性
1. JAVA_HOME
2. M2_HOME
3. PATH+EXTRA
- Dashboard > Manage Jenkins > System > Environment variables
- JAVA_HOME
- M2_HOME
- PATH+EXTRA
五、Jenkins 项目构建
- Jenkins 中自动构建项目的类型有很多,常用的有以下三种:
- 自由风格软件项目(FreeStyle Project)
- Maven 项目(Maven Project)
- 流水线项目(Pipeline Project)
- 每种类型的构建其实都可以完成一样的构建过程与结果,只是在操作方式、灵活度等方面有所区别。
- 在实际开发中可以根据自己的需求和习惯来选择。
- 个人推荐使用流水线类型,因为灵活度非常高。
1. 自由风格项目构建
1. 新建项目
- Dashboard > All
2. 配置源码管理(Git 插件)
- Dashboard > dev > Configuration > 源码管理
3. 添加构建步骤
- Dashboard > dev > Configuration > Build Steps
- Execute shell
echo "开始编译和打包"
mvn clean package
echo "编译和打包结束"
4. 添加构建后操作
- Dashboard > dev > Configuration > 构建后操作
1. 构建 War 项目(Deploy to container 插件)
- Deploy war/ear to a container(Deploy to container 插件)
- WAR/EAR files:
target/*.war
正则匹配 target 目录 - Tomcat 8.x Remote:配置 Tomcat 工具
- Credentials:凭证
- Tomcat URL:部署
2. 构建 Jar 项目(Publish Over SSH 插件)
- Send build artifacts over SSH(Publish Over SSH 插件)
- SSH Server:远程服务器
- Source files:
target/*.jar
正则匹配 target 目录 - Remove prefix:移除
target/
前缀 - Exec command:执行的命令
echo "开始部署"
source /etc/profile
cd /root/TOOL
echo 最新构建时间 $(date) >> build.log
echo $(date +%Y%m%d) >> build_time.log
sh start.sh
echo "结束部署"
#!/bin/bash
set -e
SERVER_ADDRESS="192.168.1.100"
USERNAME="your_ssh_username"
PASSWORD="your_ssh_password"
REMOTE_PATH="/opt/myapp/"
LOCAL_PATH="target/myapp.jar"
echo "Copying JAR file to remote server..."
sshpass -p ${PASSWORD} scp -o StrictHostKeyChecking=no ${LOCAL_PATH} ${USERNAME}@${SERVER_ADDRESS}:${REMOTE_PATH}
echo "Starting application on remote server..."
sshpass -p ${PASSWORD} ssh -o StrictHostKeyChecking=no ${USERNAME}@${SERVER_ADDRESS} "cd ${REMOTE_PATH} && java -jar myapp.jar"
2. Maven 项目构建(Maven Integration 插件)
1. 新增项目
- Dashboard > All(Maven Integration 插件)
2. 构建
- Dashboard > demo_maven > Configuration > Build
- Root POM:指定 pom.xml 文件的路径
- Goals and options:输入 Maven 命令(注意不用写 mvn)
3. Pipeline 流水线项目构建(Pipeline 插件)
- 概念:Pipeline 简单来说,就是一套运行在 Jenkins 上的工作流框架,将原来独立运行于 单个 或者 多个 节点的任务连接起来,实现 单个 任务难以完成的 复杂流程编排 和 可视化 的工作。
- 使用 Pipeline 有以下好处(来自官方文档):
- 代码:Pipeline 以代码的形式实现,通常被检入源代码控制,使团队能够编辑,审查 和 迭代 其传送流程。
- 持久:无论是计划内的还是计划外的服务器重启,Pipeline 都是可恢复的。
- 可停止:Pipeline 可接收交互式输入,以确定是否继续执行 Pipeline。
- 多功能:Pipeline 支持现实世界中复杂的持续交付要求。它支持 fork/join、循环执行,并行执行任务的功能。
- 可扩展:Pipeline 插件支持其 DSL 的自定义扩展 ,以及与其他插件集成的多个选项。
- Pipeline 脚本是由 Groovy 语言实现的,但是我们没必要单独去学习 Groovy。
- Pipeline 支持两种语法:
- Declarative(声明式语法)。
- Scripted Pipeline(脚本式语法)。
- Pipeline 也有两种创建方法:
- 可以直接在 Jenkins 的 Web UI 界面中输入脚本。
- 也可以通过创建一个 Jenkinsfile 脚本文件放入项目源码库中。
一般我们都推荐在 Jenkins 中直接从源代码控制(SCM),中直接载入 Jenkinsfile Pipeline 这种方法。
1. 新建项目
- Dashboard > All(Pipeline 插件)
2. 流水线
- Dashboard > demo_pipeline > Configuration > 流水线
- Pipeline script > Hello World
pipeline {
agent any
stages {
stage('Hello') {
steps {
echo 'Hello World'
}
}
}
}
1. Pipeline script 界面编写脚本
1. Declarative 声明式 Pipeline
- Pipeline script > Hello World
pipeline {
agent any
stages {
stage('拉取代码') {
steps {
echo '拉取代码'
}
}
stage('编译构建') {
steps {
echo '编译构建'
}
}
stage('项目部署') {
steps {
echo '项目部署'
}
}
}
}
- Dashboard > demo_pipeline > #3 > 流水线步骤
- 显示没步的耗时。
2. Scripted Pipeline 脚本式 Pipeline
- Pipeline script > Scripted Pipeline
node {
def mvnHome
stage('拉取代码') {
echo '拉取代码'
}
stage('编译构建') {
echo '编译构建'
}
stage('项目部署') {
echo '项目部署'
}
}
- 拉取代码 Dashboard > demo_pipeline > 流水线语法
- checkout: Check out from version control
checkout scmGit(branches: [
[name: '*/master']
], extensions: [], userRemoteConfigs: [
[credentialsId: '7b1fce0d-30ea-4351-badc-5fa8751c2c06', url: 'git@192.168.31.110:jenkins/jenkins-admin.git']
])
- 编译构建 Dashboard > demo_pipeline > 流水线语法
- sh: Shell Script
sh 'mvn clean package'
- 项目部署 Dashboard > demo_pipeline > 流水线语法
- deploy: Deploy war/ear to a container
- Add Container:可以配置多个容器(集群部署)。
deploy adapters: [tomcat8(credentialsId: '49532328-b2d0-4b35-b11b-069916f89d4d', path: '', url: 'http://192.168.31.17:8888/')], contextPath: null, war: 'target/*.war'
pipeline {
agent any
stages {
stage('拉取代码') {
steps {
checkout([$class: 'GitSCM', branches: [
[name: '*/master']
], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [
[credentialsId: '7b1fce0d-30ea-4351-badc-5fa8751c2c06', url: 'git@192.168.31.110:jenkins/jenkins-admin.git']
]])
}
}
stage('编译构建') {
steps {
sh label: '', script: 'mvn clean package'
}
}
stage('项目部署') {
steps {
deploy adapters: [tomcat8(credentialsId: 'afc43e5e-4a4e-4de6-984f- b1d5a254e434', path: '', url: 'http://192.168.31.17:8888')], contextPath: null, war: 'target/*.war'
}
}
}
}
2. Pipeline script from SCM 版本控制脚本
- 上面直接在 Jenkins 的 UI 界面编写 Pipeline 代码,这样不方便脚本维护,建议把 Pipeline 脚本放在项目中(一起进行版本控制)。
1. 在项目根目录建立 Jenkinsfile 文件,把内容复制到该文件中
- 把 Jenkinsfile 上传到 Gitlab
2. 在项目中引用该文件
- Dashboard > demo_pipeline > Configuration > 流水线
- Pipeline script from SCM
- Dashboard > demo_pipeline > #5 > 流水线步骤