Created
April 4, 2019 01:58
-
-
Save andymotta/4b0a919408bbd28f6230b05a5db534ad to your computer and use it in GitHub Desktop.
Declarative Jenkins Pipeline Cloudformation Release Stage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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