【问题标题】:Reduce Stages in Jenkinsfile when using Multibranch Pipeline使用多分支管道时减少 Jenkinsfile 中的阶段
【发布时间】:2021-07-30 20:52:45
【问题描述】:

在这个 Jenkinsfile 中,我试图为 Multibranch Pipeline 中的不同分支执行相同的阶段。我每次都需要配置每个分支名称的环境变量。有没有更好的方法来做到这一点?

        stage('Create New AMI for master branch') {
            when { branch 'master' }
            environment {
                BRANCH_NAME = "${env.BRANCH_NAME}"
                ENV_NAME = "prod"
            }
            steps {
                sh "packer build jenkins/${PROJECT_NAME}/${PROJECT_NAME}-ami.json"
            }
        }
        stage('Create New AMI for development branch') {
            when { branch 'development' }
            environment {
                BRANCH_NAME = "${env.BRANCH_NAME}"
                ENV_NAME = "dev"
            }
            steps {
                sh "packer build jenkins/${PROJECT_NAME}/${PROJECT_NAME}-ami.json"
            }
        }
        stage('Create New AMI for staging branch') {
            when { branch 'staging' }
            environment {
                BRANCH_NAME = "${env.BRANCH_NAME}"
                ENV_NAME = "staging"
            }
            steps {
                sh "packer build jenkins/${PROJECT_NAME}/${PROJECT_NAME}-ami.json"
            }
        }

【问题讨论】:

    标签: jenkins continuous-integration jenkins-pipeline jenkins-plugins jenkins-job-dsl


    【解决方案1】:

    在这种情况下,请使用共享库,它将包含您所有的阶段实现。参考示例实现,如下所示。
    创建一个名为:sharedlibraries(groovy 文件)的文件

    #!groovy
    // Write or add Functions(definations of stages) which will be called from your jenkins file
    def Create_AMI(PROJECT_NAME, ENV_NAME)
     {
         echo ENV_NAME
         sh "packer build jenkins/${PROJECT_NAME}/${PROJECT_NAME}-ami.json"
         // You can also set the environment below example:
         env.ENV_NAME ="dev"
     }
    
    return this
    

    在您的 Jenkinsfile 中写入以下代码:

    // Global variable is used to get data from groovy file(shared library)
    def stagelibrary
    stage('Create New AMI') {   
                steps {
                  script {
                            // Load Shared library Groovy file stagelibraries.Give your path of stagelibraries file whic is created
                            stagelibrary = load 'C:\\Jenkins\\stagelibraries'
                            // Execute Create_AMI function. You can add if else conditions over here, based on your branches. But am not sure of your conditions.
                            // You can also pass your environment variable 
                            // in the Crete_AMI function using env.<YOURENVVARIABLE>
                            stagelibrary.Create_AMI(PROJECT_NAME,env.ENV_NAME)       
                          }               
                      }
            }
    
    

    上面的例子是为了提供共享库的概述,您不需要编写相同的函数/或冗余阶段。

    【讨论】:

    • 嘿 @Altaf 感谢您提供新的视角,但我也想要 ENV_NAME 那么我该如何使用它?这是否也会将所有全局环境变量传递给打包器步骤?
    • @Hiren,是的,即使在打包程序步骤中,您也可以以相同的方式使用环境变量。示例:jobName= env.JOB_NAME
    • @Hiren 为您提供我提供的解决方案
    • 嗨@altaf 我仍然不清楚如何在您的示例中使用共享库来使用 ENV_NAME = "dev" / "Stage" / "prod"。
    • @Hiren 我用env.&lt;YOURENVVARIABLE&gt; 更新了如何使用和设置环境变量的答案,例如:env.ENV_NAME 。要么设置它,要么使用它
    猜你喜欢
    • 2022-01-26
    • 2020-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-28
    • 1970-01-01
    • 2020-08-20
    • 1970-01-01
    相关资源
    最近更新 更多