Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabled GitHub deployment notifications. #3

Merged
merged 1 commit into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ DREVOPS_LAGOON_PRODUCTION_BRANCH=main
# The channels of the notifications.
#
# Can be a combination of comma-separated values: email,newrelic,github,jira
DREVOPS_NOTIFY_CHANNELS=email
DREVOPS_NOTIFY_CHANNELS=email,github

# Email to send notifications from.
DREVOPS_NOTIFY_EMAIL_FROM="[email protected]"
Expand Down
3 changes: 2 additions & 1 deletion .lagoon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@ tasks:
DREVOPS_NOTIFY_EVENT=post_deployment \
DREVOPS_NOTIFY_PROJECT=$LAGOON_PROJECT \
DREVOPS_NOTIFY_ENVIRONMENT_URL=$LAGOON_ROUTE \
./scripts/drevops/notify.sh
./scripts/drevops/notify.sh || true
service: cli
shell: bash

environments:
# Branch name that represents production environment.
main:
Expand Down
31 changes: 21 additions & 10 deletions scripts/drevops/notify-github.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ DREVOPS_NOTIFY_GITHUB_TOKEN="${DREVOPS_NOTIFY_GITHUB_TOKEN:-${GITHUB_TOKEN}}"
# Deployment repository.
DREVOPS_NOTIFY_REPOSITORY="${DREVOPS_NOTIFY_REPOSITORY:-}"

# Deployment reference, such as a git SHA.
DREVOPS_NOTIFY_REF="${DREVOPS_NOTIFY_REF:-}"
# Deployment reference branch.
DREVOPS_NOTIFY_BRANCH="${DREVOPS_NOTIFY_BRANCH:-}"

# The event to notify about. Can be 'pre_deployment' or 'post_deployment'.
DREVOPS_NOTIFY_EVENT="${DREVOPS_NOTIFY_EVENT:-}"
Expand All @@ -50,7 +50,7 @@ for cmd in php curl; do command -v ${cmd} >/dev/null || {

[ -z "${DREVOPS_NOTIFY_GITHUB_TOKEN}" ] && fail "Missing required value for DREVOPS_NOTIFY_GITHUB_TOKEN" && exit 1
[ -z "${DREVOPS_NOTIFY_REPOSITORY}" ] && fail "Missing required value for DREVOPS_NOTIFY_REPOSITORY" && exit 1
[ -z "${DREVOPS_NOTIFY_REF}" ] && fail "Missing required value for DREVOPS_NOTIFY_REF" && exit 1
[ -z "${DREVOPS_NOTIFY_BRANCH}" ] && fail "Missing required value for DREVOPS_NOTIFY_BRANCH" && exit 1
[ -z "${DREVOPS_NOTIFY_EVENT}" ] && fail "Missing required value for DREVOPS_NOTIFY_EVENT" && exit 1
[ -z "${DREVOPS_NOTIFY_ENVIRONMENT_TYPE}" ] && fail "Missing required value for DREVOPS_NOTIFY_ENVIRONMENT_TYPE" && exit 1

Expand Down Expand Up @@ -79,12 +79,16 @@ if [ "${DREVOPS_NOTIFY_EVENT}" = "pre_deployment" ]; then
-H "Accept: application/vnd.github.v3+json" \
-s \
"https://api.github.com/repos/${DREVOPS_NOTIFY_REPOSITORY}/deployments" \
-d "{\"ref\":\"${DREVOPS_NOTIFY_REF}\", \"environment\": \"${DREVOPS_NOTIFY_ENVIRONMENT_TYPE}\", \"auto_merge\": false}")"
-d "{\"ref\":\"${DREVOPS_NOTIFY_BRANCH}\", \"environment\": \"${DREVOPS_NOTIFY_ENVIRONMENT_TYPE}\", \"auto_merge\": false}")"

deployment_id="$(echo "${payload}" | extract_json_value "id")"
deployment_id="$(echo "${payload}" | extract_json_value "id" || true)"

# Check deployment ID.
{ [ "${#deployment_id}" -lt 9 ] || [ "${#deployment_id}" -gt 11 ] || [ "$(expr "x${deployment_id}" : "x[0-9]*$")" -eq 0 ]; } && fail "Failed to get a deployment ID for a started operation. Payload: ${payload}" && exit 1
if [ -z "${deployment_id}" ] || [ "${#deployment_id}" -lt 9 ] || [ "${#deployment_id}" -gt 11 ] || [ "$(expr "x${deployment_id}" : "x[0-9]*$")" -eq 0 ]; then
fail "Failed to get a deployment ID for a ${DREVOPS_NOTIFY_EVENT} operation. Payload: ${payload}"
fail "Wait for GitHub checks to finish and try again."
exit 1
fi

note "Marked deployment as started."
else
Expand All @@ -96,12 +100,16 @@ else
-H "Authorization: token ${DREVOPS_NOTIFY_GITHUB_TOKEN}" \
-H "Accept: application/vnd.github.v3+json" \
-s \
"https://api.github.com/repos/${DREVOPS_NOTIFY_REPOSITORY}/deployments?ref=${DREVOPS_NOTIFY_REF}")"
"https://api.github.com/repos/${DREVOPS_NOTIFY_REPOSITORY}/deployments?ref=${DREVOPS_NOTIFY_BRANCH}")"

deployment_id="$(echo "${payload}" | extract_json_first_value "id")"
deployment_id="$(echo "${payload}" | extract_json_first_value "id" || true)"

# Check deployment ID.
{ [ "${#deployment_id}" -lt 9 ] || [ "${#deployment_id}" -gt 11 ] || [ "$(expr "x${deployment_id}" : "x[0-9]*$")" -eq 0 ]; } && fail "Failed to get a deployment ID for a finished operation. Payload: ${payload}" && exit 1
if [ -z "${deployment_id}" ] || [ "${#deployment_id}" -lt 9 ] || [ "${#deployment_id}" -gt 11 ] || [ "$(expr "x${deployment_id}" : "x[0-9]*$")" -eq 0 ]; then
fail "Failed to get a deployment ID for a ${DREVOPS_NOTIFY_EVENT} operation. Payload: ${payload}"
fail "Check that a pre_deployment notification was dispatched."
exit 1
fi

# Post status update.
payload="$(curl \
Expand All @@ -114,7 +122,10 @@ else

status="$(echo "${payload}" | extract_json_value "state")"

[ "${status}" != "success" ] && fail "Unable to set deployment status" && exit 1
if [ "${status}" != "success" ]; then
fail "Previous deployment was found, but was unable to update the deployment status. Payload: ${payload}"
exit 1
fi

note "Marked deployment as finished."
fi
Expand Down