Based GitLab + Jenkins of enabling DevOps practices

    With micro-services, the rise in Taiwan architecture, DevOps has become very critical, after all, is to build some of the infrastructure level, if you do a good job behind the great efficiency of R & D work will be improved. About the concept of DevOps itself, the Internet has been very much in the garden are a bunch of random search of some concepts, I will not repeat described. The following directly to the question, how to use GitLab + Jenkins to complete the construction of DevOps.

    Before you start practical exercises, first with a diagram to show how the practice functions to be accomplished:

    

    In this process, the environment is divided into three, namely, the preview environment, advance and production environments, the average developer after receiving the task, developed in GitLab based feature branch, and then merge the application needs to develop a good dev branch, in the process of applying for the merger, the trigger will be compiled to build the pipeline, unit testing, interface testing, calibration and other publishing environment, when the pipeline is completed, the group leader can review the code in the process of merging into the dev branch. This time will trigger dev build the pipeline branch, and then again to complete the above process, the release code to advance environment. Finally, the project leader regularly to dev merged into the master branch, complete production release.

    First, create a test project in GitLab in:

    This project is a project applications in the sub-group group lizongshenblogs the following, which is representative of a source code program.

Next, and then create three pipeline configuration for this project, the main purpose is to enable the separation of code and configuration:

     In the configuration item 3, it was stored corresponding Jenkinsfile, Jenkins for constructing pipeline configuration, the next start configuration Jenkins. Jenkins first performed a global configuration: In the Manage Jenkins Jenkins - the Configure System Configuration The following Gitlab Connection, as shown:

 

It should be noted here that this connection is a need gitlab access token may be provided on personal gitlab - the access token which is generated after the generation is complete, filled into appropriate Credentials inside:

 

 The last test, the connection is successful, as long as the show success, on it.

 

 Then you can configure specific pipeline, and the first to use Jenkins' New Item to create three types of pipeline projects are:

 

 

    In Jenkins the new type of three pipeline projects, are called feature-pipeline, dev-pipeline, master-pipeline, then these three projects are configured separately, first look at the feature-pipeline configuration:

    First item to configure is the Build Triggers, in which the check Opened Merge Request Events, and the Rebuild open Merge Requests set to On push to source branch:

 

 

 Then click on the Advanced button for advanced settings:

 

It should be noted that where the original branch of any branch target branch is dev branch, and then generate a Secret token, this token in the configuration gitlan webhooks time will be used.

The next item is to configure the Pipeline:

 

 

     This place needs to configure specific pipeline storage address in local credentials, the account password to log in to use gitlab can.

    dev lines and master pipeline configuration is slightly different, which need to be configured to dev branch accepted merge request events, it means that when the head of the request to accept the merger triggers:

 

 

     Where the master branch needs to be changed are matched branch from branch only acceptance dev merge request to the master branch:

 

 

     Here Jenkins configuration has been configured, then go back to gitlab linkage configuration, first configure webhoos project, add a webhooks in Integrations Settings items inside:

 

 

    Where the URL will fill Jenkins' Build Triggers program automatically generated the URL, secret token in the Build Triggers advanced options which generated the token, triggering the option to select Merge request events, indicate when the merger request trigger, click Save, gitlab Jenkins and arranged substantially completed.

    Finally, a look at three projects Jenkinsfile:

pipeline {
    agent {
        label 'slave-pipeline'
    }
    options {
        gitLabConnection('gitlab')
    }
    stages {
        stage('Prepare'){
            steps{
                script{
                        git branch: '${gitlabSourceBranch}', changelog: false, credentialsId: 'gitlab', poll: false, url: 'https://gitlab.com/lizongshenblogs/applications/devopsdemo.git'
                                
                        script {
                            GIT_COMMIT_ID = sh(returnStdout: true, script: 'git rev-parse --short HEAD').trim()
                            IMAGE_TAG = "${GIT_COMMIT_ID}-snapshot"
                            INGRESS_HOST = "preview-${gitlabSourceRepoName}-${gitlabMergeRequestIid}"
                            MR_URL = "${gitlabSourceRepoHomepage}/merge_requests/${gitlabMergeRequestIid}"
    
                            build_tag = "feature-${gitlabSourceRepoName}-${gitlabSourceBranch}-${GIT_COMMIT_ID}"
                            currentBuild.displayName = "#" + BUILD_NUMBER + "_" + gitlabMergeRequestIid + "_" + build_tag
                        }
                }
            }
        
        }
    
        stage('Build') {
        
            when {
                expression {
                    "${gitlabTargetBranch}" == 'dev'
                }
            }
            
            steps {
                script {
                    updateGitlabCommitStatus name: 'Build', state: 'running'
    
                    {the try 
                    
                        echo "start compiling ..." 
                        
                    } the catch (Exception EX) { 
                        updateGitlabCommitStatus name: 'the Build', State: 'failed' 
                        the throw EX; 
                    } the finally { 
    
                    } 
    
                    updateGitlabCommitStatus name: 'the Build', State: 'Success' 
    
                } 
            } 
        } 
        Stage ( 'the test') { 
            Steps { 
                Script { 
                    updateGitlabCommitStatus name: 'the test', State: 'running' 
    
                    try {
                        echo "unit testing started ..."
                    } catch(Exception ex){ 
                        updateGitlabCommitStatus name: 'the Test', State: 'failed' 
                        the throw EX; 
                    } the finally { 
    
                    } 
                    
                    updateGitlabCommitStatus name: 'the Test', State: 'Success' 
                     
                } 
            } 
        } 
        Stage ( 'the Deploy') { 
            Steps { 
                Script { 
                    updateGitlabCommitStatus name: 'the Deploy', State: 'running' 
    
                    the try { 
                        echo "begin publishing ..." 

                    } the catch (Exception EX) {
                        updateGitlabCommitStatus name: 'Deploy', state: 'failed'
                        throw ex;
                    } finally {
    
                    }
                    
                    updateGitlabCommitStatus name: 'Deploy', state: 'success'
                    addGitLabMRComment comment: "App ${gitlabSourceRepoName} preview link: <a href='http://localhost'>${gitlabSourceRepoName}</a>"
                }
            }
        }
    }
    
    post {
        failure {
            updateGitlabCommitStatus name: 'Complete', state: 'failed'
        }
        success {
            updateGitlabCommitStatus name: 'Complete', state: 'success'
        }
    }
}

    UpdateGitlabCommitStatus which you can build real-time status of the Jenkins sent back gitlab go:

 

    Click on build status, can be viewed in real time to build log. Here gitlab Jenkins and configuration basically completed, the next look at what gitlab concerning code management configuration, under normal circumstances, dev branch and are not allowed to directly push the master branch code, allowing only the combined demand from the branch Code, which requires gitlab the Settings - Repository - Protected Branches in the dev and master protected:

 

     In addition you can also set the Only when the pipeline is successful, it can be combined:

 

    By some protective measures, you can let the dev branch and master become relatively stable. Due to limited space, plus Jenkinsfile contains too many custom sensitive content, only some of the cut, if there do not understand, you can find me in private exchanges.

    Written in the last: DevOps is a very broad topic, talked about today GitLab + Jenkins DevOps in this process is only part of the full realization of DevOps process also requires more cooperation, have the opportunity to share for everyone.

 

Guess you like

Origin www.cnblogs.com/lizongshen/p/11493945.html