Skip to content
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
28 changes: 25 additions & 3 deletions .github/workflows/close-pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,40 @@ on:

jobs:
remove_lagoon_environment:
#;< VORTEX_DEV
# The template repository has no Lagoon project and no webhook token.
if: ${{ github.repository != 'drevops/vortex' }}
#;> VORTEX_DEV
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- name: Remove environment after a PR is closed
run: |
cat "$GITHUB_EVENT_PATH" | curl \
if [ -z "${LAGOON_WEBHOOK_TOKEN}" ]; then echo "LAGOON_WEBHOOK_TOKEN secret is not set."; exit 1; fi

# Lagoon verifies the signature as GitHub webhooks do, so sign the
# exact bytes sent as the body.
signature_sha256="$(openssl dgst -sha256 -hmac "${LAGOON_WEBHOOK_TOKEN}" "$GITHUB_EVENT_PATH" | sed 's/^.* //')"
signature_sha1="$(openssl dgst -sha1 -hmac "${LAGOON_WEBHOOK_TOKEN}" "$GITHUB_EVENT_PATH" | sed 's/^.* //')"

status=0
response="$(curl \
--silent \
--show-error \
--fail-with-body \
-X POST \
--header "Content-Type: application/json" \
--header "X-GitHub-Delivery: ${RUNNER_TRACKING_ID:7}" \
--header "X-GitHub-Event: pull_request" \
--data-binary @- \
"${LAGOON_WEBHOOK_ENDPOINT:-https://hooks.lagoon.amazeeio.cloud/}"
--header "X-Hub-Signature-256: sha256=${signature_sha256}" \
--header "X-Hub-Signature: sha1=${signature_sha1}" \
--data-binary @"$GITHUB_EVENT_PATH" \
"${LAGOON_WEBHOOK_ENDPOINT:-https://webhook.amazeeio.cloud/}")" || status=$?
echo "${response}"

# Lagoon answers a rejected webhook with HTTP 200 and an error body.
if [ "${status}" != "0" ] || echo "${response}" | grep -q '"error"'; then exit 1; fi
env:
LAGOON_WEBHOOK_ENDPOINT: ${{ vars.LAGOON_WEBHOOK_ENDPOINT }}
LAGOON_WEBHOOK_TOKEN: ${{ secrets.LAGOON_WEBHOOK_TOKEN }}
26 changes: 21 additions & 5 deletions .vortex/docs/content/deployment/lagoon.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,29 @@ types](https://docs.lagoon.sh/concepts-advanced/environment-types/).
On GitHub, the shipped `.github/workflows/close-pull-request.yml` workflow
runs when a pull request closes. The workflow forwards the event to the
Lagoon webhook endpoint, so Lagoon removes the pull request's environment.
The endpoint defaults to `https://hooks.lagoon.amazeeio.cloud/`. To use
another Lagoon instance, set the `LAGOON_WEBHOOK_ENDPOINT` repository
variable.
Lagoon accepts only webhooks signed with the project's webhook token. The
workflow signs the event with the `LAGOON_WEBHOOK_TOKEN` repository secret.

To add the secret:

1. In the Lagoon UI, copy the **Webhook token** value from the project's
**Details** page. For a polysite, copy the organization's webhook token
instead.
2. In GitHub, add the value under
**Settings > Secrets and variables > Actions** as the
`LAGOON_WEBHOOK_TOKEN` repository secret.

When the secret is missing, the workflow fails with
`LAGOON_WEBHOOK_TOKEN secret is not set.`. The workflow also fails when
Lagoon rejects the request, and it prints Lagoon's response on every run.

The endpoint defaults to `https://webhook.amazeeio.cloud/`. To use another
Lagoon instance, set the `LAGOON_WEBHOOK_ENDPOINT` repository variable.

The installer keeps this workflow for CircleCI projects too, so the
workflow still runs if GitHub Actions is enabled for the repository. The
CircleCI config has no step that removes pull request environments.
workflow still runs if GitHub Actions is enabled for the repository. Add
the `LAGOON_WEBHOOK_TOKEN` secret to such a repository too. The CircleCI
config has no step that removes pull request environments.

## See also

Expand Down
12 changes: 10 additions & 2 deletions .vortex/docs/content/installation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,13 @@ To set up GitHub Actions:
secret too, for database fetches. With separate keys, use the read-only
key. Acquia fetches the database through the Acquia Cloud API, so it
needs no such secret.
4. On Acquia, add the API key and secret from [step 6](#6-set-up-hosting)
4. On Lagoon, add the project's webhook token as the `LAGOON_WEBHOOK_TOKEN`
secret, so Lagoon removes a pull request's environment when the pull
request closes. [Lagoon deployment](/docs/deployment/lagoon#environments)
says where to find the token.
5. On Acquia, add the API key and secret from [step 6](#6-set-up-hosting)
as the `VORTEX_ACQUIA_KEY` and `VORTEX_ACQUIA_SECRET` repository secrets.
5. On Acquia, add these 2 variables under
6. On Acquia, add these 2 variables under
**Settings > Secrets and variables > Actions > Variables**:
- `VORTEX_DEPLOY_ARTIFACT_GIT_REMOTE`: your application's Git repository
URL
Expand Down Expand Up @@ -330,6 +334,10 @@ To set up CircleCI:
URL
- `VORTEX_DEPLOY_ARTIFACT_GIT_USER_EMAIL`: the email address for the
deployment commits
9. On Lagoon with GitHub Actions enabled for the repository, add the
project's webhook token in GitHub as the `LAGOON_WEBHOOK_TOKEN` secret.
[Lagoon deployment](/docs/deployment/lagoon#environments) says where to
find the token and why the secret is needed.

On Lagoon, the pipeline fetches and deploys with the SSH keys you added.

Expand Down
1 change: 1 addition & 0 deletions .vortex/docs/cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
"phpcompatibility",
"phpspec",
"phpstorm",
"polysite",
"postbuild",
"preboot",
"prompty",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,30 @@ jobs:
steps:
- name: Remove environment after a PR is closed
run: |
cat "$GITHUB_EVENT_PATH" | curl \
if [ -z "${LAGOON_WEBHOOK_TOKEN}" ]; then echo "LAGOON_WEBHOOK_TOKEN secret is not set."; exit 1; fi

# Lagoon verifies the signature as GitHub webhooks do, so sign the
# exact bytes sent as the body.
signature_sha256="$(openssl dgst -sha256 -hmac "${LAGOON_WEBHOOK_TOKEN}" "$GITHUB_EVENT_PATH" | sed 's/^.* //')"
signature_sha1="$(openssl dgst -sha1 -hmac "${LAGOON_WEBHOOK_TOKEN}" "$GITHUB_EVENT_PATH" | sed 's/^.* //')"

status=0
response="$(curl \
--silent \
--show-error \
--fail-with-body \
-X POST \
--header "Content-Type: application/json" \
--header "X-GitHub-Delivery: ${RUNNER_TRACKING_ID:7}" \
--header "X-GitHub-Event: pull_request" \
--data-binary @- \
"${LAGOON_WEBHOOK_ENDPOINT:-https://hooks.lagoon.amazeeio.cloud/}"
--header "X-Hub-Signature-256: sha256=${signature_sha256}" \
--header "X-Hub-Signature: sha1=${signature_sha1}" \
--data-binary @"$GITHUB_EVENT_PATH" \
"${LAGOON_WEBHOOK_ENDPOINT:-https://webhook.amazeeio.cloud/}")" || status=$?
echo "${response}"

# Lagoon answers a rejected webhook with HTTP 200 and an error body.
if [ "${status}" != "0" ] || echo "${response}" | grep -q '"error"'; then exit 1; fi
env:
LAGOON_WEBHOOK_ENDPOINT: ${{ vars.LAGOON_WEBHOOK_ENDPOINT }}
LAGOON_WEBHOOK_TOKEN: ${{ secrets.LAGOON_WEBHOOK_TOKEN }}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,30 @@ jobs:
steps:
- name: Remove environment after a PR is closed
run: |
cat "$GITHUB_EVENT_PATH" | curl \
if [ -z "${LAGOON_WEBHOOK_TOKEN}" ]; then echo "LAGOON_WEBHOOK_TOKEN secret is not set."; exit 1; fi

# Lagoon verifies the signature as GitHub webhooks do, so sign the
# exact bytes sent as the body.
signature_sha256="$(openssl dgst -sha256 -hmac "${LAGOON_WEBHOOK_TOKEN}" "$GITHUB_EVENT_PATH" | sed 's/^.* //')"
signature_sha1="$(openssl dgst -sha1 -hmac "${LAGOON_WEBHOOK_TOKEN}" "$GITHUB_EVENT_PATH" | sed 's/^.* //')"

status=0
response="$(curl \
--silent \
--show-error \
--fail-with-body \
-X POST \
--header "Content-Type: application/json" \
--header "X-GitHub-Delivery: ${RUNNER_TRACKING_ID:7}" \
--header "X-GitHub-Event: pull_request" \
--data-binary @- \
"${LAGOON_WEBHOOK_ENDPOINT:-https://hooks.lagoon.amazeeio.cloud/}"
--header "X-Hub-Signature-256: sha256=${signature_sha256}" \
--header "X-Hub-Signature: sha1=${signature_sha1}" \
--data-binary @"$GITHUB_EVENT_PATH" \
"${LAGOON_WEBHOOK_ENDPOINT:-https://webhook.amazeeio.cloud/}")" || status=$?
echo "${response}"

# Lagoon answers a rejected webhook with HTTP 200 and an error body.
if [ "${status}" != "0" ] || echo "${response}" | grep -q '"error"'; then exit 1; fi
env:
LAGOON_WEBHOOK_ENDPOINT: ${{ vars.LAGOON_WEBHOOK_ENDPOINT }}
LAGOON_WEBHOOK_TOKEN: ${{ secrets.LAGOON_WEBHOOK_TOKEN }}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,30 @@ jobs:
steps:
- name: Remove environment after a PR is closed
run: |
cat "$GITHUB_EVENT_PATH" | curl \
if [ -z "${LAGOON_WEBHOOK_TOKEN}" ]; then echo "LAGOON_WEBHOOK_TOKEN secret is not set."; exit 1; fi

# Lagoon verifies the signature as GitHub webhooks do, so sign the
# exact bytes sent as the body.
signature_sha256="$(openssl dgst -sha256 -hmac "${LAGOON_WEBHOOK_TOKEN}" "$GITHUB_EVENT_PATH" | sed 's/^.* //')"
signature_sha1="$(openssl dgst -sha1 -hmac "${LAGOON_WEBHOOK_TOKEN}" "$GITHUB_EVENT_PATH" | sed 's/^.* //')"

status=0
response="$(curl \
--silent \
--show-error \
--fail-with-body \
-X POST \
--header "Content-Type: application/json" \
--header "X-GitHub-Delivery: ${RUNNER_TRACKING_ID:7}" \
--header "X-GitHub-Event: pull_request" \
--data-binary @- \
"${LAGOON_WEBHOOK_ENDPOINT:-https://hooks.lagoon.amazeeio.cloud/}"
--header "X-Hub-Signature-256: sha256=${signature_sha256}" \
--header "X-Hub-Signature: sha1=${signature_sha1}" \
--data-binary @"$GITHUB_EVENT_PATH" \
"${LAGOON_WEBHOOK_ENDPOINT:-https://webhook.amazeeio.cloud/}")" || status=$?
echo "${response}"

# Lagoon answers a rejected webhook with HTTP 200 and an error body.
if [ "${status}" != "0" ] || echo "${response}" | grep -q '"error"'; then exit 1; fi
env:
LAGOON_WEBHOOK_ENDPOINT: ${{ vars.LAGOON_WEBHOOK_ENDPOINT }}
LAGOON_WEBHOOK_TOKEN: ${{ secrets.LAGOON_WEBHOOK_TOKEN }}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,30 @@ jobs:
steps:
- name: Remove environment after a PR is closed
run: |
cat "$GITHUB_EVENT_PATH" | curl \
if [ -z "${LAGOON_WEBHOOK_TOKEN}" ]; then echo "LAGOON_WEBHOOK_TOKEN secret is not set."; exit 1; fi

# Lagoon verifies the signature as GitHub webhooks do, so sign the
# exact bytes sent as the body.
signature_sha256="$(openssl dgst -sha256 -hmac "${LAGOON_WEBHOOK_TOKEN}" "$GITHUB_EVENT_PATH" | sed 's/^.* //')"
signature_sha1="$(openssl dgst -sha1 -hmac "${LAGOON_WEBHOOK_TOKEN}" "$GITHUB_EVENT_PATH" | sed 's/^.* //')"

status=0
response="$(curl \
--silent \
--show-error \
--fail-with-body \
-X POST \
--header "Content-Type: application/json" \
--header "X-GitHub-Delivery: ${RUNNER_TRACKING_ID:7}" \
--header "X-GitHub-Event: pull_request" \
--data-binary @- \
"${LAGOON_WEBHOOK_ENDPOINT:-https://hooks.lagoon.amazeeio.cloud/}"
--header "X-Hub-Signature-256: sha256=${signature_sha256}" \
--header "X-Hub-Signature: sha1=${signature_sha1}" \
--data-binary @"$GITHUB_EVENT_PATH" \
"${LAGOON_WEBHOOK_ENDPOINT:-https://webhook.amazeeio.cloud/}")" || status=$?
echo "${response}"

# Lagoon answers a rejected webhook with HTTP 200 and an error body.
if [ "${status}" != "0" ] || echo "${response}" | grep -q '"error"'; then exit 1; fi
env:
LAGOON_WEBHOOK_ENDPOINT: ${{ vars.LAGOON_WEBHOOK_ENDPOINT }}
LAGOON_WEBHOOK_TOKEN: ${{ secrets.LAGOON_WEBHOOK_TOKEN }}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,30 @@ jobs:
steps:
- name: Remove environment after a PR is closed
run: |
cat "$GITHUB_EVENT_PATH" | curl \
if [ -z "${LAGOON_WEBHOOK_TOKEN}" ]; then echo "LAGOON_WEBHOOK_TOKEN secret is not set."; exit 1; fi

# Lagoon verifies the signature as GitHub webhooks do, so sign the
# exact bytes sent as the body.
signature_sha256="$(openssl dgst -sha256 -hmac "${LAGOON_WEBHOOK_TOKEN}" "$GITHUB_EVENT_PATH" | sed 's/^.* //')"
signature_sha1="$(openssl dgst -sha1 -hmac "${LAGOON_WEBHOOK_TOKEN}" "$GITHUB_EVENT_PATH" | sed 's/^.* //')"

status=0
response="$(curl \
--silent \
--show-error \
--fail-with-body \
-X POST \
--header "Content-Type: application/json" \
--header "X-GitHub-Delivery: ${RUNNER_TRACKING_ID:7}" \
--header "X-GitHub-Event: pull_request" \
--data-binary @- \
"${LAGOON_WEBHOOK_ENDPOINT:-https://hooks.lagoon.amazeeio.cloud/}"
--header "X-Hub-Signature-256: sha256=${signature_sha256}" \
--header "X-Hub-Signature: sha1=${signature_sha1}" \
--data-binary @"$GITHUB_EVENT_PATH" \
"${LAGOON_WEBHOOK_ENDPOINT:-https://webhook.amazeeio.cloud/}")" || status=$?
echo "${response}"

# Lagoon answers a rejected webhook with HTTP 200 and an error body.
if [ "${status}" != "0" ] || echo "${response}" | grep -q '"error"'; then exit 1; fi
env:
LAGOON_WEBHOOK_ENDPOINT: ${{ vars.LAGOON_WEBHOOK_ENDPOINT }}
LAGOON_WEBHOOK_TOKEN: ${{ secrets.LAGOON_WEBHOOK_TOKEN }}
Loading