Created
September 28, 2020 12:27
-
-
Save andreidiaconu90/59dba3abc22928ce6cca4fa9364c2583 to your computer and use it in GitHub Desktop.
complex-bash
This file contains 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
#! /bin/bash | |
declare -r bucketName="aws-pipeline-bucket" | |
declare -r region="eu-west-1" | |
declare -r stackName="aws-pipeline" | |
declare -r pipelinesFolderPath="./aws-pipelines" | |
declare -r pathToTemplate="<the-path-of-the-pipeline-template>" | |
handleResponse() { | |
if [ $1 -eq 0 ] | |
then | |
echo "Done" | |
else | |
echo "Failed!" | |
echo "Error: $2" | |
echo "Exiting..." | |
exit 1 | |
fi | |
} | |
bucketExists() { | |
aws s3api head-bucket --bucket $1 2> /dev/null | |
local -i response=$? | |
if [ $response -eq 0 ] | |
then | |
true | |
else | |
false | |
fi | |
} | |
validateTemplate() { | |
echo "Validating template..." | |
output=$(aws cloudformation validate-template --template-body $pathToTemplate 2>&1) | |
exitCode=$? | |
handleResponse $exitCode "$output" | |
} | |
createBucket() { | |
echo "Creating bucket [$1]..." | |
output=$(aws s3api create-bucket --bucket $1 --region $region --create-bucket-configuration LocationConstraint=$region --acl private 2>&1) | |
local -i exitCode=$? | |
handleResponse $exitCode "$output" | |
} | |
packageTeplates() { | |
echo "Packaging templates..." | |
output=$(aws cloudformation package --template-file $pipelinesFolderPath/aws-pipeline-root.yml --output-template $pipelinesFolderPath/aws-pipeline.yml --s3-bucket $bucketName 2>&1) | |
local -i exitCode=$? | |
handleResponse $exitCode "$output" | |
} | |
deployPipelines() { | |
echo "Deploying pipelines..." | |
output=$(aws cloudformation deploy --template-file $pipelinesFolderPath/aws-pipeline.yml --stack-name $stackName --capabilities CAPABILITY_NAMED_IAM --region $region 2>&1) | |
local -i exitCode=$? | |
handleResponse $exitCode "$output" | |
} | |
deploy() { | |
echo "Starting pipeline deployment..." | |
validateTemplate | |
packageTeplates | |
deployPipelines | |
echo "Success!" | |
} | |
main() { | |
if bucketExists $bucketName | |
then | |
echo "Bucket [$bucketName] found" | |
deploy | |
else | |
echo "Bucket [$bucketName] not found" | |
createBucket $bucketName | |
deploy | |
fi | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment