简体   繁体   English

使用Jenkins管道中的选项从嵌套映射中定义环境变量

[英]Use choices in Jenkins pipeline to define env vars from a nested map

What I am trying to achieve is: the user will select an environment and then I can set appropriate env vars for that environment (URL, DB, etc.). 我要实现的目标是:用户将选择一个环境,然后我可以为该环境设置适当的环境变量(URL,数据库等)。 Is it possible? 可能吗? I tried a lot of things: with interpolation in environment, trying to define environments map in different places but no luck. 我尝试了很多事情:在环境中进行插值,尝试在不同位置定义环境地图,但是没有运气。 My environments map variable is not accesible in environment section and also there seems to be some limitations to what can be done inside environment section, I got messages like: you can concatenate only with +, env var can only be value or function call. 我的环境map变量在环境部分中不可访问,并且在环境部分中可以执行的操作似乎存在一些限制,我得到的消息是:您只能用+串联,env var只能是值或函数调用。 I tried some variations with those hints but still no luck. 我根据这些提示尝试了一些变体,但还是没有运气。

def environments = [
  TEST: [APP_URL: 'http://test'],
  DEV: [ APP_URL: 'https://dev'],
  QA: [ APP_URL: 'https://qa']
]

pipeline {
  agent any
  parameters {
    choice(name: 'environment', choices: "${environments.keySet().join('\n')}")
  }
  stages {
    stage ('Test') {
      environment {
        APP_URL = environments[params.environment]['APP_URL']
      }
      steps {
        sh 'env'
      }
    }
  }
}

This works :) 这有效:)

def environments = [
  TEST: [APP_URL: 'http://test'],
  DEV: [ APP_URL: 'https://dev'],
  QA: [ APP_URL: 'https://qa']
]

pipeline {
  agent any
  parameters {
    choice(name: 'environment', choices: "${environments.keySet().join('\n')}")
  }
  stages {
    stage ('Test') {
      steps {
        sh """
            export APP_URL=${environments[params.environment]['APP_URL']}
            env
        """
      }
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM