felix-cao / Blog

A little progress a day makes you a big success!
31 stars 4 forks source link

基于 Jenkins 的 pipeline 实现远程主机通信及发送邮件 #205

Open felix-cao opened 3 years ago

felix-cao commented 3 years ago

一、安装 pipeline

Manage Jenkins -> Manage plugins -> Available -> 输入 pipeline

安装后重启 Jenkins

按照 官方 doc 创建一个 item, 在 Script text area 输入官方提供的脚本

pipeline {
    agent any 
    stages {
        stage('Stage 1') {
            steps {
                echo 'Hello world!' 
            }
        }
    }
}

跑一把看看效果 image

二、发送邮件

2.1 脚本

下载安装 Email Extension Plugin 插件, 在 Configure System 中配置 Extended E-mail Notification,具体方法及步骤参考 Jenkins 邮件通知配置

pipeline 脚本中添加 post 部分

pipeline {
    agent any 
    stages {
        stage('Stage 1') {
            steps {
                echo 'Hello world!' 
            }
        }
    }
    post {
        success {
            emailext (
                subject: "SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",
                body: """<p>SUCCESSFUL: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':</p>
                    <p>Check console output at "<a href="${env.BUILD_URL}">${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>"</p>""",
                to: "491766244@qq.com,czf2008700@163.com",
                from: "THC AutoTest-Monitor<491766244@qq.com>"
            )
        }
        failure {
            emailext (
                subject: "FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]'",
                body: """<p>FAILED: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]':</p>
                    <p>Check console output at "<a href="${env.BUILD_URL}">${env.JOB_NAME} [${env.BUILD_NUMBER}]</a>"</p>""",
                to: "491766244@qq.com,czf2008700@163.com",
                from: "THC AutoTest-Monitor<491766244@qq.com>"
            )
        }
    }
}

2.2 URL 触发

image

http://ip/job/THC_Message_Notification/build?token=12212

2.3 带参数的URL触发

script 中配置参数

pipeline {
    agent any
    parameters {
        string(defaultValue: 'czf', description: '', name: 'name')
    }
    // ....
}

注意 &name 前有转义符 \

curl http://***.***.***.***:8081/job/THC_Message_Notification/buildWithParameters?token=12212\&name=zfcao

三、远程主机通信

3.1、创建 Jenkins 服务器与 被控主机之间的 免密登录,

具体方法和步骤参考 linux 下开启 SSH,远程免密登录

3.2、设置凭据配置后,再去 Manage Credentials

image

3.3、添加凭证

image

注意, Private KeyJenkins 服务器中的 私钥

3.4、安装 SSH Agent

Manage Jenkins -> Manage plugins -> Available -> 输入 SSH Agent, 这里有一篇不错的 SSH Agent 知识

3.5、编写 pipeline

stage('client160.51') {
    steps {
        sshagent(credentials: ['client160.51']) {
            sh "ssh -o StrictHostKeyChecking=no -l root 42.192.160.51 'uname -a; ifconfig; cd /home; mkdir hello'"
        }
    }
}

这里的 shell 的写法 参考 Reference2

Reference

  1. Jenkins User Documentation

  2. 使用shell登录远程服务器执行多条命令,ssh登录之后执行脚本文件

felix-cao commented 2 years ago

远程触发

Jenkins 用户账号密码触发任务或用户 API token, 参考 how to trigger jenkins job via curl command remotely

curl -I -u hsxue:password http://j.cxxx.cn/job/msg_notification/buildWithParameters?token=thcweb&name=czf

但这有个弊端,处理的参数数量有限

curl -X POST http://j.cxxx.cn/job/msg_notification/buildWithParameters?token=thcweb \
--user hsxue:'password' \
--data name=czf

上面的脚本会报错: image

改为 apitoken 就不报错了, 参考 Jenkins: 403 No valid crumb was included in the request

API token: Accounts icon -> configure -> API Token -> Add New token

curl -X POST http://j.cxxx.cn/job/msg_notification/buildWithParameters?token=thcweb \
--user hsxue:apitoken \
--data name=czf
felix-cao commented 2 years ago

给你的Jenkins Console加上颜色

安装 AnsiColor Manage Jenkins -> Manage plugins -> Available -> 输入 ansicolor;

pipeline 中加入

    agent any
    options {
        ansiColor('xterm')
    }

参考 Jenkins ANSI Color Plugin

jest 单元测试的 log 加颜色

yarn add ansi-colors -D

参考npm 之 ansi-colors

最终结果

image

image

felix-cao commented 2 years ago

Jenkins 添加新的全局 credentials

参考 Jenkins 实例添加新的全局凭证