jenkins学习笔记之十:配置Gitlab提交流水线

一、添加测试Maven项目

1.新建一个gitlab项目

2.导入simple-java-maven-app仓库代码(可以去github或者Gittree上都有) 

3.配置mvn 国内源

参考:https://developer.aliyun.com/article/1039874

二、创建并配置测试流水线

1.新建流水线

2.添加选项参数

3.配置pipeline

三、编写测试jenkinsfile 

1.jenkinsfile

ci.jenkinsfile

#!groovy

@Library('jenkinslibrary@master') _

// shareLibrary 函数
def build = new org.devops.build()
def tools = new org.devops.tools()

// jenkins 配置参数
String srcUrl = "${env.srcUrl}"
String branchName = "${env.branchName}"
String buildType = "${env.buildType}"
String buildShell = "${env.buildShell}"


pipeline {
    agent any

    stages {
        stage("CheckOut"){
            steps{
                script{
                    tools.PrintMes("获取代码","green")
// 具体使用可以查看流水线语法 checkout scmGit(branches: [[name:
"${branchName}"]], extensions: [], userRemoteConfigs: [[credentialsId: 'e7054d0e-275e-48ca-8188-e69da2faffb8', url: "${srcUrl}"]]) } } } stage("Build"){ steps{ script { tools.PrintMes("编译打包","blue") build.Build(buildType,buildShell) } } } } }

2.shareLibrary

src/org/devops/build.groovy

package org.devops

// 构建类型

def Build(buildType,buildShell){
    
    // M3 ANT等值都是配置相关全局工具是定义的名称
    def buildTools = ["mvn":"M3","ant":"ANT","gradle":"GRADLE","npm":"NPM"]
    
    println("当前选择的构建类型为:${buildType}")
    buildHome = tool buildTools[buildType]
    
    sh "${buildHome}/bin/${buildType}  ${buildShell}"
}

src/org/devops/tools.groovy

package  org.devops


//格式化输出
def PrintMes(value,color){
    colors = ['red'   : "\033[40;31m >>>>>>>>>>>${value}<<<<<<<<<<< \033[0m",
              'blue'  : "\033[47;34m ${value} \033[0m",
              'green' : "\033[40;32m >>>>>>>>>>>${value}<<<<<<<<<<< \033[0m" ]
    ansiColor('xterm') {
        println(colors[color])
    }
}

四、验证以上配置

 

五、配置gitlab提交流水线

1.安装通用触发器插件

2.配置触发器

选择触发器

3.为触发器添加相关参数

4.配置gitlab 项目webhook

5. 验证钩子是否生效,确保状态码200,并且查看jenkins是否运行流水线

六、查看Generic Webhook插件打印信息

 

posted @ 2023-03-10 00:10  百衲本  阅读(869)  评论(0编辑  收藏  举报
cnblogs_post_body { color: black; font: 0.875em/1.5em "微软雅黑" , "PTSans" , "Arial" ,sans-serif; font-size: 15px; } cnblogs_post_body h1 { text-align:center; background: #333366; border-radius: 6px 6px 6px 6px; box-shadow: 0 0 0 1px #5F5A4B, 1px 1px 6px 1px rgba(10, 10, 0, 0.5); color: #FFFFFF; font-family: "微软雅黑" , "宋体" , "黑体" ,Arial; font-size: 23px; font-weight: bold; height: 25px; line-height: 25px; margin: 18px 0 !important; padding: 8px 0 5px 5px; text-shadow: 2px 2px 3px #222222; } cnblogs_post_body h2 { text-align:center; background: #006699; border-radius: 6px 6px 6px 6px; box-shadow: 0 0 0 1px #5F5A4B, 1px 1px 6px 1px rgba(10, 10, 0, 0.5); color: #FFFFFF; font-family: "微软雅黑" , "宋体" , "黑体" ,Arial; font-size: 20px; font-weight: bold; height: 25px; line-height: 25px; margin: 18px 0 !important; padding: 8px 0 5px 5px; text-shadow: 2px 2px 3px #222222; } cnblogs_post_body h3 { background: #2B6695; border-radius: 6px 6px 6px 6px; box-shadow: 0 0 0 1px #5F5A4B, 1px 1px 6px 1px rgba(10, 10, 0, 0.5); color: #FFFFFF; font-family: "微软雅黑" , "宋体" , "黑体" ,Arial; font-size: 18px; font-weight: bold; height: 25px; line-height: 25px; margin: 18px 0 !important; padding: 8px 0 5px 5px; text-shadow: 2px 2px 3px #222222; } 回到顶部 博客侧边栏 回到顶部 页首代码 回到顶部 页脚代码