-
Notifications
You must be signed in to change notification settings - Fork 0
/
cicd.sh
executable file
·94 lines (70 loc) · 1.73 KB
/
cicd.sh
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
#!/bin/bash
if [ -z "$1" ]; then
echo "Please set version!"
exit 0
fi
VERSION=$1
WORKING_DIRECTORY=$(pwd)
BUCKET_NAME="dmm-integration"
declare -a LAMBDAS=("poll_feed" "manage_iam_policies")
function build {
local name=$1
local src="src/$name"
local out="out/$name"
# clean
rm -rf "$out"
mkdir -p "$out"
# copy sources
cp -r "$src/lambda_handler.py" "$src/requirements.txt" "$out"
cd "$out" || exit 1
# install dependencies
python3.10 -m pip install --upgrade -r requirements.txt --target .
cd "$WORKING_DIRECTORY" || exit 1
}
function test {
local name=$1
local src="src/$name"
local out="out/$name"
# copy testfile
cp -r "$src/test_lambda_handler.py" "$out"
cd "$out" || exit 1
# run tests
python3.10 -m unittest -v test_lambda_handler -f
exit_code=$?
# exit if any test failed
if [ $exit_code != 0 ]; then exit $exit_code
fi
# remove testfile
rm test_lambda_handler.py
cd "$WORKING_DIRECTORY" || exit 1
}
function deploy {
local name=$1
local result="${name}_${VERSION}.zip"
cd "out/$name" || exit 1
# bundle
zip -rqu "$result" .
# copy to s3
aws s3 cp "$result" "s3://$BUCKET_NAME/$name/src/${VERSION}/lambda.zip"
# back to working directory
cd "$WORKING_DIRECTORY" || exit 1
}
# create s3 bucket
aws s3 mb "s3://$BUCKET_NAME"
# build lambda sources
for i in "${LAMBDAS[@]}" ; do
build "$i"
done
# test lambda sources
for i in "${LAMBDAS[@]}" ; do
test "$i"
done
# deploy lambda sources to s3
for i in "${LAMBDAS[@]}" ; do
deploy "$i"
done
# create or update infrastructure and application
terraform -chdir=terraform init -upgrade
terraform -chdir=terraform apply\
-var='versions={"poll_feed":"'"$VERSION"'","manage_iam_policies":"'"$VERSION"'"}'\
-auto-approve