-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.sh
executable file
·124 lines (103 loc) · 3.61 KB
/
build.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
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
#!/usr/bin/env bash
shopt -s nullglob
set -eo pipefail
function usage {
cat << USAGE >&2
usage:
${0##*/}
optional parameters:
-h help
description:
Packages cloudformation artifacts like templates, lambdas and archives and puts the result into the target directory.
Paths to different resources are configured within the build.conf file.
INFO: This script should be called from the project's root directory during the build phase.
requires:
aws cli >= 1.16.212
jq
zip
USAGE
exit 1
}
while getopts "h" opt ; do
case $opt in
h) usage ;;
\?) echo "Invalid option: -$OPTARG" >&2 && usage;;
esac
done
shift $((OPTIND-1))
[[ $# -ne 0 ]] && usage
# Configurations
# shellcheck source=build.conf
source build.conf
PROJECT_TEMPLATES=${PROJECT_TEMPLATES:-"project-templates"}
CFN_TEMPLATE_SNIPPETS=${CFN_TEMPLATE_SNIPPETS:-"cloudformation/templates"}
STACK=${STACK:-"cloudformation/stack"}
LAMBDAS_PATH=${LAMBDAS_PATH:-"lambda"}
PROJECT_VERSION=$(jq -r .Parameters.ProjectVersion "$STACK/develop_config.json")
if [[ -z "$PROJECT_VERSION" ]]; then
echo "BUILD_ERROR: provide the paramater $PROJECT_VERSION in $STACK/develop_config.json" && exit 1
fi
# Target path configuration. These paths are used by CloudFlow pipelines. Do not adjust them unless you know what you're doing
# The target has to be the same as in the buildspec.yaml
PROJECT_ROOTDIR=$(pwd)
TARGET="$PROJECT_ROOTDIR/target/$PROJECT_VERSION"
TARGET_LATEST="$PROJECT_ROOTDIR/target/latest/"
TARGET_CFN_TEMPLATES="$TARGET/cloudformation/templates"
TARGET_STACK="$TARGET/cloudformation"
TARGET_PROJECT_TEMPLATES="$TARGET/project-templates"
TARGET_LAMBDA="$TARGET/lambda"
function validate_template() {
aws cloudformation validate-template --template-body "file://$1" 1>/dev/null
}
function replace_extension() {
[[ $# -ne 2 ]] && echo -e "\e[31mBUILD_ERROR: failed to replace extension in $1: need 2 arguments but $# were given" >&2 && exit 2
[[ -z $2 ]] && echo -e "\e[31mBUILD_ERROR: failed to replace extension in $1:, extension cannot be empty" >&2 && exit 2
filename="$(basename -- "$1")"
ext="$2"
filename="${filename%.*}"
[[ -z $filename ]] && echo -e "\e[31mBUILD_ERROR: failed to replace extension in $1:, filename cannot be empty" >&2 && exit 2
echo "${filename}.$ext"
}
echo "BUILD_INFO: Building current project with version $PROJECT_VERSION"
echo "BUILD_INFO: Preparing the workspace"
rm -rf "${TARGET:?}"/* "${TARGET_LATEST:?}"/*
mkdir -p "$TARGET_LATEST" \
"$TARGET_CFN_TEMPLATES" \
"$TARGET_STACK" \
"$TARGET_PROJECT_TEMPLATES" \
"$TARGET_LAMBDA"
# stack
echo "BUILD_INFO: Packaging the stack artifact"
validate_template "$STACK/stack.yaml"
# validate the configuration files
jq empty "$STACK/live_config.json"
jq empty "$STACK/develop_config.json"
zip --junk-paths "$TARGET_STACK/stack.zip" "$STACK"/*
# cfn templates
for template in "$CFN_TEMPLATE_SNIPPETS"/*; do
echo "BUILD_INFO: Packaging $template"
validate_template "$template"
cp "$template" "$TARGET_CFN_TEMPLATES"
done
# lambda source code
( cd "$LAMBDAS_PATH"
for lambda in ./*[^"common"]; do
echo "BUILD_INFO: Packaging Lambda source code $lambda"
artifact_name=$(replace_extension "$lambda" zip)
zip -r "$TARGET_LAMBDA/$artifact_name" "$lambda" "common"/*
done
)
# project templates
( cd "$PROJECT_TEMPLATES"
for dir in *; do
echo "BUILD_INFO: Packaging project-template $dir"
artifact_name="${dir}.zip"
( cd "$dir"
zip -r "$TARGET_PROJECT_TEMPLATES/$artifact_name" ./* .*ignore
)
done
)
echo "BUILD_INFO: Updating the latest version "
if [[ "$PROJECT_VERSION" != "latest" ]]; then
cp -r "$TARGET"/* "$TARGET_LATEST"
fi