Abstract: This paper mainly introduces automatic CICD based on Jenkins + GitLab + Webhook. Jenkinsfile is recommended to do pipeline configuration.

First, plug-in installation

  • Generic Webhook Trigger(This method is recommended. This plug-in is used for configuration in the following sections.)
  • GitLab Hook Plugin
  • GitLab Plugin
  • Authentication Tokens API Plugin
  • description setter(This plug-in is used to describe each build.)

2. Basic project configuration

The basic code for Jenkinsfile can be generated using a code generator, which you can then fine-tune yourself, as shown below

Create a new pipeline project and open the configuration project

Then copy the contents of jenkinsFile as follows

pipeline {
  agent any
  triggers {
    GenericTrigger(
     genericVariables: [
        [key: 'branch', value: '$.ref'],
       [key: 'git_http_url', value: '$.repository.git_http_url'], 
       [key: 'commit_message', value: '$.commits[0].message'],
       [key: 'commit_author_name', value: '$.commits[0].author.name'],
       [key: 'commit_author_email', value: '$.commits[0].author.email']
     ],
     causeString: 'Triggered on $ref',
     token: '123456',
     printContributedVariables: true,
     printPostContent: true,
     silentResponse: false
    )
  }
  stages {
    stage('Init') {
      steps {
          script {
              branchName = branch.substring("refs/heads/".length())
          }
      }
    }
    stage('Build') {
      steps {
          script {
              sh "echo Build"
          }
      }
    }
    stage('Deploy') {
      steps {
          script {
              sh "echo Deploy"
          }
      }
    }
  }
}

Copy the code

The specific steps in the middle are based on the project type. The purpose of using GenericTrigger is to take the parameters we want from gitLab and put them into the environment variable, which can be used later

If we don’t want to build every time we commit, we can specify the regular match to build and modify GenericTrigger slightly to add these two parameters. I will only build if Jenkins is included in the message I commit

regexpFilterExpression: '^.*jenkins.*? $', regexpFilterText: '$.commit_message'Copy the code

Gitlab configuration

You can configure the above URL and push the test after configuration

SpringBoot project test environmentJenkinsfileconfiguration

pipeline { agent any environment { APP_CODE = '' APP_VERSION = '' } parameters { string(name: 'APP_CODE', defaultValue: ", description: 'APP encoding ') string(name: 'APP_VERSION', defaultValue: ', description: 'APP version ')} triggers {GenericTrigger(genericVariables: [[key: 'branch', value: '$.ref'], [key: 'git_http_URL ', value: '$.repository.git_http_url'], [key: 'git_ssh_url', value: '$.repository.git_ssh_url'], [key: 'commit_message', value: '$.commits[0].message'], [key: 'commit_author_name', value: '$.commits[0].author.name'], [key: 'commit_author_email', value: '$.commits[0].author.email'] ], causeString: 'Triggered on $ref', token: '123456', printContributedVariables: true, printPostContent: true, silentResponse: false ) } post { always { cleanWs() } } stages { stage('Init') { steps { script { sh "echo Init" branchName = branch.substring("refs/heads/".length()) } } } stage('Build') { steps { script { sh "echo Build" checkout( [ $class: 'GitSCM', branches: [[name: branchName] ], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '', url: '${git_sSH_URL}']]]) sh "/maven/bin/ MVN clean package - dmaven.test. skip=true" // docker package}}} stage('Deploy') {steps {script {sh "echo Deploy" // You can use SSH upload or docker or K8S to Deploy. See Intranet requirements}}}}}Copy the code