2026-01-14 21:30:20 +08:00

231 lines
6.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## Jenkins自动化DevOps共享库
Docker常规命令
```
# 查看docker磁盘使用情况
docker system df
# 查看更详细的信息
docker system df -v
# 用于清理磁盘删除关闭的容器、无用的数据卷和网络以及dangling镜像(即无tag的镜像)
docker system prune
```
1、首先在 Jenkins 中正确配置 Shared Library
步骤 1在 Jenkins 中配置 Global Shared Library
进入 Jenkins → Manage Jenkins → Configure System
找到 Global Pipeline Libraries 部分
点击 Add 添加新的库配置:
```groovy
Name: share-library # 库的名称(必须与@Library中的名称匹配
Default version: master # 默认分支,可以是 mastertest
Retrieval method: Modern SCM
Source Code Management: Git
Project Repository: https://github.com/your-username/share-library.git
Credentials: 添加您的 Git 凭据 ID
```
步骤 2检查库的配置详情
确保配置正确:
Name 必须与 @Library('share-library')_ 中的名称完全匹配
如果使用 @master 分支,需要确保该分支在 Git 仓库中存在
凭据必须有访问 Git 仓库的权限
Jenkins安装插件
1、HTTP Request
2、Build User Vars 用于获取构建人信息
3、SSH server
4、Pipeline
5、DingTalk
钉钉消息案例:
```
// 基本使用
def dingtalk = new DingTalkNotifier(this)
dingtalk.sendBuildSuccessMessage("my-project", "main", 120000)
// 完整参数使用
def dingtalk = new DingTalkNotifier(
this, // script
false, // atAll
[], // atMobiles
'markdown', // msgType
'my-dingtalk-robot', // credentialsId
3, // retryCount
2000 // retryInterval
)
// 运行时更新配置
dingtalk.updateMsgType('text')
dingtalk.updateAtSettings(true, ['13800138000'])
```
新版本使用案例:
```
try{
// 初始化钉钉通知器
def dingTalk = new com.docker.DingTalkNotifier(
this,
false,
['13800138000'],
[],
'markdown',
)
echo "钉钉新服务:${dingTalk}"
// 发送构建开始消息
dingTalk.sendBuildStartMessage(
env.JOB_NAME,
env.GIT_BRANCH ?: '',
'修复bug',
'developer',
[环境: '测试环境', 版本: 'v1.0.0']
)
// ActionCard 消息示例
dingTalk.sendMultiActionCard(
"构建完成通知",
"项目构建已完成,请查看详情",
[
[title: "查看构建详情", actionURL: "${env.BUILD_URL}"],
[title: "下载制品", actionURL: "https://www.dingtalk.com/"],
[title: "查看测试报告", actionURL: "https://www.dingtalk.com/"]
]
)
// FeedCard 消息示例
dingTalk.sendFeedCardMessage([
[title: "生产环境部署成功", messageURL: "https://www.dingtalk.com/", picURL: "https://gw.alicdn.com/imgextra/i4/O1CN017tMvJS1jaMdFd8RSv_!!6000000004564-2-tps-640-368.png"],
[title: "测试环境构建完成", messageURL: "https://www.dingtalk.com/", picURL: "https://gw.alicdn.com/imgextra/i4/O1CN017tMvJS1jaMdFd8RSv_!!6000000004564-2-tps-640-368.png"]
])
}catch(Exception e) {
echo "⚠️ 钉钉消息发送失败: ${e.message}"
}
```
```js
stage('发送各种类型消息') {
steps {
script {
// 1. 文本消息
dingtalk.sendTextMessage(
"简单文本消息内容",
false,
['13800138000'],
[]
)
// 2. Markdown消息
dingtalk.sendMarkdownMessage(
"Markdown标题",
"### 这是Markdown内容\n- 列表项1\n- 列表项2\n**加粗文本**",
false,
['13800138000'],
[]
)
// 3. 链接消息
dingtalk.sendLinkMessage(
"链接标题",
"链接描述内容",
"https://www.example.com",
"https://img.example.com/image.jpg"
)
// 4. 独立ActionCard
dingtalk.sendSingleActionCard(
"卡片标题",
"卡片内容描述",
"查看详情",
"https://www.example.com",
'0' // 按钮方向0-竖直
)
// 5. 多按钮ActionCard
def buttons = [
[title: "按钮1", actionURL: "https://example.com/btn1"],
[title: "按钮2", actionURL: "https://example.com/btn2"],
[title: "按钮3", actionURL: "https://example.com/btn3"]
]
dingtalk.sendMultiActionCard(
"多按钮卡片",
"请选择操作:",
buttons,
'1' // 按钮方向1-横向
)
// 6. FeedCard消息
def links = [
[
title: "新闻标题1",
messageURL: "https://example.com/news1",
picURL: "https://img.example.com/news1.jpg"
],
[
title: "新闻标题2",
messageURL: "https://example.com/news2",
picURL: "https://img.example.com/news2.jpg"
]
]
dingtalk.sendFeedCardMessage(links)
}
}
}
```
消息通知发布案例:
```
// 在Pipeline中初始化
def notificationService = new NotificationService(this, params, stageService)
// 构建成功时发送通知
notificationService.sendBuildSuccess(
projectName: "my-project",
branch: "master",
duration: currentBuild.duration,
extraInfo: [version: "1.0.0", commit: "abc123"]
)
// 构建失败时发送通知
notificationService.sendBuildFailure(
projectName: "my-project",
branch: "feature/branch",
duration: currentBuild.duration,
errorMsg: "单元测试失败",
extraInfo: [stage: "test", errorDetails: "具体错误信息"]
)
// 部署通知
notificationService.sendDeploymentNotification(
projectName: "my-project",
environment: "prod",
version: "1.0.0",
status: "success",
deployUrl: "http://deploy.example.com"
)
```
钉钉客服:
```
小钉钉客服1号
https://oapi.dingtalk.com/robot/send?access_token=976ae1a272bc79a1c3bf2eba97dae488eec80b57387d17e1c8352627b963bb5c
SEC0e2d1bed404cc0615885321276766598ce2efefcd0be23bfffe96c6b8f4d0ed6
小钉钉客服2号
https://oapi.dingtalk.com/robot/send?access_token=c7fd2348813bacd0797f141bc579e7f1db31d50e00ebc8af658a769534ac755c
SEC26137d6d5ff8f9ecda917e787ec31c2b3804e7820d93363a1a9f0770f1720f2d
```