Jenkins shared library and CDK constructs for AWS infrastructure. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
97 lines
2.2 KiB
Groovy
97 lines
2.2 KiB
Groovy
/**
|
|
* AWS utilities for Spicy CDK pipelines
|
|
*/
|
|
|
|
def runCLI(Map args) {
|
|
withCredentials([[$class: 'AmazonWebServicesCredentialsBinding',
|
|
credentialsId: args.account.jenkinsAwsCredentialsId,
|
|
accessKeyVariable: 'AWS_ACCESS_KEY_ID',
|
|
secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) {
|
|
return sh(
|
|
script: """#!/bin/bash +x
|
|
set -e
|
|
export AWS_DEFAULT_REGION=${args.account.region}
|
|
set -x
|
|
${args.command}
|
|
set +x
|
|
""",
|
|
returnStdout: true
|
|
).trim()
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get the current AWS account ID
|
|
*/
|
|
def getAccountId(Map args) {
|
|
return runCLI(
|
|
account: args.account,
|
|
command: "aws sts get-caller-identity --query Account --output text"
|
|
)
|
|
}
|
|
|
|
/**
|
|
* Check if a CloudFormation stack exists
|
|
*/
|
|
def stackExists(Map args) {
|
|
try {
|
|
runCLI(
|
|
account: args.account,
|
|
command: "aws cloudformation describe-stacks --stack-name ${args.stackName}"
|
|
)
|
|
return true
|
|
} catch (err) {
|
|
return false
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get CloudFormation stack outputs as a map
|
|
*/
|
|
def getStackOutputs(Map args) {
|
|
def outputs = [:]
|
|
try {
|
|
def result = runCLI(
|
|
account: args.account,
|
|
command: "aws cloudformation describe-stacks --stack-name ${args.stackName} --query 'Stacks[0].Outputs' --output json"
|
|
)
|
|
def parsed = readJSON text: result
|
|
parsed.each { output ->
|
|
outputs[output.OutputKey] = output.OutputValue
|
|
}
|
|
} catch (err) {
|
|
echo "Could not get stack outputs: ${err}"
|
|
}
|
|
return outputs
|
|
}
|
|
|
|
/**
|
|
* Get CloudFormation export value by export name
|
|
*/
|
|
def getCloudFormationExport(Map account, String exportName) {
|
|
try {
|
|
def result = runCLI(
|
|
account: account,
|
|
command: "aws cloudformation list-exports --query \"Exports[?Name=='${exportName}'].Value\" --output text"
|
|
)
|
|
return result ?: null
|
|
} catch (err) {
|
|
echo "Could not get CloudFormation export ${exportName}: ${err.message}"
|
|
return null
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Build account configuration from pipeline arguments
|
|
*/
|
|
def buildAccountConfig(Map args) {
|
|
return [
|
|
region: args.region,
|
|
jenkinsAwsCredentialsId: args.jenkinsAwsCredentialsId,
|
|
accountId: args.accountId ?: ''
|
|
]
|
|
}
|
|
|
|
return this
|
|
|