forked from youyo/aws-cdk-github-actions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
executable file
·123 lines (103 loc) · 3.02 KB
/
entrypoint.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
#!/bin/bash
set -u
function parseInputs(){
# Required inputs
if [ "${INPUT_CDK_SUBCOMMAND}" == "" ]; then
echo "Input cdk_subcommand cannot be empty"
exit 1
fi
}
function installNpmPackage(){
package=$1
scope=$2
echo "Install $package with scope $scope"
if [ "${INPUT_DEBUG_LOG}" == "true" ] && [ "$scope" == "local" ]; then
npm install $package
elif [ "${INPUT_DEBUG_LOG}" == "true" ] && [ "$scope" == "global" ]; then
sudo npm install -g $package
elif [ "$scope" == "local" ]; then
npm install $package --log-level=error --no-fund --no-audit $package >/dev/null
else
sudo npm install -g --log-level=error --no-fund --no-audit $package >/dev/null
fi
if [ "${?}" -ne 0 ]; then
echo "Failed to install $package"
else
echo "Successful install $package"
fi
}
function installAwsCdk(){
echo "Install aws-cdk ${INPUT_CDK_VERSION}"
if [ "${INPUT_CDK_VERSION}" == "latest" ]; then
installNpmPackage aws-cdk global
else
installNpmPackage aws-cdk@${INPUT_CDK_VERSION} global
fi
}
function npmCi(){
if [ -e "package.json" ]; then
echo "Run npm ci"
if [ "${INPUT_DEBUG_LOG}" == "true" ]; then
npm ci
else
npm ci --log-level=error --no-fund --no-audit >/dev/null
fi
if [ "${?}" -ne 0 ]; then
echo "Failed to run npm ci"
else
echo "Successful npm ci"
fi
fi
}
function installPipRequirements(){
if [ -e "requirements.txt" ]; then
echo "Install requirements.txt"
if [ "${INPUT_DEBUG_LOG}" == "true" ]; then
pip install -r requirements.txt
else
pip install -r requirements.txt >/dev/null 2>&1
fi
if [ "${?}" -ne 0 ]; then
echo "Failed to install requirements.txt"
else
echo "Successful install requirements.txt"
fi
fi
}
function runCdk(){
echo "Run cdk ${INPUT_CDK_SUBCOMMAND} ${*} \"${INPUT_CDK_STACK}\""
set -o pipefail
cdk ${INPUT_CDK_SUBCOMMAND} ${*} "${INPUT_CDK_STACK}" 2>&1 | tee output.log
exitCode=${?}
set +o pipefail
echo "status_code=${exitCode}" >> $GITHUB_OUTPUT
output=$(cat output.log)
commentStatus="Failed"
if [ "${exitCode}" == "0" ]; then
commentStatus="Success"
elif [ "${exitCode}" != "0" ]; then
echo "CDK subcommand ${INPUT_CDK_SUBCOMMAND} for stack ${INPUT_CDK_STACK} has failed. See above console output for more details."
exit 1
fi
if [ "$GITHUB_EVENT_NAME" == "pull_request" ] && [ "${INPUT_ACTIONS_COMMENT}" == "true" ]; then
commentWrapper="#### \`cdk ${INPUT_CDK_SUBCOMMAND}\` ${commentStatus}
<details><summary>Show Output</summary>
\`\`\`
${output}
\`\`\`
</details>
*Workflow: \`${GITHUB_WORKFLOW}\`, Action: \`${GITHUB_ACTION}\`, Working Directory: \`${INPUT_WORKING_DIR}\`*"
payload=$(echo "${commentWrapper}" | jq -R --slurp '{body: .}')
commentsURL=$(cat ${GITHUB_EVENT_PATH} | jq -r .pull_request.comments_url)
echo "${payload}" | curl -s -S -H "Authorization: token ${GITHUB_TOKEN}" --header "Content-Type: application/json" --data @- "${commentsURL}" > /dev/null
fi
}
function main(){
parseInputs
cd ${GITHUB_WORKSPACE}/${INPUT_WORKING_DIR}
npmCi
installAwsCdk
installPipRequirements
runCdk ${INPUT_CDK_ARGS}
}
main