-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
executable file
·56 lines (46 loc) · 1.79 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
#!/bin/bash
function parseInputs {
subcommand="check"
if [[ "${INPUT_AUTO_UPDATE}" == "true" ]]; then
subcommand="update latest"
elif [[ -n "${INPUT_SPECIFIC_VERSION}" ]]; then
subcommand="update ${INPUT_SPECIFIC_VERSION}"
fi
workdir="./"
if [[ -n "${INPUT_WORKING_DIR}" ]]; then
workdir=${INPUT_WORKING_DIR}
fi
commentPR=""
if [[ "${INPUT_COMMENT_PR}" == "true" ]]; then
commentPR="true"
fi
}
function main {
parseInputs
output=$(go run main.go ${subcommand} --root-path ${workdir} 2> /dev/null)
exitCode=${?}
if [ ${exitCode} -ne 0 && ${exitCode} -ne 3 ]; then
echo "error: failed to check Terraform cloud version"
exit ${exitCode}
fi
if [ -z "${output}" ]; then
echo "::set-output name=is_available_update::false"
echo "info: no updates are available"
exit 0
fi
echo "::set-output name=output::${output}"
echo "::set-output name=is_available_update::true"
workspaceLink=$(echo "${output}" | tail -n 1)
output=$(echo "${output}" | grep -v "${workspaceLink}")
echo "info: ${output}"
if [[ "${commentPR}" == "true" ]] && [[ "${GITHUB_EVENT_NAME}" == "pull_request" ]]; then
CommentsURL=$(cat ${GITHUB_EVENT_PATH} | jq -r .pull_request.comments_url)
echo "info: commenting on the pull request"
output="{\"body\": \"#### Terraform Cloud Workspace new version available\n\`\`\`\n${output}\n\`\`\`\n\n*working directory: \`${workdir}\`, ${workspaceLink}*\"}"
echo "${output}" | curl -XPOST -sS -H "Authorization: token ${GITHUB_TOKEN}" -H "Content-Type: application/json" --data @- "${CommentsURL}"
if [ ${?} -ne 0 ]; then
echo "error: failed to post a comment to the pull request"
fi
fi
}
main