Skip to content

Commit

Permalink
Fixed environment ref in GitHub becomes stale on follow-up PR deploym…
Browse files Browse the repository at this point in the history
…ents.
  • Loading branch information
AlexSkrypnyk committed Jan 31, 2025
1 parent a4668c7 commit 6015d4c
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
22 changes: 20 additions & 2 deletions .lagoon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,16 @@ tasks:
- run:
name: Notify about pre-deployment.
command: |
if [ -n "$LAGOON_PR_NUMBER" ]; then export VORTEX_NOTIFY_REF=$LAGOON_PR_NUMBER;export VORTEX_NOTIFY_SHA=${LAGOON_PR_HEAD_SHA#origin/};export VORTEX_NOTIFY_BRANCH=$LAGOON_PR_HEAD_BRANCH;else export VORTEX_NOTIFY_REF=$LAGOON_GIT_BRANCH;export VORTEX_NOTIFY_SHA=$LAGOON_GIT_SHA;export VORTEX_NOTIFY_BRANCH=$LAGOON_GIT_BRANCH;fi
if [ -n "$LAGOON_PR_NUMBER" ]; then
export VORTEX_NOTIFY_REF=$LAGOON_PR_NUMBER
export VORTEX_NOTIFY_SHA=${LAGOON_PR_HEAD_SHA#origin/}
export VORTEX_NOTIFY_BRANCH=$LAGOON_PR_HEAD_BRANCH
export VORTEX_NOTIFY_PR_NUMBER="${LAGOON_PR_NUMBER:-}"
else
export VORTEX_NOTIFY_REF=$LAGOON_GIT_BRANCH
export VORTEX_NOTIFY_SHA=$LAGOON_GIT_SHA
export VORTEX_NOTIFY_BRANCH=$LAGOON_GIT_BRANCH
fi
VORTEX_NOTIFY_PROJECT=$LAGOON_PROJECT \
VORTEX_NOTIFY_ENVIRONMENT_URL=$LAGOON_ROUTE \
VORTEX_NOTIFY_EVENT=pre_deployment ./scripts/vortex/notify.sh || true
Expand Down Expand Up @@ -88,7 +97,16 @@ tasks:
- run:
name: Send deployment notifications
command: |
if [ -n "$LAGOON_PR_NUMBER" ]; then export VORTEX_NOTIFY_REF=$LAGOON_PR_NUMBER; export VORTEX_NOTIFY_SHA=${LAGOON_PR_HEAD_SHA#origin/}; export VORTEX_NOTIFY_BRANCH=$LAGOON_PR_HEAD_BRANCH; else export VORTEX_NOTIFY_REF=$LAGOON_GIT_BRANCH; export VORTEX_NOTIFY_SHA=$LAGOON_GIT_SHA; export VORTEX_NOTIFY_BRANCH=$LAGOON_GIT_BRANCH; fi
if [ -n "$LAGOON_PR_NUMBER" ]; then
export VORTEX_NOTIFY_REF=$LAGOON_PR_NUMBER
export VORTEX_NOTIFY_SHA=${LAGOON_PR_HEAD_SHA#origin/}
export VORTEX_NOTIFY_BRANCH=$LAGOON_PR_HEAD_BRANCH
export VORTEX_NOTIFY_PR_NUMBER="${LAGOON_PR_NUMBER:-}"
else
export VORTEX_NOTIFY_REF=$LAGOON_GIT_BRANCH
export VORTEX_NOTIFY_SHA=$LAGOON_GIT_SHA
export VORTEX_NOTIFY_BRANCH=$LAGOON_GIT_BRANCH
fi
VORTEX_NOTIFY_EVENT=post_deployment \
VORTEX_NOTIFY_PROJECT=$LAGOON_PROJECT \
VORTEX_NOTIFY_ENVIRONMENT_URL=$LAGOON_ROUTE \
Expand Down
10 changes: 10 additions & 0 deletions .vortex/docs/content/workflows/variables.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1456,6 +1456,16 @@ Default value: `UNDEFINED`

Defined in: `scripts/vortex/notify-webhook.sh`, `scripts/vortex/notify.sh`

### `VORTEX_NOTIFY_PR_NUMBER`

Deployment PR number.

If set, the environment type will be set to 'pr-[`${VORTEX_NOTIFY_PR_NUMBER}`](#vortex_notify_pr_number)'.

Default value: `UNDEFINED`

Defined in: `scripts/vortex/notify-github.sh`

### `VORTEX_NOTIFY_REF`

Git reference to notify about.
Expand Down
30 changes: 30 additions & 0 deletions .vortex/tests/bats/notify.bats
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,36 @@ load _helper.bash
popd >/dev/null || exit 1
}

@test "Notify: github, pre_deployment, PR" {
pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1

app_id="123456789"

declare -a STEPS=(
"Started dispatching notifications."
"Started GitHub notification for pre_deployment event."
"@curl -X POST -H Authorization: token token12345 -H Accept: application/vnd.github.v3+json -s https://api.github.com/repos/myorg/myrepo/deployments -d {\"ref\":\"existingbranch\", \"environment\": \"PR-123\", \"auto_merge\": false} # {\"id\": \"${app_id}\", \"othervar\": \"54321\"}"
"Marked deployment as started."
"Finished GitHub notification for pre_deployment event."
"Finished dispatching notifications."
)

mocks="$(run_steps "setup")"

export VORTEX_NOTIFY_CHANNELS="github"
export VORTEX_NOTIFY_EVENT="pre_deployment"
export VORTEX_NOTIFY_GITHUB_TOKEN="token12345"
export VORTEX_NOTIFY_REPOSITORY="myorg/myrepo"
export VORTEX_NOTIFY_BRANCH="existingbranch"
export VORTEX_NOTIFY_PR_NUMBER="123"
run ./scripts/vortex/notify.sh
assert_success

run_steps "assert" "${mocks[@]}"

popd >/dev/null || exit 1
}

@test "Notify: github, pre_deployment, longer ID" {
pushd "${LOCAL_REPO_DIR}" >/dev/null || exit 1

Expand Down
9 changes: 9 additions & 0 deletions scripts/vortex/notify-github.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ VORTEX_NOTIFY_REPOSITORY="${VORTEX_NOTIFY_REPOSITORY:-}"
# Deployment reference branch.
VORTEX_NOTIFY_BRANCH="${VORTEX_NOTIFY_BRANCH:-}"

# Deployment PR number.
#
# If set, the environment type will be set to 'pr-${VORTEX_NOTIFY_PR_NUMBER}'.
VORTEX_NOTIFY_PR_NUMBER="${VORTEX_NOTIFY_PR_NUMBER:-}"

# The event to notify about. Can be 'pre_deployment' or 'post_deployment'.
VORTEX_NOTIFY_EVENT="${VORTEX_NOTIFY_EVENT:-}"

Expand Down Expand Up @@ -72,6 +77,10 @@ extract_json_value() {
php -r "\$data=json_decode(file_get_contents('php://stdin'), TRUE); isset(\$data[\"${key}\"]) ? print trim(json_encode(\$data[\"${key}\"], JSON_UNESCAPED_SLASHES), '\"') : exit(1);"
}

if [ "${VORTEX_NOTIFY_ENVIRONMENT_TYPE}" = "PR" ] && [ -n "${VORTEX_NOTIFY_PR_NUMBER}" ]; then
VORTEX_NOTIFY_ENVIRONMENT_TYPE="PR-${VORTEX_NOTIFY_PR_NUMBER}"
fi

if [ "${VORTEX_NOTIFY_EVENT}" = "pre_deployment" ]; then
payload="$(curl \
-X POST \
Expand Down

1 comment on commit 6015d4c

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.