在Jenkins pipeline的groovy脚本中可以实现很多复杂灵活的功能,但是:
1 一来对java、groovy不是很熟,也不知道能不能引入一些三方库?比如搞个jdbc操作下mysql什么的。
2 二是自己对go和python比较熟悉,所以想能够更加灵活的实现更多的功能:
比如:
stage('send email to sendList and exec mysqlDB') {
                checkout([$class: 'GitSCM', branches: [[name: '*/master']], 
                doGenerateSubmoduleConfigurations: false, 
                extensions: [], 
                submoduleCfg: [], 
                userRemoteConfigs: [[credentialsId: 'gitlabAdmin', 
                url: 'http://gitlab-ip/group/go-script.git']]])
                def sendList = [
                    "XXXXXXXX@qq.com",
                    "XXXXXXXX@qq.com",
                    "XXXXXXXX@qq.com",
                    "XXXXXXXX@qq.com",
                    "XXXXXXXX@qq.com"
                    ]
                def goMail = [
                    user: "发送者邮箱地址",
                    secret: "发送者邮箱秘钥",
                    smtpHost: "smtp.qq.com",
                    sslPort: "465",
                    sendTo: sendList,
                    subject: "测试subject",
                    content: "测试content",      // 这里自定义html,或者将html模板放在代码库中替换,  
                                          // 在go脚本中定义好相应的header就行了
                ]
                def goMysql = [
                    DSN: "root:passwd@tcp(1.1.1.1:3306)/jenkins?charset=utf8",
                    sql: [
                        "update XXX set XX=XX where id=XX AND XX=XX",
                        "update XXX set XX=XX where id=XX AND XX=XX",
                        "update XXX set XX=XX where id=XX AND XX=XX",
                        "update XXX set XX=XX where id=XX AND XX=XX"
                     ]
                ]
                def scriptParams = [
                    GoMail: goMail,
                    GoMysql: goMysql
                ]
                def output = JsonOutput.toJson(scriptParams)
                try {
                    echo '执行go脚本,参数:'
                    println(output);  
                    sh "chmod +x ./run && ./run -scriptParams /'${output}/'"
                }
  type GoMail struct {
    User     string   `json:"user"`
    Secret   string   `json:"secret"`
    SmtpHost string   `json:"smtpHost"`
    SslPort  string   `json:"sslPort"`
    SendTo   []string `json:"sendTo"`
    Subject  string   `json:"subject"`
    Content  string   `json:"content"`
}
type GoMysql struct {
    DSN        string `json:"DSN"`
    Sql []string `json:"sql"`
}
type ScriptParams struct {
    GoMail  GoMail  `json:"GoMail"`
    GoMysql GoMysql `json:"GoMysql"`
}