【问题标题】:How to pass parameters in a stage call in Jenkinsfile如何在 Jenkinsfile 的阶段调用中传递参数
【发布时间】:2021-08-01 21:22:58
【问题描述】:

其实我的 Jenkinsfile 是这样的:

@Library('my-libs') _
myPipeline{
                my_build_stage(project: 'projectvalue', tag: '1.0' )
                my_deploy_stage()
                
                
}

我正在尝试将这两个变量(项目和标签)传递给我的 build_stage.groovy,但它不起作用。

在 my_build_stage.groovy 中使用 $params.project 或 $params.tag 的正确语法是什么?

【问题讨论】:

    标签: jenkins groovy jenkins-pipeline pipeline stage


    【解决方案1】:

    请看下面将传递参数的代码。 在您的 Jenkinsfile 中写入以下代码:

    // Global variable is used to get data from groovy file(shared library file)
    def mylibrary
    def PROJECT_VALUE= "projectvalue"
    def TAG = 1
    pipeline{
    agent{} 
    stages{
           stage('Build') {   
                steps {
                  script {
                            // Load Shared library Groovy file mylibs.Give your path of mylibs file which will contain all your function definitions
                            mylibrary= load 'C:\\Jenkins\\mylibs'
                            // Call function my_build stage and pass parameters
                            mylibrary.my_build_stage(PROJECT_VALUE, TAG )   
                          }               
                      }
            }
            
            stage('Deploy') {   
                steps {
                  script {
                            // Call function my_deploy_stage
                            mylibrary.my_deploy_stage()   
                          }               
                      }
            }
       }
    }
    
     
    

    创建一个名为:mylibs(groovy file)的文件

    #!groovy
    // Write or add Functions(definations of stages) which will be called from your jenkins file
    def my_build_stage(PROJECT_VALUE,TAG_VALUE)
     {
        echo "${PROJECT_VALUE} : ${TAG_VALUE}"
     }
    
    def my_deploy_stage()
     {
        echo "In deploy stage"
     }
    return this
    

    【讨论】:

    • @codecex 我提供的解决方案对你有用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-17
    • 1970-01-01
    相关资源
    最近更新 更多