[Cloud native | Kubernetes] Detailed explanation of WebHooks based on GitLab


1. WebHooks based on GitLab

An operation to be automated here CI, that is, after the developer Pushcode is placed in the Gitwarehouse, Jenkinsthe project will be automatically built, and the latest submission point code will be built and packaged and deployed. Here, the above CDoperations are distinguished. The CDoperation needs to be deployed based on a certain version. , and here every time the latest commit point is integrated into the trunk and tested.

1.1 WebHooks Notification

JenkinsAutomatic builds enabled :

build trigger
[External link image transfer failed, the source site may have an anti-leech mechanism, it is recommended to save the image and upload it directly (img-eljuwASF-1661690703508)(Pictures/1642500817131.png)]

Set up Gitlab's webhooks:

Setting up Gitlab's webhooks
[External link image transfer failed, the source site may have an anti-leech mechanism, it is recommended to save the image and upload it directly (img-8azha0AF-1661690703510)(Pictures/1642500933316.png)]

Jenkins Gitlabauthentication needs to be turned off:

Turn off Gitlab authentication for Jenkins
[External link image transfer failed, the source site may have anti-leech mechanism, it is recommended to save the image and upload it directly (img-NLEfA09l-1661690703511)(Pictures/1642501016474.png)]

Test Gitlab again:

test again
[External link image transfer failed, the source site may have an anti-leech mechanism, it is recommended to save the image and upload it directly (img-ZOYeaBTE-1661690703512)(Pictures/1642501065243.png)]

1.2 Modify the configuration

Modify the Jenkinsfileimplementation to achieve continuous integration based on the latest commit point, and remove all the previous references to ${tag}:

// 所有的脚本命令都放在pipeline中
pipeline{
    
    
	// 指定任务再哪个集群节点中执行
	agent any

	// 声明全局变量,方便后面使用
	environment {
    
    
		harborUser = 'admin'
        harborPasswd = 'Harbor12345'
        harborAddress = '192.168.11.102:80'
        harborRepo = 'repo'
	}

    stages {
    
    
        stage('拉取git仓库代码') {
    
    
            steps {
    
    
                checkout([$class: 'GitSCM', branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[url: 'http://192.168.11.101:8929/root/mytest.git']]])
            }
        }
        stage('通过maven构建项目') {
    
    
            steps {
    
    
                sh '/var/jenkins_home/maven/bin/mvn clean package -DskipTests'
            }
        }
        stage('通过SonarQube做代码质量检测') {
    
    
            steps {
    
    
                sh '/var/jenkins_home/sonar-scanner/bin/sonar-scanner -Dsonar.source=./ -Dsonar.projectname=${JOB_NAME} -Dsonar.projectKey=${JOB_NAME} -Dsonar.java.binaries=./target/ -Dsonar.login=40306ae8ea69a4792df2ceb4d9d25fe8a6ab1701'
            }
        }
        stage('通过Docker制作自定义镜像') {
    
    
            steps {
    
    
                sh '''mv ./target/*.jar ./docker/
                docker build -t ${JOB_NAME}:latest ./docker/'''
            }
        }
        stage('将自定义镜像推送到Harbor') {
            steps {
                sh '''docker login -u ${harborUser} -p ${harborPasswd} ${harborAddress}
                docker tag ${JOB_NAME}:latest  ${harborAddress}/${harborRepo}/${JOB_NAME}:latest
                docker push ${harborAddress}/${harborRepo}/${JOB_NAME}:latest '''
            }
        }
        stage('将yml文件传到k8s-master上') {
            steps {
                sshPublisher(publishers: [sshPublisherDesc(configName: 'k8s', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: 'pipeline.yml')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
            }
        }
        stage('远程执行k8s-master的kubectl命令') {
            steps {
               sh '''ssh [email protected] kubectl apply -f /usr/local/k8s/pipeline.yml
                ssh [email protected] kubectl rollout restart deployment pipeline -n test'''
            }
        }

    }
    post {
        success {
            dingtalk(
                robot: 'Jenkins-DingDing',
                type: 'MARKDOWN',
                title: "success: ${JOB_NAME}",
                text: ["- 成功构建:${JOB_NAME}! \n- 版本:latest \n- 持续时间:${currentBuild.durationString}" ]
            )
        }
        failure {
            dingtalk(
                robot: 'Jenkins-DingDing',
                type: 'MARKDOWN',
                title: "success: ${JOB_NAME}",
                text: ["- 构建失败:${JOB_NAME}! \n- 版本:latest \n- 持续时间:${currentBuild.durationString}" ]
            )
        }
    }
}

Modify pipeline.yml, change the mirror version:

apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: test
  name: pipeline
  labels:
    app: pipeline
spec:
  replicas: 2
  selector:
    matchLabels:
      app: pipeline
  template:
    metadata:
      labels:
        app: pipeline    
    spec:
      containers:
      - name: pipeline
        image: 192.168.11.102:80/repo/pipeline:latest   # 这里
        imagePullPolicy: Always
        ports:
        - containerPort: 8080
# 省略其他内容…………

1.3 Rolling updates

Because pipelinewhen there is no change, it will not be reloaded each time, which will cause Podthe container in the medium to not be dynamically updated. kubectlThe rollout restartcommand to be used here is scrolled and updated.

Setting up Jenkinsfle
[External link image transfer failed, the source site may have anti-leech mechanism, it is recommended to save the image and upload it directly (img-6jx4IX1z-1661690703513) (Pictures/1642501521065.png)]
[External link image transfer failed, the source site may have anti-leech mechanism, it is recommended to save the image and upload it directly (img-PNKfVYuI-1661690703514) (Pictures/1642501549176.png)]

Guess you like

Origin blog.csdn.net/zhangxia_/article/details/126574154