Privately deploy gitlab, webhooks code submission and merged automatically trigger kubesphere's developops pipeline

1. Create the develops pipeline of kubesphere

You must use "Use Jenkinsfile to create a pipeline", see the official documentation: Use Jenkinsfile to create a pipeline

In other words, the Jenkinsfile file must be in the git warehouse. We usually put the Jenkinsfile file in the same git warehouse as the source code. We are accustomed to placing it in the first-level directory of the git warehouse.

1. Create a pipeline, click "Code Warehouse" --- "git", we built a gitlab private warehouse on the intranet, so select "git" here, fill in the git address and credentials, click the check mark, "Next".

 

2. Set the Jenkinsfile path and view the WebHook address, as shown below

 Remember the WebHook address here.

"Regular filtering" can filter branches. For example, if we only create the develop branch, fill in "develop" in the regular form; note that if there are multiple branches, it is best to create 2 pipelines, otherwise multiple branches will be triggered at the same time. (If there is a better way to add)

 This concludes the pipeline configuration of kubesphere. Jenkinsfile script code content:

pipeline {
  agent {
    node {
      label 'base'
    }

  }
  stages {
    stage('拉取代码') {
      agent none
      steps {
        container('base') { 
         // echo "--printenv --" 
         // sh 'printenv'


          checkout(scm)

          script {
             
            gitbranch=env.GIT_BRANCH

            if(env.GIT_BRANCH=="develop"){
               env_name="test"
               env_name_big="Test"
              }
            else if(env.GIT_BRANCH=="master"){ 
               env_name="prod"
               env_name_big="Production" 
            }
            
            
          }
        //  echo "env_name=${env_name}"
        //  echo "env_name_big=${env_name_big}"
        //  echo "gitbranch=${gitbranch}"
        //  echo "GIT_COMMIT=${GIT_COMMIT}" 
         
          sh '''pwd
                ls'''
        }

      }
    }

    stage('构建镜像') {
      agent none
      steps {
        container('base') {

         // echo "env_name=${env_name}"
         // echo "env_name_big=${env_name_big}"
         //  echo "gitbranch=${gitbranch}"
         //  echo "GIT_COMMIT=${GIT_COMMIT}" 

           script { 
             docker_image_url="${REGISTRY}/${DOCKERHUB_NAMESPACE}/${APP_NAME}:${env_name}${timestr}_${BUILD_NUMBER}"
           }
           // echo "docker_image_url=${docker_image_url}"
           //sh "echo ${docker_image_url}"
          
          sh 'mv ${web_dictionary_name}/Dockerfile .'
          sh  " docker build --build-arg ASPNETCORE_ENVIRONMENT=${env_name_big} -t    ${docker_image_url}   .  "
          echo '镜像打tag完成了'

        }

      }
    }

    stage('推送镜像到Harbor仓库') {
      agent none
      steps {
        container('base') {

         // echo "env_name=${env_name}"
         // echo "env_name_big=${env_name_big}"
        //  echo "gitbranch=${gitbranch}"
        //  echo "GIT_COMMIT=${GIT_COMMIT}" 
        //  echo "docker_image_url=${docker_image_url}"

          withCredentials([usernamePassword(credentialsId : 'harbor-auth' ,passwordVariable : 'password_var' ,usernameVariable : 'username_var' ,)]) {
            //echo '本地镜像上传到harbor---start'

            sh " docker login -u ${username_var}  -p ${password_var} ${REGISTRY}"
            sh  " docker push $docker_image_url "
            //echo '本地镜像上传到harbor-ok'
          }

        }

      }
    }

    stage('部署') {
      agent none
       environment {
          env_name_big = "${env_name_big}"
          env_name = "${env_name}"
        }
      steps {
        container('base') {
             
          echo "env_name=${env_name}"
          echo "env_name_big=${env_name_big}"
          echo "gitbranch=${gitbranch}"
          echo "GIT_COMMIT=${GIT_COMMIT}" 
          echo "docker_image_url=${docker_image_url}"

          withCredentials([kubeconfigFile(credentialsId: env.KUBECONFIG_CREDENTIAL_ID, variable: 'KUBECONFIG')]) {
             sh 'envsubst    < ${web_dictionary_name}/deploy/svc.yml | kubectl apply -f -'   
          }
          
          script {

            if(env_name=="prod")
            {              
               // echo "http请求更改版本号"  
               // echo docker_image_url
                sh """ curl -X 'POST'  '${version_update_posturi}' -H 'accept: text/plain'  -H 'Content-Type: application/json-patch+json' -H 'request-from: swagger' -d '{ "appCode": "${APP_NAME}",  "appVersionInfo": "${GIT_COMMIT}",  "dockerImageUrl": "${docker_image_url}" }'"""

                 echo "http请求更改版本号成功!"  
              }
          }

          echo "自动构建成功!"  
        }

      }
    }

  }
  
  environment {
    APP_NAME = 'test-api'
    web_dictionary_name = 'Test.WebAPI' 

    version_update_posturi='http://10.120.34.5/api/version/BuildNum'
    DOCKER_CREDENTIAL_ID = 'dockerhub-id'
    GITHUB_CREDENTIAL_ID = 'github-id'
    KUBECONFIG_CREDENTIAL_ID = 'kubeconfig'
    REGISTRY = '150.10.1.26'
    DOCKERHUB_NAMESPACE = 'default'
    GITHUB_ACCOUNT = 'kubesphere'
    timestr=new Date().format("yyyyMMdd") 
    gitbranch=''
    env_name=''
    docker_image_url=''
  } 
}

There is another pitfall. Our kubesphere version is 3.2.1. I don’t know about higher versions. We deployed 2 clusters, a kubephere main cluster and a member cluster (all webapi developed sites are deployed in this cluster). The webhooks address displayed on the pipeline creation page shows the main cluster address, which is actually wrong. It should be the master address of the member cluster. The address of all gitlab webhooks must be the address of the member cluster master. Of course, if it is "All-In-One" mode, there will be no such problem.

2. Webhooks configuration of gitlab

Open the git address you just specified, "Settings"--"Webhooks", and fill in the URL with the webhooks address you just saw in kubesphere.

Remember, only check "Push events" here and fill in the "develop" branch name.

Uncheck "Merge request events", Uncheck "Merge request events", Uncheck "Merge request events". Because when the Merge code requests consent, it pushes the Push event, so you only need to check the first one.

In order to support multiple branches, create multiple webhooks in gitlab.

3. Jenkinsfile script writing

You need to obtain some git parameters, such as branch name, git commit id and other data. You can print the environment variables first. If there are relevant values ​​in them, you can use them directly. 

sh 'printenv'

You can find that there are several environment variables that can be used directly.

env.BRANCH_NAME   //分支名
env.GIT_BRANCH    //分支名
env.GIT_COMMIT   //git commit id

Guess you like

Origin blog.csdn.net/puzi0315/article/details/126996831