一、jenkins分布式节点
1)创建节点
- 创建远程工作目录


- 运行节点(依赖jdk)


2)创建项目
- jenkins添加webhook
选择gitee,添加webhook
# 使用ngrok映射公网地址
sudo tar -xvzf ngrok-v3-stable-linux-amd64.tgz -C /usr/local/bin
ngrok config add-authtoken 2hVzpESrnADT1PJqcdGCmYmDWmI_6LZKZXm83s6uvg6WfbgSH
ngrok http 8080
# 访问
https://356b-112-20-67-139.ngrok-free.app/generic-webhook-trigger/invoke?token=fa0apo-weqa-093d-seffe@123

-
添加git源码管理
jenkins添加gitee,ssh方式连接
查看私钥:cat ~/.ssh/id_rsa

-
执行shell(自动构建镜像并推送)
docker build -t autotpsite:v1 .
docker tag autotpsite:v1 192.168.xxx.xxx:5001/autotpsite:v1
docker login 192.168.xxx.xxx:5001
docker push 192.168.xxx.xxx:5001/autotpsite:v1
二、pipeline流水线
pipeline的出现代表企业人员可以更自由的通过代码来实现不同的工作流程。

1)流水线结构
Node:节点(某台机器),执行任务的具体环境
Stage:环节,表示一组操作,通常用来逻辑划分
脚本语法:shell+groovy
容器部署jenkins:
docker run -id --name jenkins2 -p 8080:8080 -p 50000:50000 -v /var/jenkins_node:/var/jenkins_home --restart=always --privileged=true registry.cn-hangzhou.aliyuncs.com/sqqdcl/jenkins:v5
2)流水线语法
分为声明式语法和脚本式语法,一般使用脚本式语法。

流水线脚本配置Jenkinsfile:
node("autotpsite"){ //指定标签是autotpsite的节点运行此任务
// 需要运行的任务,命令行
stage("停止服务"){
checkout scm //下载同步代码文件
sh "docker-compose down"
}
stage("构建"){
//sh "docker build -t autotpsite:v2"
//测试流水线脚本
//def yml_content = readYaml file: 'docker-compose.yml'
//println(yml_content)
//sh "cd autotpsite && cat settings.py" //jenkins默认工作目录下
dir('autotpsite'){
sh "cat settings.py"
}
}
stage("打标签"){
//sh "docker tag autotpsite:v2 192.168.xxx.xxx:5001/autotpsite:v2"
}
stage("重启服务"){
sh "docker-compose up -d"
}
jenkins添加流水线脚本

3)YAML配置流水线
准备:安装插件Pipeline Utility steps

该插件提供readYaml函数,可以直接在pipeline中使用readYaml读取并解析yml文件
语法:readYaml file:'path/conf.yml'
重新构建流水线模式:
采用yml文件配置流水线(build.yml)
stages:
停止服务:
- echo 停止服务
- docker-compose down
- cat settings.py
构建:
- echo 构建镜像
打标签:
- echo 打标签
- cat settings.py
重启服务:
- docker-compose up -d
utils.groovy
def runPipeline() {
// 需要运行的任务,命令行
stage("停止服务"){
sh "docker-compose down"
}
stage("构建"){
//sh "docker build -t autotpsite:v2"
//测试流水线脚本
def yml_content = readYaml file: 'docker-compose.yml'
println(yml_content)
}
stage("打标签"){
//sh "docker tag autotpsite:v2 192.168.174.128:5001/autotpsite:v2"
}
stage("重启服务"){
sh "docker-compose up -d"
}
}
//根据yaml配置文件运行流水线脚本
def runWithYaml(path) {
conf = readYaml file: path
stages = conf.stages.keySet() //取出所有步骤名称
for (name in stages){
stage(name){ //pipeline函数
scripts = conf.stages[name]
for(code in scripts){
sh code //执行每个动作
}
}
}
}
return this //一定要加上这句话
Jenkinsfile
node("autotpsite"){ //指定标签是autotpsite的节点运行此任务
checkout scm //下载同步代码文件
//引入自定义库
//evaluate(new File("utils.groovy")) //不适用于jenkinsfile
//def tools = new utils()
def tools = load("utils.groovy")
//执行groovy脚本
//tools.runPipeline()
//根据yaml配置文件运行流水线脚本
tools.runWithYaml('build.yml')
}
1351

被折叠的 条评论
为什么被折叠?



