前言

在整个软件开发过程中,大致有如下的几个工作流程阶段:

计划 -> 编码 -> 构建 -> 集成 -> 测试 -> 交付 -> 部署 -> 运营

Jenkins pipeline vs GitLab pipeline_Jenkins

从上面的工作流程阶段可以看到,整个软件开发周期是繁琐而复杂的,需要多部门多人员的参与共同协作来完成开发任务。其中就需要一些可以帮助开发者提升效能的自动化工具,在过去几十年当中,持续集成(Continuous Integration)、持续交付(Continuous Delivery)和 持续部署(Continuous Deployment)方面取得了巨大进展。

Pipeline

Pipeline 翻译过来是管道,在软件工作流程中是指流水线,类似于工厂里的生产线,可以理解成将我们整个软件开发过程的每一步都通过 Pipeline 流水线串联起来,并代码化经过既定工作流,一键就能将本地的代码发布到测试环境中进行测试及其发布到其他多任务的环境中,最终实现持续集成、持续交付、持续部署发布。

如下图所示的管道 Pipeline

Jenkins pipeline vs GitLab pipeline_Jenkins_02


Jenkins pipeline vs GitLab pipeline


Jenkins pipeline vs GitLab pipeline_docker_03

Jenkins pipeline 时序图如下

Jenkins pipeline vs GitLab pipeline_Jenkins_04

Jenkins pipeline 效果如下 

Jenkins pipeline vs GitLab pipeline_git_05

Jenkins pipeline 代码结构部分如下:

// 输出日志用颜色标识
def log(def level, def message) {
    // 定义日志输出颜色
    def ansi_color = ["red": "\033[31m", "green": "\033[32m", "yellow": "\033[33m", "blue": "\033[34m", "res": "\033[0m"]
    if("${level}" == "info") {
        println("${ansi_color.green}********${message}********${ansi_color.res}")
    } else if("${level}" == "warnning") {
        println("${ansi_color.red}********${message}********${ansi_color.res}")
    }
}

// 清理工作空间
def clean_dir() {
    log("info", "start clean workspace")
    deleteDir()
    log("info", "end clean workspace")
    // params.each {
    //     my_params.add(string(name: it.key, value: it.value))
    // }
}

// check 仓库代码
def scprit_check() {
    msg = "echo scprit_check"
    /*sh """
        git clone http://${user_name}:${password_p}@192.203.72.162:18068/root/mvnwebapp.git --depth 1 --branch master mvnwebapp
    """*/
}

// 执行 mvn 编译
def scprit_run() {
    msg = "echo scprit_run"
    /*sh """
        cd ${env.WORKSPACE}/${repo_name} && mvn clean compile package -DskipTests=true
    """*/
}

// sonar 代码扫描质量分析
def scprit_sonar() {
    msg = "echo scprit_sonar"
    /*sh """
        cd ${env.WORKSPACE}/${repo_name} && echo "sonarqube"
    """*/
}

// 构建镜像 push 到 docker 
def scprit_docker() {
    msg = "echo scprit_docker"
    /*sh """
        cd ${env.WORKSPACE}/${repo_name} && echo "docker"
    """*/
}

// 构建镜像 push 到 artifactory
def scprit_artifactory() {
    msg = "echo scprit_artifactory"
    /*sh """
        cd ${env.WORKSPACE}/${repo_name}/out
        jfrog rt upload --user scmadmin --access-token eiSHEkj4RJlji32o215AHek --url http://www.jfrogpro.encdate.cn/artifactory/ mvnwebapp/targer/ encdate-maven-repo/dev/
    """*/
}

// 发布到研发环境进行验证
def scprit_deploy() {
    msg = "echo scprit_deploy"
    /*sh """
        cd ${env.WORKSPACE}/${repo_name} && cp -rf target/*.war scmadmin@192.23.17.105/data/${repo_name}_${BUILDDATE}
    """*/
}

// ****************************************** Start ************************************
pipeline {
    agent {
        node {
            label "master"
            //label "SH_Ubuntu22"
        }
    }
    
    environment {
        BUILDDATE = sh(script: "date +%Y%m%d", returnStdout: true).trim()
        BUILDTIME = sh(script: "date +%Y%m%d%H%M%S", returnStdout: true).trim()
        LC_ALL="C.UTF-8"
        LANG="C.UTF-8"
    }

    parameters {
        string(name: "REPO_NAME", defaultValue: "mvnwebapp", description: "gitlab中的git仓库名称")
        string(name: "BRANCH_NAME", defaultValue: "master", description: "source仓库主干分支")
        string(name: "USER_NAME", defaultValue: "", description: "用户名")
        string(name: "PASSWORD_P", defaultValue: "", description: "用户密码")
    }

    options {
        ansiColor('xterm')
        disableConcurrentBuilds()
        timestamps()
    }

    stages {
        stage("Clean") {
            steps {
                dir("${env.workspace}") {
                    script {
                        clean_dir()
                    }
                }
            }
        }

        stage("CheckOut") {
            steps {
                dir("${env.workspace}") {
                    script {
                        scprit_check()
                        println env.JOB_NAME
                        println env.BUILD_NUMBER
                    }
                }
            }
        }
        stage("Build") {
            steps {
                dir("${env.workspace}") {
                    script {
                        scprit_run()
                    }
                }
            }
        }
        stage("SonarQube") {
            steps {
                dir("${env.workspace}") {
                    script {
                        scprit_sonar()
                    }
                }
            }
        }
        stage("Push Docker") {
            steps {
                dir("${env.workspace}") {
                    script {
                        scprit_docker()
                    }
                }
            }
        }
        stage("Push Artifactory") {
            steps {
                dir("${env.workspace}") {
                    script {
                        scprit_artifactory()
                    }
                }
            }
        }
        stage("Deploy") {
            steps {
                dir("${env.workspace}") {
                    script {
                        scprit_deploy()
                    }
                }
            }
        }
    }
}


GitLab pipeline 时序图如下

Jenkins pipeline vs GitLab pipeline_git_06

GitLab pipeline 效果如下 

Jenkins pipeline vs GitLab pipeline_docker_07

GitLab pipeline 代码结构部分如下:

stages:
- check
- clean
- build

#更新代码
job_check:
stage: check
only:
- master
tags:
- docker
script:
#- cd /home/gitlab-runner
#- git clone git@192.168.19.128:root/mvnwebapp.git
- git clone http://root:scmadmin@192.168.19.128:18068/root/mvnwebapp.git
- git reset --hard HEAD
- git clean -xfd
- git pull origin master
- git fetch
- git log -5 --pretty=oneline

#执行编译构建任务
job_build:
stage: build
only:
- master
tags:
- docker
script:
- echo "--------------------------- Start compilation build task ---------------------------"
- pwd
- mvn clean compile -Dmaven.test.skip=true
- echo "--------------------------- End compilation build task ---------------------------"

job_clean:
stage: clean
tags:
- docker
script:
- echo "--------------------------- Start packaging installation tasks ---------------------------"
- mvn clean install package -Dmaven.test.skip=true
- echo "--------------------------- End packaging installation tasks ---------------------------"

pipeline 运行效果如下

Jenkins pipeline vs GitLab pipeline_git_08


Jenkins pipeline vs GitLab pipeline_docker_09