-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.sh
More file actions
executable file
·156 lines (133 loc) · 3.56 KB
/
deploy.sh
File metadata and controls
executable file
·156 lines (133 loc) · 3.56 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/bin/bash
REGION="us-east-1"
PIPELINE_MODE=false
show_help() {
echo "Usage: deploy.sh [OPTIONS]"
echo "Deploy application to specified environment."
echo ""
echo "Options:"
echo " -e, --env <environment> Environment to deploy (dev or prod)"
echo " -p, --profile <profile> AWS profile"
echo " -r, --region <region> AWS region"
echo " --pipeline Activate pipeline mode"
echo " -h, --help Show this help message"
}
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-e|--env)
ENV="$2"
shift
shift
;;
-p|--profile)
PROFILE="$2"
shift
shift
;;
-r|--region)
REGION="$2"
shift
shift
;;
--pipeline)
PIPELINE_MODE=true
shift
;;
-h|--help)
show_help
exit 0
;;
*)
echo "Unknown option: $1"
show_help
exit 1
;;
esac
done
if [ "$PIPELINE_MODE" = false ]; then
shopt -s expand_aliases
source ~/.bash_aliases
fi
if [ -z "$ENV" ]; then
echo "Error: -e parameter is mandatory."
show_help
exit 1
fi
if [ "$PIPELINE_MODE" = false ] && [ -z "$PROFILE" ]; then
echo "Error: -p parameter is mandatory when not in pipeline mode."
show_help
exit 1
fi
if [ "$ENV" != "dev" ] && [ "$ENV" != "prod" ]; then
echo "Error: Invalid environment. Allowed values are dev and prod."
exit 1
fi
if [ "$ENV" == "prod" ]; then
if [ "$PIPELINE_MODE" = false ]; then
read -p "Are you sure you want to deploy to prod environment? (y/N) " confirm
if [ "$confirm" != "y" ]; then
echo "Deployment to prod environment aborted."
exit 1
fi
fi
fi
if [ "$PIPELINE_MODE" = true ]; then
echo "Pipeline mode activated."
else
echo "Standard deployment mode."
fi
echo "Compiling lambdas"
cd ./tag-processor
npm install
npm run compile
cp ./package.json ./build
cd ./build
npm install --omit=dev
cd ../..
cd ./tag-notification-sender
npm install
npm run compile
cp ./package.json ./build
cd ./build
npm install --omit=dev
cd ../..
echo "Deploying to $ENV environment in region $REGION..."
BUCKET_NAME="tag-processor-app-artifacts-$ENV"
PROFILE_FLAG=""
if [ "$PIPELINE_MODE" = false ]; then
PROFILE_FLAG="--profile $PROFILE"
echo "using profile $PROFILE..."
fi
if aws s3api head-bucket --bucket "$BUCKET_NAME" 2>/dev/null; then
echo "Bucket $BUCKET_NAME already exists."
else
echo "Bucket $BUCKET_NAME does not exist. Creating..."
aws s3api create-bucket --bucket "$BUCKET_NAME" --region "$REGION" $PROFILE_FLAG
echo "Bucket $BUCKET_NAME created."
fi
sam package --output-template-file ./output.yaml \
--s3-bucket "$BUCKET_NAME" \
$PROFILE_FLAG \
--region "$REGION" \
--template ./cloudformation/template.yaml || {
echo "Deployment failed due to a packaging error."
exit 1
}
STACK_NAME="tag-processor-stack-$ENV"
sam deploy --capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM \
--no-confirm-changeset \
--no-fail-on-empty-changeset \
--parameter-overrides Env=$ENV NotificationEmail=edgarp.dev@gmail.com \
$PROFILE_FLAG \
--region "$REGION" \
--s3-bucket "$BUCKET_NAME" \
--stack-name "$STACK_NAME" \
--template ./output.yaml || {
echo "Deployment failed due to a stack error."
exit 1
}
echo "Cleaning build files"
rm -rf ./tag-processor/build
rm -rf ./tag-notification-sender/buid
rm output.yaml