一、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
- 手动安装
# 下载地址 https://jenkins.io/zh/download/
# 下载文件 jenkins-2.190.3-1.1.noarch.rpm
# 进行安装
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
- 修改 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 ~]# systemctl start jenkins.service
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 ~]# systemctl status jenkins.service
● 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 ~]# journalctl -xe
-- 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 或者其它,要改成 root
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. 端口占用
- 修改前 kill 所有 jenkins 进程
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 版本管理
# CentOS 安装 git
yum install git -y
# 安装后查看版本
git --version
Started by user wy
# 由用户wy启动
Running as SYSTEM
# 作为SYSTEM运行
Building in workspace /var/lib/jenkins/workspace/dev
# 在workspace/var/lib/jenkins/workspace/dev中构建
The recommended git tool is: NONE
# 建议使用的git工具是:NONE
using credential 1d8dad31-e432-45b7-9aea-5482f5ab7096
# 使用凭证1d8dad31-e432-45b7-9aea-5482f5ab7096
Cloning the remote Git repository
# 克隆远程Git存储库
Cloning repository http://192.168.31.110:85/jenkins/jenkins-admin.git
# 克隆存储库http://192.168.31.110:85/jenkins/jenkins-admin.git
> git init /var/lib/jenkins/workspace/dev # timeout=10
Fetching upstream changes from http://192.168.31.110:85/jenkins/jenkins-admin.git
# 正在从获取上游更改http://192.168.31.110:85/jenkins/jenkins-admin.git
> git --version # timeout=10
> git --version # 'git version 2.27.0'
using GIT_ASKPASS to set credentials gitlab-Username
# 使用GIT_ASKPASS设置凭据gitlab用户名
> git fetch --tags --force --progress -- http://192.168.31.110:85/jenkins/jenkins-admin.git +refs/heads/*:refs/remotes/origin/* # timeout=10
> git config remote.origin.url http://192.168.31.110:85/jenkins/jenkins-admin.git # timeout=10
> git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
Avoid second fetch
# 避免第二次提取
> git rev-parse refs/remotes/origin/master^{commit} # timeout=10
Checking out Revision 7ad80d4a04677aa88db3cc47f1f41d7538085658 (refs/remotes/origin/master)
> git config core.sparsecheckout # timeout=10
> git checkout -f 7ad80d4a04677aa88db3cc47f1f41d7538085658 # timeout=10
Commit message: "初始化"
# 交消息
First time build. Skipping changelog.
# 首次构建。正在跳过更改日志。
Finished: SUCCESS
# 完成:成功
1. 配置 SSH
ssh-keygen -t rsa
# 在 /root/.ssh/ 目录生成了 私钥 和 公钥
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
- master_server
- web_server
12. NodeJS 前端打包
13. Email Extension Template 邮箱
1. 系统管理员邮件地址
- Dashboard > Manage Jenkins > System
- Jenkins Location > 系统管理员邮件地址
2. Extended E-mail Notification
- Dashboard > Manage Jenkins > System
- Extended E-mail Notification
- SMTP Port:默认 25,SSL 加密 465
- Default Recipients:默认收件人(回复件)
3. Jenkins 默认邮箱通知
- Dashboard > Manage Jenkins > System
- 邮件通知:Jenkins 默认邮箱通知
4. 邮箱模版
- email.html
- 更多参数 Dashboard > Manage Jenkins > System
5. 发送配置
- Dashboard > demo_pipeline
- Declarative Directive Generator > post: Post Stage or Build Conditions
post {
always {
// One or more steps need to be included within each condition's block.
}
success {
// One or more steps need to be included within each condition's block.
}
failure {
// One or more steps need to be included within each condition's block.
}
}
- Dashboard > demo_pipeline > 流水线语法
- emailext: Extended Email
emailext body: '${FILE,path="email.html"}', subject: '构建通知:${PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS}!', to: '443749091@qq.com'
pipeline {
agent any
stages {
stage('拉取代码') { // for display purposes
steps {
echo '拉取代码'
checkout scmGit(branches: [
// [name: '*/master']
[name: '*/${branch}']
], extensions: [], userRemoteConfigs: [
[credentialsId: '7b1fce0d-30ea-4351-badc-5fa8751c2c06', url: 'git@192.168.31.110:jenkins/jenkins-admin.git']
])
}
}
stage('编译构建') {
steps {
echo '编译构建'
sh 'mvn clean package'
sh 'echo ${branch}'
}
}
stage('项目部署') {
steps {
echo '项目部署wywy'
deploy adapters: [tomcat8(credentialsId: '49532328-b2d0-4b35-b11b-069916f89d4d', path: '', url: 'http://192.168.31.17:8888/')], contextPath: null, war: 'target/*.war'
}
}
}
post {
always {
emailext body: '${FILE,path="email.html"}', subject: '构建通知:${PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS}!', to: '443749091@qq.com'
}
}
}
14. SonarQube Scanner 代码审查
1. SonarQube 安装
- SonarQube 是一个用于管理代码质量的开放平台,可以快速的定位代码中潜在的或者明显的错误。
- 目前支持 Java、C#、C/C++、Python、PL/SQL、Cobol、JavaScrip、Groovy 等二十几种编程语言的代码质量管理与检测。
- SonarQube 官网
- SonarQube 下载
- SonarSource/sonarqube
- Sonar 社区
- 环境要求:
- JDK 1.8
- MySQL 5.7(
注意版本
)- SonarQube 6.7.4
- 创建 sonar 数据库
- 解压 sonar,并设置权限
yum install unzip
# 解压
unzip sonarqube-7.9.6.zip
# 创建目录
mkdir /opt/sonar
# 移动文件
mv sonarqube-7.9.6/* /opt/sonar
# 创建 sonar 用户,必须 sonar 用于启动,否则报错
useradd sonar
# 更改 sonar 目录及文件权限
chown -R sonar. /opt/sonar
2. 修改 sonar 配置文件
vi /opt/sonar/conf/sonar.properties
`
sonar.jdbc.username=test
sonar.jdbc.password=test
sonar.jdbc.url=jdbc:mysql://192.168.31.200:3308/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false
`
3. 启动 sonar
cd /opt/sonar
# 启动
su sonar ./bin/linux-x86-64/sonar.sh start
# 查看状态
su sonar ./bin/linux-x86-64/sonar.sh status
# 停止
su sonar ./bin/linux-x86-64/sonar.sh stop
# 查看日志
tail -f logs/sonar.logs
4. 访问 sonar
- 访问 sonar
- 49c7e8df43231a98d1f47bf57e503371917a5916
5. SonaQube 配置
- SonaQube 关闭审查结果上传到 SCM 功能
6. Jenkins 配置
- Dashboard > Manage Jenkins > Credentials > System > Global credentials (unrestricted)
- 配置 Dashboard > Manage Jenkins > System
- SonarQube servers
- Name:sonarqube-6.7.4
- Server URL:http://192.168.31.110:9000
- Server authentication token:49c7e8df43231a98d1f47bf57e503371917a5916
- 工具 Dashboard > Manage Jenkins > Tools
- SonarQube Scanner 安装
- Name:sonarqube-6.7.4
7. 非流水线项目
- 增加构建步骤 Execute SonarQube Scanner
- Task to run:scan
- JDK:选择 JDK 工具
- Path to project properties:项目属性的路径
- Analysis properties:分析属性
- Execute SonarQube Scanner
# must be unique in a given SonarQube instance
sonar.projectKey=jenkins-admin
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=jenkins-admin
sonar.projectVersion=1.0
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# This property is optional if sonar.modules is set.
sonar.sources=.
sonar.exclusions=**/test/**,**/target/**
sonar.java.source=1.8
sonar.java.target=1.8
# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8
sonar.java.binaries=./target/classes
- SonarQube
8. 流水线项目
- sonar-project.properties
- Jenkinsfile
15. Extended Choice Parameter 多选框
16. 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
# 查找 Maven 版本
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 > Maven 安装
3. Tomcat
1. 安装 Tomcat
# 安装JDK
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
# 启动 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
<!--
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
-->
- 重启
# 停止
/opt/tomcat/bin/shutdown.sh
# 启动
/opt/tomcat/bin/startup.sh
- 访问 http://127.0.0.1:8888/manager/html
4. NodeJS
- Dashboard > Manage Jenkins > Tools > NodeJS 安装
yum install epel-release
npm install -g yarn
四、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. 动态参数
- Dashboard > demo_pipeline > Configuration > General
- This project is parameterized
- Boolean Parameter:Boolean 参数
- Choice Parameter:选择参数
- Credentials Parameter:凭据参数
- File Parameter:文件参数
- Multi-line String Parameter:
- Password Parameter:密码参数
- Run Parameter:
- String Parameter:字符串参数
- Dashboard > demo_pipeline
v1:指定分支名
- ${branch}:获取分支参数
3. 配置源码管理(Git 插件)
- Dashboard > dev > Configuration > 源码管理
4. 添加构建步骤
- Dashboard > dev > Configuration > Build Steps
- Execute shell
echo "开始编译和打包"
mvn clean package
echo "编译和打包结束"
5. 添加构建后操作
- 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"
# 远程服务器 JAR 包部署路径
REMOTE_PATH="/opt/myapp/"
# 本地 JAR 包路径
LOCAL_PATH="target/myapp.jar"
# 将 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 的自定义扩展 ,以及与其他插件集成的多个选项。
- 如何创建 Jenkins Pipeline 呢?
- 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:代表整个流水线的所有执行阶段。
通常 stages 只有1个,里面包含多个 stage。
*/
stages {
/*
stage:代表流水线中的某个阶段,可能出现 n 个。
一般分为 拉取代码,编译构建,部署 等阶段。
*/
stage('Hello') {
/*
steps:代表一个阶段内需要执行的逻辑。
steps 里面是 shell 脚本,git 拉取代码,ssh 远程发布 等任意内容。
*/
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('拉取代码') { // for display purposes
echo '拉取代码'
}
stage('编译构建') {
echo '编译构建'
}
stage('项目部署') {
echo '项目部署'
}
}
3. 拉取代码
- Dashboard > demo_pipeline > 流水线语法
- checkout: Check out from version control
- War
checkout scmGit(branches: [
[name: '*/master']
], extensions: [], userRemoteConfigs: [
[credentialsId: '7b1fce0d-30ea-4351-badc-5fa8751c2c06', url: 'git@192.168.31.110:jenkins/jenkins-admin.git']
])
- Jar
checkout scmGit(branches: [
[name: '*/master']
], extensions: [], userRemoteConfigs: [
[credentialsId: '7b1fce0d-30ea-4351-badc-5fa8751c2c06', url: 'git@192.168.31.110:jenkins/jenkins-jar.git']
])
- Web
checkout scmGit(branches: [
[name: '*/master']
], extensions: [], userRemoteConfigs: [
[credentialsId: '7b1fce0d-30ea-4351-badc-5fa8751c2c06', url: 'git@192.168.31.110:jenkins/jenkins-web.git']
])
4. 编译构建
- Dashboard > demo_pipeline > 流水线语法
- sh: Shell Script
- War、Jar
sh 'mvn clean package'
- Web
sh '''yarn install
yarn build'''
5. 项目部署 War
- 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'
6. 项目部署 Jar
- Dashboard > demo_pipeline > 流水线语法
- sshPublisher: Send build artifacts over SSH
sshPublisher(publishers: [
sshPublisherDesc(configName: 'master_server',
transfers: [
sshTransfer(cleanRemote: false, excludes: '',
execCommand:
'''echo "开始部署"
source /etc/profile
cd /root/TOOL
echo 最新构建时间 $(date) >> build.log
echo $(date +%Y%m%d) >> build_time.log
sh start.sh
echo "结束部署" ''',
execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false,
removePrefix: 'target/', sourceFiles: 'target/*.jar'
)
],
usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false
)]
)
7. 项目部署 Web
- Dashboard > demo_pipeline > 流水线语法
- sshPublisher: Send build artifacts over SSH
sshPublisher(publishers: [
sshPublisherDesc(configName: 'master_server',
transfers: [
sshTransfer(cleanRemote: false, excludes: '', execCommand: '', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+',
remoteDirectory: '/usr/share/nginx/html', remoteDirectorySDF: false,
removePrefix: 'dist', sourceFiles: 'dist/**'
)
],
usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false
)
])
8. 完整的声明式
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'
}
}
}
}
9. 完整的脚本式
- War
node {
def mvnHome
stage('拉取代码') { // for display purposes
echo '拉取代码'
checkout scmGit(branches: [
[name: '*/master']
], extensions: [], userRemoteConfigs: [
[credentialsId: '7b1fce0d-30ea-4351-badc-5fa8751c2c06', url: 'git@192.168.31.110:jenkins/jenkins-admin.git']
])
}
stage('编译构建') {
echo '编译构建'
sh 'mvn clean package'
}
stage('项目部署') {
echo '项目部署'
deploy adapters: [tomcat8(credentialsId: '49532328-b2d0-4b35-b11b-069916f89d4d', path: '', url: 'http://192.168.31.17:8888/')], contextPath: null, war: 'target/*.war'
}
}
- Jar
node {
def mvnHome
stage('拉取代码') { // for display purposes
echo '拉取代码'
checkout scmGit(branches: [
[name: '*/master']
], extensions: [], userRemoteConfigs: [
[credentialsId: '7b1fce0d-30ea-4351-badc-5fa8751c2c06', url: 'git@192.168.31.110:jenkins/jenkins-jar.git']
])
}
stage('编译构建') {
echo '编译构建'
sh 'mvn clean package'
}
stage('项目部署') {
echo '项目部署'
sshPublisher(publishers: [
sshPublisherDesc(configName: 'master_server',
transfers: [
sshTransfer(cleanRemote: false, excludes: '',
execCommand:
'''echo "开始部署"
source /etc/profile
cd /root/TOOL
echo 最新构建时间 $(date) >> build.log
echo $(date +%Y%m%d) >> build_time.log
sh start.sh
echo "结束部署" ''',
execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false,
removePrefix: 'target/', sourceFiles: 'target/*.jar'
)
],
usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false
)]
)
}
}
- Web
node {
def mvnHome
stage('拉取代码') { // for display purposes
echo '拉取代码'
checkout scmGit(branches: [
[name: '*/master']
], extensions: [], userRemoteConfigs: [
[credentialsId: '7b1fce0d-30ea-4351-badc-5fa8751c2c06', url: 'git@192.168.31.110:jenkins/jenkins-web.git']
])
}
stage('编译构建') {
echo '编译构建'
sh '''yarn install
yarn build'''
}
stage('项目部署') {
echo '项目部署'
sshPublisher(publishers: [
sshPublisherDesc(configName: 'web_server',
transfers: [
sshTransfer(cleanRemote: false, excludes: '', execCommand: '', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+',
remoteDirectory: '/usr/share/nginx/html', remoteDirectorySDF: false,
removePrefix: 'dist', sourceFiles: 'dist/**'
)
],
usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false
)
])
}
}
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 > 流水线步骤
文章来源地址https://www.toymoban.com/news/detail-608585.html
文章来源:https://www.toymoban.com/news/detail-608585.html
到了这里,关于Jenkins 安装构建的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!