-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into lorenyu/platform-cli
- Loading branch information
Showing
8 changed files
with
174 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# This workflow scans for orphaned PR environments | ||
name: Scan orphaned PR environments | ||
|
||
on: | ||
workflow_dispatch: | ||
schedule: | ||
# Run every day at 07:30 UTC (3:30am ET, 12:30am PT) after engineers are likely done with work | ||
- cron: "30 7 * * *" | ||
|
||
jobs: | ||
get-app-names: | ||
name: Get app names | ||
runs-on: ubuntu-latest | ||
outputs: | ||
app_names: ${{ steps.get-app-names.outputs.app_names }} | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Get app names | ||
id: get-app-names | ||
run: | | ||
source bin/util.sh | ||
app_names="$(get_app_names)" | ||
# turn app_names into a json list using jq | ||
app_names="$(echo "${app_names}" | jq -R -s -c 'split("\n")[:-1]')" | ||
echo "App names retrieved: ${app_names}" | ||
echo "app_names=${app_names}" >> "$GITHUB_OUTPUT" | ||
shell: bash | ||
scan: | ||
name: Scan | ||
runs-on: ubuntu-latest | ||
needs: get-app-names | ||
|
||
strategy: | ||
matrix: | ||
app_name: ${{ fromJson(needs.get-app-names.outputs.app_names) }} | ||
|
||
permissions: | ||
contents: read | ||
id-token: write | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Set up Terraform | ||
uses: ./.github/actions/setup-terraform | ||
|
||
- name: Configure AWS credentials | ||
uses: ./.github/actions/configure-aws-credentials | ||
with: | ||
app_name: ${{ matrix.app_name }} | ||
environment: dev | ||
|
||
- name: List PR workspaces | ||
run: | | ||
./bin/orphaned-pr-environments ${{ matrix.app_name }} | ||
env: | ||
GH_TOKEN: ${{ github.token }} | ||
TF_IN_AUTOMATION: "true" | ||
|
||
notify: | ||
name: Notify | ||
needs: scan | ||
if: failure() | ||
uses: ./.github/workflows/send-system-notification.yml | ||
with: | ||
channel: "workflow-failures" | ||
message: "🧹 [Orphaned PR environments for ${{ github.repository }}](https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }})" | ||
secrets: inherit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#!/bin/bash | ||
# ----------------------------------------------------------------------------- | ||
# This script checks for orphaned PR environments by listing all PR workspaces | ||
# and checking if the associated PR is closed. If the PR is closed the | ||
# resources in the workspace should have been destroyed and the workspace | ||
# deleted, so existing workspaces for closed PRs are considered orphaned. | ||
# ----------------------------------------------------------------------------- | ||
set -euo pipefail | ||
|
||
app_name="$1" | ||
|
||
echo "::group::Initialize Terraform" | ||
echo terraform -chdir="infra/${app_name}/service" init -input=false -reconfigure -backend-config="dev.s3.tfbackend" | ||
terraform -chdir="infra/${app_name}/service" init -input=false -reconfigure -backend-config="dev.s3.tfbackend" | ||
echo "::endgroup::" | ||
|
||
echo "::group::List PRs with PR environments" | ||
echo terraform -chdir="infra/${app_name}/service" workspace list | ||
workspaces="$(terraform -chdir="infra/${app_name}/service" workspace list)" | ||
pr_nums="$(echo "${workspaces}" | grep -o 'p-[0-9]\+' | sed 's/p-//')" | ||
echo "PRs" | ||
echo "${pr_nums}" | ||
echo "::endgroup::" | ||
|
||
echo "::group::Check status of each PR" | ||
closed_prs=() | ||
for pr_num in $pr_nums; do | ||
pr_status="$(gh pr view "$pr_num" --json state --jq ".state")" | ||
echo "PR ${pr_num}: ${pr_status}" | ||
|
||
if [ "$pr_status" == "CLOSED" ]; then | ||
closed_prs+=("$pr_num") | ||
fi | ||
done | ||
echo "::endgroup::" | ||
|
||
# if closed_prs is not empty exit with 1 otherwise exit with 0 | ||
if [ ${#closed_prs[@]} -gt 0 ]; then | ||
echo "Found orphaned PR environments for the following PRs: ${closed_prs[*]}" | ||
echo "Found orphaned PR environments for the following PRs: ${closed_prs[*]}" >> "${GITHUB_STEP_SUMMARY}" | ||
exit 1 | ||
fi | ||
|
||
echo "No orphaned PR environments" | ||
echo "No orphaned PR environments" >> "${GITHUB_STEP_SUMMARY}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/bin/bash | ||
# Utility functions | ||
|
||
# Retrieve the names of the applications in the repo by listing the directories in the "infra" directory | ||
# and filtering out the directories that are not applications. | ||
# Returns: A list of application names. | ||
function get_app_names() { | ||
find "infra" -maxdepth 1 -type d -not -name "infra" -not -name "accounts" -not -name "modules" -not -name "networks" -not -name "project-config" -not -name "test" -exec basename {} \; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# System Notifications | ||
|
||
The project sends notifications as part of CI/CD workflows to notify the team about system events such as deployments and workflow failures. | ||
|
||
## System notifications configuration | ||
|
||
The configuration for system notifications is defined in the application's [project-config module](/infra/project-config/). The [system-notifications.tf](/infra/project-config/system-notifications.tf) file defines one or more notification channels that CI/CD workflows can send notifications to. Each channel can use a different notification type. Currently, Slack is the only supported notification type. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters