Jenkins声明性管道-基于Git分支的动态环境变量

fhg3lkii  于 2022-11-21  发布在  Jenkins
关注(0)|答案(1)|浏览(193)

我有一个多分支Jenkins管道,它工作得很好。现在,我尝试优化它。我有以下步骤:
`

stage('Migration-Dev') {
      environment {
        ENVIRONMENT = 'development'
      }
      when {
        anyOf {
          branch 'development';
          branch 'staging';          
          branch 'production';
         }
      }

      steps {
        echo "Installing the NodeJS dependencies..."
        sh 'npm ci'
        echo "Performing some more tasks"
        sh 'do somethingelse
       }
       }

上面提到的每个git分支都需要它唯一的ENVIRONMENT值来执行一些特定的操作。 是否有可能根据git分支动态创建ENVIRONMENT值名,而不是硬编码它?就像如果分支是staging,那么ENVIRONMENT=release`?
我将非常感激任何帮助。

g6baxovj

g6baxovj1#

当然,请看this answer
例如,我的产品线中有:

environment {
        APIGEE_ORGANIZATION = "companyName"
        }

    stages {

        stage('Setup') {
                steps {
                    echo 'Getting Proxies source from Gitea'

                    script
                        {
                                if (env.BRANCH_NAME == 'qas') {
                                    echo 'Deployment to qas'
                                    env.APIGEE_ENVIRONMENT = 'qas'
                                }
                                if (env.BRANCH_NAME == 'sandbox'){
                                    env.APIGEE_ENVIRONMENT = 'sandbox'
                                    echo 'Deployment to sandbox'
                                }
                                if (env.BRANCH_NAME == 'main'){
                                    env.APIGEE_ENVIRONMENT = 'prod'
                                    echo 'Deployment to prod'
                                }
                         }

相关问题