Ivory Micky
Ivory Micky

Reputation: 581

How to switch aws profile within a groovy script in Jenkins active choice reactive parameter

I am trying to switch to the aws prod profile (defined in ~/.aws/credentials as jprod) inside a Jenkins active choice reactive parameter groovy script.

But nothing seems to work. So far I've tried setting AWS_PROFILE variable and defining a system property with System.setProperty

Interestingly enough the aws cli commands with --profile=jprod seem to work. Is there a way of doing something similar with kops? Or any other suggestion on how to switch profiles inside the groovy script.

//System.setProperty("AWS_PROFILE", "jprod")
System.properties.'AWS_PROFILE' = 'jprod'

//AWS_PROFILE='jprod'
//def AWS_PROFILE='jstage'

def command ='kops get clusters --state=s3://k8s-bucket-state-prod'

def proc=command.execute()
proc.waitFor()       

def output = proc.in.text
def exitcode= proc.exitValue()
def error = proc.err.text

if (error) {
    println "Std Err: ${error}"
    println "Process exit code: ${exitcode}"
    return exitcode
}
return output.tokenize()

Upvotes: 1

Views: 1548

Answers (1)

Ivory Micky
Ivory Micky

Reputation: 581

Found the answer: just had to modify the command to use env:

def command ='env AWS_PROFILE=jprod kops get clusters --state=s3://k8s-bucket-state-prod'

Upvotes: 2

Related Questions