-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdeploy-cicd.sh
More file actions
executable file
·61 lines (50 loc) · 2.38 KB
/
deploy-cicd.sh
File metadata and controls
executable file
·61 lines (50 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/bash
echo Deploying AiProxy CICD Pipeline
# Create/Update the AiProxy build/deploy pipeline stack. This is manually created and maintained, but should not require elevated permissions.
# Options include:
# - TARGET_BRANCH: Defaults to `main`, passed as a Parameter for "cicd/2-cicd/cicd.template.yml"
# - ENVIRONMENT_TYPE: Can be 'production' (default) or 'development', passed as a Parameter for "cicd/2-cicd/cicd.template.yml"
# - GITHUB_BADGE_ENABLED: defaults to true, passed as a Parameter for "cicd/2-cicd/cicd.template.yml"
# 'Developer' role requires a specific service role for all CloudFormation operations.
if [[ $(aws sts get-caller-identity --query Arn --output text) =~ "assumed-role/Developer/" ]]; then
# Append the role-arn option to the positional parameters $@ passed to cloudformation deploy.
set -- "$@" --role-arn "arn:aws:iam::$(aws sts get-caller-identity --query Account --output text):role/admin/CloudFormationService"
fi
# Default to main branch, but support pipelines using other branches
TARGET_BRANCH=${TARGET_BRANCH-'main'}
if [ "$TARGET_BRANCH" == "main" ]
then
STACK_NAME="aiproxy-cicd"
else
# only allow alphanumeric branch names that may contain an internal hyphen.
# to avoid complicated logic elsewhere, we're constraining it here.
if [[ "$TARGET_BRANCH" =~ ^[a-z0-9]([-a-z0-9]*[a-z0-9])$ ]]; then
STACK_NAME="aiproxy-${TARGET_BRANCH}-cicd"
else
echo "Invalid branch name '${TARGET_BRANCH}', branches must be alphanumeric and may contain hyphens."
exit
fi
fi
ENVIRONMENT_TYPE=${ENVIRONMENT_TYPE-'production'}
GITHUB_BADGE_ENABLED=${GITHUB_BADGE_ENABLED-'true'}
TEMPLATE_FILE=cicd/2-cicd/cicd.template.yml
echo Validating cloudformation template...
aws cloudformation validate-template \
--template-body file://${TEMPLATE_FILE} \
> /dev/null
ACCOUNT=$(aws sts get-caller-identity --query "Account" --output text)
read -r -p "Would you like to deploy this template to AWS account $ACCOUNT? [y/N] " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]
then
echo Updating cloudformation stack...
aws cloudformation deploy \
--stack-name $STACK_NAME \
--template-file $TEMPLATE_FILE \
--parameter-overrides GitHubBranch=$TARGET_BRANCH GitHubBadgeEnabled=$GITHUB_BADGE_ENABLED EnvironmentType=$ENVIRONMENT_TYPE \
--capabilities CAPABILITY_IAM \
--tags EnvType=${ENVIRONMENT_TYPE} \
"$@"
echo Complete!
else
echo Exiting...
fi