Skip to content

Instantly share code, notes, and snippets.

@andreidiaconu90
Created September 28, 2020 12:27
Show Gist options
  • Save andreidiaconu90/59dba3abc22928ce6cca4fa9364c2583 to your computer and use it in GitHub Desktop.
Save andreidiaconu90/59dba3abc22928ce6cca4fa9364c2583 to your computer and use it in GitHub Desktop.
complex-bash
#! /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