本文流水线例子,使用pipeline 参数化构建 springboot 应用, 项目是多个目录, 目录结构有两种, 构建完jar包以后,使用docker build 打包镜像进行push, 完成以后使用企业微信进行通知(也可以是其他webhook通知)


def qyWechatUrl ="https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxxxxxxxxxxx"

def buildAndPush = {  folder ->
    def dockerUrl = "${params.ImageUrl}:$folder-${params.RELASE}-${env.BUILD_NUMBER}"
    def fileDir = "${WORKSPACE}@2/$folder"
    def bootDir = "${WORKSPACE}@2/$folder/$folder-boot"
    if (fileExists("$bootDir")) {
        dir ("$bootDir") {
            script {
                echo "${params.BRANCH} ${params.RELASE} ${params.ENVIRONMENT} "
                sh 'pwd'
                sh 'ls -lsa'
                sh "docker build -t ${dockerUrl} . && docker push ${dockerUrl}"
                sh "/var/lib/jenkins/script-file/send-message.sh \"# ${JOB_NAME}\n构建名:  ${env.STAGE_NAME} --> $folder\n分支: ${params.BRANCH}\n版本: ${params.RELASE}\n构建次数: ${BUILD_NUMBER}\n镜像地址: **${dockerUrl}**\nTag: $folder-${params.RELASE}-${BUILD_NUMBER}\n[详情](${BUILD_URL})\" "
            }
        } 
    } else if(fileExists("$fileDir")) {
        dir ("$fileDir") {
            script {
                echo "${params.BRANCH} ${params.RELASE} ${params.ENVIRONMENT} "
                sh 'pwd'
                sh 'ls -lsa'
                sh "docker build -t ${dockerUrl} . && docker push ${dockerUrl}"
                sh "/var/lib/jenkins/script-file/send-message.sh \"# ${JOB_NAME}\n构建名:  ${env.STAGE_NAME} --> $folder\n分支: ${params.BRANCH}\n版本: ${params.RELASE}\n构建次数: ${BUILD_NUMBER}\n镜像地址: **${dockerUrl}**\nTag: $folder-${params.RELASE}-${BUILD_NUMBER}\n[详情](${BUILD_URL})\" "
            }
        }
    } else {
        echo "$folder 文件夹不存在"
    }
}

def deploy = {  env ->
    echo "部署 $env"
}

pipeline {
    agent any

    parameters {
        choice(
            choices: ['gateway', 'background', 'management', 'auth', 'business-user', 'data-interaction', 'inter-message', 'modularize-e-business', 'order', 'payment', 'shop-product', 'user'], 
            description: '选择部署的 jar包', 
            name: 'ENVIRONMENT'
        )
        choice(
            choices: ['prod', 'test'],
            description: '选择版本',
            name: 'RELASE'
        )
        choice(
            choices: ['main','aliyun'],
            description: '选择分支',
            name: 'BRANCH'
        )
        choice(
            choices: ['registry.cn-chengdu.aliyuncs.com/仓库名/项目名','registry-vpc.cn-chengdu.aliyuncs.com/仓库名/项目名'],
            description: '选择镜像仓库地址',
            name: 'ImageUrl'
        )
        choice(
            choices: ['https://gitlab.xxx.xxx/xxxx/xxx.git','https://gitlab.xxx.xxx/xxxx/xxx.git'],
            description: '选择代码GitLab地址',
            name: 'GitLabUrl'
        )
        choice(
            choices: ['用户名/项目名:标签名','用户名/项目名:标签名'],
            description: '选择打包镜像地址',
            name: 'BuildImage'
        )

    }
    stages {
        stage('Maven 打包项目') {
            agent{
                docker { 
                    image "${params.BuildImage}"
                }
            }
            steps {
                git branch: "${params.BRANCH}", credentialsId: 'gitlab-root', url: "${params.GitLabUrl}"
                sh 'ls -lsa'
                sh 'pwd'
                sh 'mvn -v'
                sh "mvn clean package -Dmaven.test.skip=true -P${params.RELASE}" 
            }
        }

        stage('构建镜像') {
            steps {
                script {
                    echo "${params.BRANCH} ${params.RELASE} ${params.ENVIRONMENT} "
                    def environment = ENVIRONMENT
                    if (environment) {
                        buildAndPush(environment)

                    } else {
                        error("无效的环境选项")
                    }
                }
            }
        }

        stage('部署') {
            steps {
                script {

                    def environment = ENVIRONMENT
                    if (environment) {
                        deploy(environment)
                    } else {
                        error("无效的环境选项")
                    }
                }
            }
        }

    }
    post {
        always {
            qyWechatNotification mentionedId: '', mentionedMobile: '', moreInfo: '', webhookUrl: "$qyWechatUrl"
        }
    }
}

添加gitlba认证 file file 构建

  1. 选择对应项目进行构建 file
  2. blue ocean 构建, 点击运行,然后选择对应的项目 file

运行结果, blueocean查看 file