Skip to content

Commit

Permalink
Move PR environment info to PR description (#672)
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenyu authored Jun 28, 2024
1 parent 53cf971 commit 525d3da
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/ci-app-pr-environment-destroy.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
name: CI App PR Environment Destroy
on:
workflow_dispatch:
inputs:
pr_number:
required: true
type: string
# !! Uncomment the following lines once you've set up the dev environment and are ready to enable PR environments
# pull_request:
# types: [closed]
Expand All @@ -11,3 +15,4 @@ jobs:
with:
app_name: "app"
environment: "dev"
pr_number: ${{ inputs.pr_number || github.event.number }}
9 changes: 9 additions & 0 deletions .github/workflows/ci-app-pr-environment-update.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
name: CI App PR Environment Update
on:
workflow_dispatch:
inputs:
pr_number:
required: true
type: string
commit_hash:
required: true
type: string
# !! Uncomment the following lines once you've set up the dev environment and are ready to enable PR environments
# pull_request:
jobs:
Expand All @@ -10,3 +17,5 @@ jobs:
with:
app_name: "app"
environment: "dev"
pr_number: ${{ inputs.pr_number || github.event.number }}
commit_hash: ${{ inputs.commit_hash || github.event.pull_request.head.sha }}
9 changes: 6 additions & 3 deletions .github/workflows/pr-environment-destroy.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: PR Environment Destroy
run-name: Destroy PR Environment ${{ github.event.number }}
run-name: Destroy PR Environment ${{ inputs.pr_number }}
on:
workflow_call:
inputs:
Expand All @@ -9,6 +9,9 @@ on:
environment:
required: true
type: string
pr_number:
required: true
type: string
jobs:
destroy:
name: Destroy environment
Expand All @@ -19,7 +22,7 @@ jobs:
id-token: write
pull-requests: write # Needed to comment on PR

concurrency: pr-environment-${{ github.event.number }}
concurrency: pr-environment-${{ inputs.pr_number }}

steps:
- uses: actions/checkout@v4
Expand All @@ -37,6 +40,6 @@ jobs:
environment: ${{ inputs.environment }}

- name: Destroy environment
run: ./bin/destroy-pr-environment "${{ inputs.app_name }}" "${{ inputs.environment }}" "${{ github.event.number }}"
run: ./bin/destroy-pr-environment "${{ inputs.app_name }}" "${{ inputs.environment }}" "${{ inputs.pr_number }}"
env:
GH_TOKEN: ${{ github.token }}
14 changes: 10 additions & 4 deletions .github/workflows/pr-environment-update.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: PR Environment Update
run-name: Update PR Environment ${{ github.event.number }}
run-name: Update PR Environment ${{ inputs.pr_number }}
on:
workflow_call:
inputs:
Expand All @@ -9,13 +9,19 @@ on:
environment:
required: true
type: string
pr_number:
required: true
type: string
commit_hash:
required: true
type: string
jobs:
build-and-publish:
name: " " # GitHub UI is noisy when calling reusable workflows, so use whitespace for name to reduce noise
uses: ./.github/workflows/build-and-publish.yml
with:
app_name: ${{ inputs.app_name }}
ref: ${{ github.sha }}
ref: ${{ inputs.commit_hash }}

update:
name: Update environment
Expand All @@ -27,7 +33,7 @@ jobs:
id-token: write
pull-requests: write # Needed to comment on PR

concurrency: pr-environment-${{ github.event.number }}
concurrency: pr-environment-${{ inputs.pr_number }}

steps:
- uses: actions/checkout@v4
Expand All @@ -45,6 +51,6 @@ jobs:
environment: ${{ inputs.environment }}

- name: Update environment
run: ./bin/update-pr-environment "${{ inputs.app_name }}" "${{ inputs.environment }}" "${{ github.event.number }}" "${{ github.sha }}"
run: ./bin/update-pr-environment "${{ inputs.app_name }}" "${{ inputs.environment }}" "${{ inputs.pr_number }}" "${{ inputs.commit_hash }}"
env:
GH_TOKEN: ${{ github.token }}
21 changes: 19 additions & 2 deletions bin/update-pr-environment
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,23 @@ service_name="$(terraform -chdir="infra/$app_name/service" output -raw service_n
echo "Wait for service ${service_name} to become stable"
aws ecs wait services-stable --cluster "${cluster_name}" --services "${service_name}"

echo "Post comment to PR with service endpoint"
service_endpoint="$(terraform -chdir="infra/${app_name}/service" output -raw service_endpoint)"
gh pr comment "${pr_number}" -b "Updated PR environment at ${service_endpoint}"
pr_info=$(cat <<EOF
<!-- begin PR environment info -->
## Preview environment
- Service endpoint: ${service_endpoint}
- Deployed commit: ${image_tag}
<!-- end PR environment info -->
EOF
)

pr_body="$(gh pr view "${pr_number}" --json body | jq --raw-output .body)"
if [[ $pr_body == *"<!-- begin PR environment info -->"*"<!-- end PR environment info -->"* ]]; then
pr_body="${pr_body//<!-- begin PR environment info -->*<!-- end PR environment info -->/$pr_info}"
else
pr_body="${pr_body}"$'\n\n'"${pr_info}"
fi

echo "Update PR description with PR environment info"
echo "${pr_info}"
gh pr edit "${pr_number}" --body "${pr_body}"
3 changes: 2 additions & 1 deletion docs/infra/pull-request-environments.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# Pull request environments

A temporary environment is created for each pull request that stays up while the pull request is open. Use cases for the temporary pull request environment includes:
A temporary environment is created for each pull request that stays up while the pull request is open. The endpoint for the pull request and the deployed commit are added to the pull request description, and updated when the environment is updated. Use cases for the temporary pull request environment includes:

- Allow other delivery stakeholders—including product managers, designers, and business owners—to review changes before being merged and deployed
- Enable automated end-to-end tests on the pull request
- Enable automated accessibility checks on the pull request
- Facilitate workspace creation for developing and testing service layer infrastructure changes

## Lifecycle of pull request environments

Expand Down

0 comments on commit 525d3da

Please sign in to comment.