• Jenkins Pipeline 方法(函数)定义及调用



    网上查了一些文档资料,Jenkins Pipeline 方法调用 的文章还是比较少的,就算有也描述得不太清楚。于是根据自己的经验总结了如何在 Jenkinsfile 进行方法调用。如有不当之处,请予指正。

    首先来说一下为什么要使用方法?有这样一个场景,我有十几台远端服务器,这些服务器都是我的测试(或线上)服务器,我构建后的包需要部署到这些服务器上,于是我会为每台远程服务器定义相关登录信息(用户名、密码或密钥)等。这些配置对于每台服务器来说其实都是一样,无非就是 IP 不同而已(用户名、密钥或密码我暂且保持一致),于是我就可以将相同的部分定义为一个方法或叫函数,并在我的流水线阶段来调用这些方法或函数,当你定义的函数过多时,你还可以将这些函数写道共享库中,然后流水线调用共享库即可。

    本案例以声明式 Jenkinsfile 为例进行演示!

    1、定义方法(函数)

    方法定义于 pipeline {} 之外,并在 pipeline {} 之内进行调用。如下,我定义了一个远程服务器的远程登录信息,方便我能把构建的包发布到远程服务器上。

    // 创建函数
    def RemoteHost(Jarnname, Host, ProJect) {
        withCredentials([usernamePassword(credentialsId: '131', passwordVariable: 'PassWord', usernameVariable: 'UserName')]){
            def remote = [:]
            remote.name = "$Jarnname"
            remote.host = "$Host"
            remote.user = "$UserName"
            remote.password = "$PassWord"
            remote.port = 22
            remote.allowAnyHosts = true
            sshPut remote: remote, from: "./target/$Jarnname", into: "./${ProJect}/", override: true
        }
    }
    
    // 流水线步骤
    pipeline {
    	stages {
    		// 阶段 + 步骤
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    2、调用方法(函数)

    看下面代码块的项目发布阶段,在这里调用了上面定义的函数,并进行参数传递。

    // 创建函数
    def RemoteHost(Jarnname, Host, ProJect) {
        withCredentials([usernamePassword(credentialsId: '131', passwordVariable: 'PassWord', usernameVariable: 'UserName')]){
            def remote = [:]
            remote.name = "$Jarnname"
            remote.host = "$Host"
            remote.user = "$UserName"
            remote.password = "$PassWord"
            remote.port = 22
            remote.allowAnyHosts = true
            sshPut remote: remote, from: "./target/$Jarnname", into: "./${ProJect}/", override: true
        }
    }
    
    // 流水线步骤
    pipeline {
        agent {
            docker {
                image 'maven:v2'
                args '-v /var/lib/jenkins/workspace/$JOB_NAME:/root/.m2 -v /root/mavenRepository:/usr/repository'
            }
        }
        ...
        ...
    	stages {
    	    // 拉取代码
            stage('拉取代码') {
                steps {
                    checkout([$class: 'GitSCM',
                        branches: [[name: "${params.BRANCH_TAG}"]],
                        doGenerateSubmoduleConfigurations: false,
                        extensions: [],
                        gitTool: 'Default',
                        submoduleCfg: [],
                        userRemoteConfigs: [[url: 'https://codeup.aliyun.com/xx/java_code.git',credentialsId: 'yourid',]]
                    ])
            }
            // 项目构建
            stage("构建项目") {
                steps {
                    sh "mvn clean package -Dmaven.test.skip=true -P test"
                }
            }
            // 项目发布(在这里调用方法)
            stage ("项目发布") {
                steps {
                    script {
                        switch("$p_name"){
                            case "cms":
                                Jarnname = "demo.jar"
                                Host = "192.168.56.131"
                                ProJect = "cms"
                                // 调用方法并传递参数
                                RemoteHost("${Jarnname}", "${Host}", "${ProJect}")
                            break
                        }
                    }
                }
            }
            ...
            ...
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63

    这样的话,我们就不用每次都写一遍定义的方法内容了,只需调用其函数名即可。

    注意:上面只是我 Jenkinsfile 的一部分!

    至此,Jenkins Pipeline 方法定义及调用已经完成。

  • 相关阅读:
    字节前端实习的两道算法题,看看强度如何
    Linux压缩、解压缩以及打包命令
    全面揭秘!微信传输助手的用处有哪些!
    C++项目:高并发内存池
    【各种**问题系列】MVC、MVP、MVVM 、MVI、VIPER 架构(设计模式)
    虚假新闻检测——Exploring the Role of Large Language Models in Fake News Detection
    mysql 中with的用法(3)
    英语翻译器-免费英语自动批量翻译器
    会stm32有机会进大公司吗?
    【性能测试】使用JMeter对code论坛进行压力测试
  • 原文地址:https://blog.csdn.net/IT_ZRS/article/details/125601643