Skip to content

Instantly share code, notes, and snippets.

@andymotta
Created April 4, 2019 01:58
Show Gist options
  • Save andymotta/4b0a919408bbd28f6230b05a5db534ad to your computer and use it in GitHub Desktop.
Save andymotta/4b0a919408bbd28f6230b05a5db534ad to your computer and use it in GitHub Desktop.
Declarative Jenkins Pipeline Cloudformation Release Stage
stage ('Release') {
steps {
script {
def apply = true
def status = null
try {
status = sh(script: "aws cloudformation describe-stacks --stack-name WEBAPP-${STACK_ENV} \
--query Stacks[0].StackStatus --output text --profile ${PROFILE}", returnStdout: true)
apply = true
} catch (err) {
apply = false
sh 'echo Creating WEBAPP-${STACK_ENV}....'
sh "aws cloudformation validate-template --template-body file://`pwd`/cloudformation.yml --profile ${PROFILE}"
sh "aws cloudformation create-stack --stack-name WEBAPP-${STACK_ENV} --template-body \
file://`pwd`/cloudformation.yml --parameters file://`pwd`/${ENV}.json --profile ${PROFILE}"
sh "aws cloudformation wait stack-create-complete --stack-name WEBAPP-${STACK_ENV} --profile ${PROFILE}"
sh "aws cloudformation describe-stack-events --stack-name WEBAPP-${STACK_ENV} \
--query 'StackEvents[].[{Resource:LogicalResourceId,Status:ResourceStatus,Reason:ResourceStatusReason}]' \
--output table --profile ${PROFILE}"
}
if (apply) {
try {
sh "echo Stack exists, attempting update..."
sh "aws cloudformation update-stack --stack-name \
WEBAPP-${STACK_ENV} --template-body file://`pwd`/cloudformation.yml \
--parameters file://`pwd`/${ENV}.json --profile ${PROFILE}"
} catch (error) {
sh "echo Finished create/update - no updates to be performed"
}
}
sh "echo Finished create/update successfully!"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment