# 如何在 push 到 GitHub 時觸發 Jenkins pipeline 並使用 PAT [TOC] ###### tags: `chatgpt` --- 在使用 GitHub 与 Jenkins 集成时,可以通过以下步骤实现在 push 到 GitHub 时触发 Jenkins pipeline 并使用 PAT: 1. 在 GitHub 上创建一个 Personal Access Token(PAT),用于 Jenkins 访问 GitHub 代码库。可以在 GitHub 上的 Settings -> Developer settings -> Personal access tokens 中创建 PAT,并将其保存到安全的位置。 2. 安装 GitHub 插件。在 Jenkins 中安装 GitHub 插件以实现与 GitHub 代码库的集成。可以在 Jenkins 的插件管理器中查找并安装 GitHub 插件。 3. 配置 Jenkins 与 GitHub 的集成。在 Jenkins 的系统管理中,配置 GitHub Server 访问 GitHub,需要提供之前创建的 PAT 和 GitHub API URL。将 Jenkins 与 GitHub 代码库进行关联,需要在 Jenkins 项目中配置 GitHub 仓库 URL 和凭据。 4. 配置 Jenkins pipeline。在 Jenkins 中创建 pipeline,配置 pipeline 触发器为 GitHub webhook,通过 GitHub webhook 实现 push 代码到 GitHub 时触发 Jenkins pipeline。 下面是一个简单的 pipeline 脚本示例: ```groovy pipeline { agent any stages { stage('Build') { steps { // build code } } stage('Test') { steps { // test code } } stage('Deploy') { steps { // deploy code } } } post { success { // send notification if success } failure { // send notification if failure } } } ``` 在 Jenkins 中创建 pipeline 后,需要将 pipeline 触发器配置为 GitHub webhook。在 GitHub 代码库中,添加一个 webhook 并指向 Jenkins pipeline,需要提供 Jenkins 服务器的 URL 和在 Jenkins 中设置的 secret token。 最后,当在 GitHub 上 push 代码时,会触发 GitHub webhook 并通知 Jenkins,Jenkins pipeline 将会开始执行。在 pipeline 执行完成后,Jenkins 可以向其他系统发送通知,以便实现更完整的持续集成和持续交付流程。 ## 前述 pipeline 中能否加上 error handling 是的,可以在 pipeline 中添加 error handling。在 Jenkins pipeline 中,可以使用 try-catch 块捕获异常并执行适当的操作。 下面是一个示例 pipeline,包含了一个 try-catch 块来捕获异常并发送通知: ```groovy pipeline { agent any stages { stage('Build') { steps { // build code } } stage('Test') { steps { // test code } } stage('Deploy') { steps { // deploy code } } } post { success { // send notification if success } failure { // send notification if failure } always { try { // do some cleanup } catch (Exception e) { // handle exception sh "echo 'Error during cleanup: ${e.message}'" } } } } ``` 在这个 pipeline 中,添加了一个 always 块,它会在 pipeline 执行结束后始终执行。在 always 块中,使用 try-catch 块捕获异常,并使用 sh 命令在 Jenkins 控制台中输出异常信息。根据需要,也可以将异常信息发送到 Slack、Email 等通知渠道,以便通知相关人员。 在实际使用过程中,应该根据具体的需求和情况,添加适当的 error handling。 ## 在這個 pipeline 中的 try-catch 塊中使用 LINE Notify 回報錯誤訊息 在 Jenkins pipeline 的 try-catch 块中,可以使用 sh 命令来调用 curl 命令,向 LINE Notify 接口发送消息通知。以下是一个简单的示例 pipeline: ```groovy pipeline { agent any stages { stage('Build') { steps { sh "mvn clean package" } } stage('Test') { steps { sh "mvn test" } } stage('Deploy') { steps { sh "mvn deploy" } } } post { success { sh "curl -X POST -H 'Authorization: Bearer [ACCESS_TOKEN]' -F 'message=Pipeline succeeded' https://notify-api.line.me/api/notify" } failure { try { sh "curl -X POST -H 'Authorization: Bearer [ACCESS_TOKEN]' -F 'message=Pipeline failed' https://notify-api.line.me/api/notify" } catch (Exception e) { sh "curl -X POST -H 'Authorization: Bearer [ACCESS_TOKEN]' -F 'message=Error during LINE Notify: ${e.message}' https://notify-api.line.me/api/notify" } } } } ``` 在上面的示例中,try-catch 块捕获异常并调用 curl 命令向 LINE Notify 接口发送错误信息。在 curl 命令中,使用 -F 参数指定要发送的消息内容,-H 参数指定 Authorization 头部中的 access token,以及 LINE Notify 接口的 URL。需要将 [ACCESS_TOKEN] 替换为你在 LINE Notify 网站上申请的 access token。 如果 pipeline 成功执行,将调用 success 块中的 curl 命令发送成功消息。如果 pipeline 执行失败,将调用 failure 块中的 curl 命令发送失败消息。如果在发送消息时发生异常,将调用 catch 块中的 curl 命令发送错误信息,包含异常信息。 在实际使用过程中,你可以根据具体的需求和情况调整消息内容和格式,并添加其他的 error handling。
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up