如何在詹金斯的AWS帐户之间切换?

问题描述 投票:1回答:1

我正在使用activeChoiseParam插件创建Jenkins作业。我的问题是如何根据选择(开发,预生产)在aws帐户之间动态切换,以及如何从所选帐户获取kubernetes命名空间。我尝试在脚本参数中使用withAWS,但不支持。我的参数:

properties([
  parameters([
    choice(name: "CLUSTER_NAME", choices: ["development", "pre-prod"], description: "Cluster for deploying"),
    [$class: 'CascadeChoiceParameter',
     choiceType: 'PT_SINGLE_SELECT',
     description: 'Select the Server from the Dropdown List',
     filterLength: 1,
     filterable: true,
     name: 'Server',
     randomName: 'choice-parameter-5631314456178619',
     referencedParameters: 'CLUSTER_NAME',
     script: [
       $class: 'GroovyScript',
       fallbackScript: [
         classpath: [],
         sandbox: false,
         script:
           'return[\'Could not get Environment from Env Param\']'
       ],
       script: [
         classpath: [],
         sandbox: false,
         script:
           ''' if (CLUSTER_NAME.equals("development")){
                                def getNamespaces = "kubectl get namespaces -o jsonpath={.items[*].metadata.name}"
                                def devNamespaces = getNamespaces.execute().in.text.split().toList()
                                return devNamespaces
                            }
                            else if(CLUSTER_NAME.equals("pre-prod")){
                                def getNamespaces = "kubectl get namespaces -o jsonpath={.items[*].metadata.name}"
                                def preProdNamespaces =getNamespaces.execute().in.text.split().toList()
                                return preProdNamespaces
                                }
                        '''
       ]
     ]
    ]
  ])
])
jenkins groovy jenkins-pipeline jenkins-plugins
1个回答
0
投票

使用'CloudBees AWS Credentials'插件在jenkins中声明您的凭据

然后在管道中使用'withCredentials'指代先前创建的aws凭据。

ex:

    stage("xxxxxxx") {

    withCredentials([[$class: 'AmazonWebServicesCredentialsBinding',
        accessKeyVariable: 'aKV',
        secretKeyVariable: 'sKV',
        credentialsId: 'id_of_your_credential',
    ]]) {
            sh '''
            AWS_ACCESS_KEY_ID=${aKV}\
            AWS_SECRET_ACCESS_KEY=${sKV}\
            AWS_DEFAULT_REGION=us-east-1\
            aws ec2 describe-images        
            '''
    }
}
  • [AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY ...是aws-cli的shell环境变量。

我不确定您到底需要什么,但您明白了。

© www.soinside.com 2019 - 2024. All rights reserved.