持续集成交付CICD:GitLab Webhook触发Jenkins流水线

目录

一、实验

1.Jenkins远程下载GiaLab仓库代码

2.curl远程触发Jenkins流水线

3.GitLab Webhook触发Jenkins流水线

二、问题

1.GitLab配置Webhook时报错


一、实验

1.Jenkins远程下载GiaLab仓库代码

(1) Jenkins添加选项参数

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第1张图片

(2)添加字符参数

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第2张图片

(3)查看构建参数情况

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第3张图片

(4)添加凭证

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第4张图片

(5)生成流水线脚本

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第5张图片

(6)生成

checkout scmGit(branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: '89c7573f-2d38-4aba-af7c-d8e62582b4d7', url: 'http://192.168.204.8:82/devops03/devops03-demo-service.git']])

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第6张图片

(7)保存流水线

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第7张图片

(8)开始构建feature-01持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第8张图片

(9)成功

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第9张图片

(10)先查看仓库

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第10张图片

(11)再查看分支

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第11张图片

(12)gitlab查看特性分支的2个文件

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第12张图片

(13) jenkins日志显示有2个文件

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第13张图片

(14)开始构建master

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第14张图片

(15)成功持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第15张图片

(16)修改回放

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第16张图片

(17)成功

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第17张图片

2.curl远程触发Jenkins流水线

(1)Jenkins 开启 Webhook Trigger

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第18张图片

(2)开启打印日志

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第19张图片

(3)添加token,以作业名命名

(4)curl远程触发

curl "http://192.168.204.15:8080/generic-webhook-trigger/invoke?token=devops03-demo-service" 

(5)成功

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第20张图片

(6)查看日志

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第21张图片

3.GitLab Webhook触发Jenkins流水线

(1)GitLab添加Webhooks的URL

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第22张图片

(2)提交特性分支

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第23张图片

(3)push

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第24张图片

(4)HTTP200 触发成功

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第25张图片

(5)jenkins成功触发流水线持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第26张图片

(6)查看日志

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第27张图片

(7)提交特性分支

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第28张图片

(8)选择edit查看(提交代码触发hook)

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第29张图片

(9)post请求(发送给jenkins分析)

Request URL: POST http://192.168.204.15:8080/generic-webhook-trigger/invoke?token=devops03-demo-service 200

Trigger: Push Hook

Elapsed time: 0.02 sec

Request time: 2 minutes ago

Request headers:
Content-Type: application/json
X-Gitlab-Event: Push Hook
Request body:
{
  "object_kind": "push",
  "event_name": "push",
  "before": "3e2975fa3f53a85535356ca0a4a4e3bf19ac6968",
  "after": "a99b9ea258b237739f675aa956939cbda1df8fdc",
  "ref": "refs/heads/feature-addmsg",
  "checkout_sha": "a99b9ea258b237739f675aa956939cbda1df8fdc",
  "message": null,
  "user_id": 1,
  "user_name": "Administrator",
  "user_username": "root",
  "user_email": "",
  "user_avatar": "https://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon",
  "project_id": 9,
  "project": {
    "id": 9,
    "name": "devops03-demo-service",
    "description": "",
    "web_url": "http://192.168.204.8:82/devops03/devops03-demo-service",
    "avatar_url": null,
    "git_ssh_url": "[email protected]:devops03/devops03-demo-service.git",
    "git_http_url": "http://192.168.204.8:82/devops03/devops03-demo-service.git",
    "namespace": "devops03",
    "visibility_level": 0,
    "path_with_namespace": "devops03/devops03-demo-service",
    "default_branch": "master",
    "ci_config_path": null,
    "homepage": "http://192.168.204.8:82/devops03/devops03-demo-service",
    "url": "[email protected]:devops03/devops03-demo-service.git",
    "ssh_url": "[email protected]:devops03/devops03-demo-service.git",
    "http_url": "http://192.168.204.8:82/devops03/devops03-demo-service.git"
  },
  "commits": [
    {
      "id": "a99b9ea258b237739f675aa956939cbda1df8fdc",
      "message": "Update build.sh   hehehe",
      "title": "Update build.sh   hehehe",
      "timestamp": "2023-11-27T15:50:36+08:00",
      "url": "http://192.168.204.8:82/devops03/devops03-demo-service/-/commit/a99b9ea258b237739f675aa956939cbda1df8fdc",
      "author": {
        "name": "Administrator",
        "email": "[email protected]"
      },
      "added": [

      ],
      "modified": [
        "build.sh"
      ],
      "removed": [

      ]
    }
  ],
  "total_commits_count": 1,
  "push_options": {
  },
  "repository": {
    "name": "devops03-demo-service",
    "url": "[email protected]:devops03/devops03-demo-service.git",
    "description": "",
    "homepage": "http://192.168.204.8:82/devops03/devops03-demo-service",
    "git_http_url": "http://192.168.204.8:82/devops03/devops03-demo-service.git",
    "git_ssh_url": "[email protected]:devops03/devops03-demo-service.git",
    "visibility_level": 0
  }
}
Response headers:
Date: Mon, 27 Nov 2023 07:50:37 GMT
Connection: close
X-Content-Type-Options: nosniff
Content-Type: application/json;charset=utf-8
Content-Length: 196
Server: Jetty(10.0.15)
Response body:
{"jobs":{"devops03/devops03-demo-service":{"regexpFilterExpression":"","triggered":true,"resolvedVari

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第30张图片

(10)分支信息

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第31张图片

(11)发送者

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第32张图片

(12)项目信息

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第33张图片

(13)提交信息

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第34张图片

(14)修改的内容

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第35张图片

(15)jenkins添加Post参数

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第36张图片

(16)修改流水线

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第37张图片

(17)添加script

webHookData = readJSON text: "${webHookData}"
userName = webHookData["user_username"]
userEmail = webHookData["user_email"]
branchName = webHookData["ref"] - "refs/heads/"
commitID = webHookData["checkout_sha"]

currentBuild.displayName = commitID
currentBuild.description = "Trigger by user ${userName} \n branch: ${branchName}"


pipeline {
    agent any

    stages {
        stage('resdJson') {
            steps {
                println("the user name is ${userName}")
                println("the trigger branch name is ${branchName}")
                println("the user email is ${userEmail}")

            }
        }
    }
}

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第38张图片

(18)再次修改流水线

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第39张图片

(19)edit

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第40张图片

(20)详细信息

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第41张图片

(21)resend

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第42张图片

(22)成功触发

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第43张图片

(23)成功

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第44张图片

(24)查看日志

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第45张图片

(25)这里显示gitlab分支id等信息

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第46张图片

(26)修改,删除一行

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第47张图片

(27)成功触发

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第48张图片

(28)只允许feature分支提交

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第49张图片

(29)修改feature分支内容

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第50张图片

(30)jenkins已拿到

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第51张图片

(31)修改release分支内容(jenkins没有触发)

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第52张图片

(32)添加允许release分支

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第53张图片

(33)此时一个项目有2个webhook(一个针对特性分支,一个针对版本分支)

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第54张图片

(34)修改release版本分支内容

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第55张图片

(35)jenkins已触发

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第56张图片

(36)手动测试报错

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第57张图片

(37)因为webhook是gitlab传过来的

(38)修改流水线,捕获异常

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第58张图片

(39)手动触发

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第59张图片

(40)输出信息报错

(41)修改代码,捕获异常

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第60张图片

(42)手动触发

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第61张图片

(43)查看日志

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第62张图片

(44)修改分支

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第63张图片

(45)成功触发

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第64张图片

4.GitLab Webhook触发邮箱发送

(1)修改邮箱,不起效果

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第65张图片

(2)修改admin邮箱

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第66张图片

(3)修改保存

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第67张图片

(4)删除第2邮箱

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第68张图片

(5)完成

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第69张图片

(6)邮箱已同步

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第70张图片

(7)修改release1.1.3

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第71张图片

(8)触发成功

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第72张图片

(9)未获取到邮箱

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第73张图片

(10)修改public email

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第74张图片

(11)查看日志

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第75张图片

(12)安装email 插件

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第76张图片

(13)修改feature分支

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第77张图片

(14)已拿到邮箱持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第78张图片(15)等待安装

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第79张图片

(16)输入邮箱

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第80张图片

(17)触发成功

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第81张图片

(18)修改文本类型为HTML

(19)邮箱整体配置

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第82张图片

(20)修改流水线


    webHookData = readJSON text: "${webHookData}"
    userName = webHookData["user_username"]
    userEmail = webHookData["user_email"]
    branchName = webHookData["ref"] - "refs/heads/"
    commitID = webHookData["checkout_sha"]
    currentBuild.displayName = commitID
    currentBuild.description = "Trigger by GitLab \n user: ${userName} \n branch: ${branchName}"

pipeline {
    agent any
    stages {
        stage('resdJson') {
            steps {            
               println("the user name is ${userName}")
               println("the trigger branch name is ${branchName}")
               println("the user email is ${userEmail}")
            }
        }
    }
    post {
        always {
            script {
               emailext body: 'hello world!....jenkins', subject: 'test.....', to: '[email protected]'
            }
        }
    }
}

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第83张图片

(21)gitlab 触发

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第84张图片

(22)触发成功

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第85张图片

(23)邮箱接收成功

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第86张图片

(24)查看日志

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第87张图片

(25)resend

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第88张图片

(26)修改流水线


    webHookData = readJSON text: "${webHookData}"
    userName = webHookData["user_username"]
    userEmail = webHookData["user_email"]
    branchName = webHookData["ref"] - "refs/heads/"
    commitID = webHookData["checkout_sha"]
    currentBuild.displayName = commitID
    currentBuild.description = "Trigger by GitLab \n user: ${userName} \n branch: ${branchName}"
pipeline {
    agent any

    stages {
        stage('resdJson') {
            steps {
                  echo "this is readJSON"
            }
        }
    }

    post {
        always {
            script {
                  EmailUser("${userEmail}",currentBuild.currentResult)
            }
        }

    }

}


def EmailUser(userEmail,status){
    emailext body: """
             
             
             
             
             
             
                

构建信息
""", subject: "Jenkins-${JOB_NAME}项目构建信息 ", to: userEmail

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第89张图片

(27)构建成功

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第90张图片

(28) 收到邮件持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第91张图片

二、问题

1.GitLab配置Webhook时报错

(1)报错

持续集成交付CICD:GitLab Webhook触发Jenkins流水线_第92张图片

(2)原因分析

未配置允许外部请求

(3)解决方法

进入 Admin area => Settings => Network ,然后点击 Outbound requests 右边 的“expand”按钮,按图片勾选,并点击 Save changes按钮

再重新配置Webhook即可正确生成Hook

你可能感兴趣的:(持续集成交付CICD,ci/cd,jenkins,运维)