# Jenkins 接入 Server酱：流水线构建结果推送到微信（2026 教程）

> 在 Jenkins Pipeline 的 post 块中用 curl 调用 Server酱 API，构建成功或失败时自动推送消息到微信。

- 网页版：https://sct.ftqq.com/docs/integrations/jenkins/
- 更新日期：2026-07-17

# Jenkins + Server酱：流水线构建结果推送到微信

> 效果：Jenkins 流水线构建成功或失败时，自动把结果推送到微信

## 前置条件

- Server酱 SendKey：登录 sct.ftqq.com，在「SendKey」页面复制（[30 秒获取教程](https://sct.ftqq.com/docs/getting-started/sendkey/)）
- Jenkins 实例可访问公网（能发起 HTTPS 请求到 sctapi.ftqq.com）
- Pipeline 项目（声明式或脚本式均可）

## 配置步骤

### 1. 把 SendKey 存到 Jenkins 凭据

在 Jenkins 中添加一个 Secret text 类型的凭据，值为你的 SendKey，ID 设为 `serverchan-sendkey`。不要在 Jenkinsfile 里硬编码 SendKey。

### 2. 在 Pipeline 的 post 块中调用 Server酱

在声明式 Pipeline 的 `post` 块里，用 `withCredentials` 注入凭据，再用 `sh` 执行 curl：

```groovy
pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                echo 'Building...'
            }
        }
    }
    post {
        success {
            script {
                withCredentials([string(credentialsId: 'serverchan-sendkey', variable: 'SERVERCHAN_SENDKEY')]) {
                    sh '''
                        curl -sS -X POST "https://sctapi.ftqq.com/${SERVERCHAN_SENDKEY}.send" \
                          --data-urlencode "title=【构建成功】${JOB_NAME} #${BUILD_NUMBER}" \
                          --data-urlencode "desp=详情见 ${BUILD_URL}"
                    '''
                }
            }
        }
        failure {
            script {
                withCredentials([string(credentialsId: 'serverchan-sendkey', variable: 'SERVERCHAN_SENDKEY')]) {
                    sh '''
                        curl -sS -X POST "https://sctapi.ftqq.com/${SERVERCHAN_SENDKEY}.send" \
                          --data-urlencode "title=【构建失败】${JOB_NAME} #${BUILD_NUMBER}" \
                          --data-urlencode "desp=详情见 ${BUILD_URL}"
                    '''
                }
            }
        }
    }
}
```

### 3. Server酱³ 用户改用对应端点

如果 SendKey 以 `sctp` 开头，API 端点改为 `https://你的UID.push.ft07.com/send/你的SENDKEY.send`，其中 UID 取自 sctp 后的数字。

如需推送到企业微信/钉钉/飞书群，参见[通道配置](https://sct.ftqq.com/docs/getting-started/channels/)。

## 常见问题

**免费额度是多少？**

免费用户每天 5 条，每分钟上限 50 条。订阅会员目前特价最低 3 元/月，一天最多可发 1000 条（[价格详情](https://sct.ftqq.com/subscribe)）。

**构建跑完没收到消息？**

先在 Jenkins 构建日志里看 curl 是否报错；再检查 SendKey 是否正确、凭据 ID 是否匹配。仍无法解决可查[常见问题与排查](https://sct.ftqq.com/docs/getting-started/faq/)。

**多条流水线同时推送会被限流吗？**

Server酱 每分钟上限 50 条。若有多条流水线并发，建议在 title 里区分来源，或错开推送。

**可以用 HTTP Request 插件代替 curl 吗？**

可以。HTTP Request 插件能发任意 HTTP POST，把 URL 指向 Server酱 API、body 传 title 和 desp 即可，效果与 curl 一致。

## 相关集成

- [/integrations/python/](https://sct.ftqq.com/docs/integrations/python/) —— Python 脚本推送
- [/integrations/qinglong/](https://sct.ftqq.com/docs/integrations/qinglong/) —— 青龙面板任务通知
- [/integrations/uptime-kuma/](https://sct.ftqq.com/docs/integrations/uptime-kuma/) —— Uptime Kuma 宕机告警


## 懒人方案：把这段话复制给你的 AI 助手

把下面这段文字原样粘贴给 Claude Code、Cursor 或任何 AI 编程助手，它就会替你完成 Server酱 的接入：

```text
请帮我把 Server酱 微信通知接入到我当前的项目/工具中：
1. API：POST https://sctapi.ftqq.com/{SendKey}.send，参数 title（必填，不能含换行）、desp（正文，支持 Markdown，可选）；成功时返回 JSON 的 code 为 0。
2. 如果我的 SendKey 以 sctp 开头，则改用 https://{uid}.push.ft07.com/send/{SendKey}.send，uid 是 SendKey 中 sctp 与 t 之间的数字。
3. SendKey 从环境变量 SERVERCHAN_SENDKEY 读取；如果没有，提醒我到 https://sct.ftqq.com 免费获取（每天 5 条额度）。
4. 安全要求：不要把 SendKey 硬编码进代码、不要打印到日志、不要提交到 git。
5. 频率约定：任务结束发 1 条结果摘要即可，批量任务合并成一条，不要每步都推。
请根据我当前的项目和场景直接写好代码/配置并验证。
```

