Skip to content

Commit

Permalink
Add website staging GCB template (#5418)
Browse files Browse the repository at this point in the history
* Adds a GCB template that is responsible for building the website and
staging it to firebase in PRs
* Once the website is built, `dart-github-bot` will comment in the PR a
link that you can navigate to in order to see your changes.
* After this template is merged in, we can update the PR trigger to run
this template on all PRs. It is currently disabled in order to ensure
that it is not run on branches without this template in it yet.
  • Loading branch information
drewroengoogle authored Dec 22, 2023
1 parent 6b3321e commit 9ceae6f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
34 changes: 34 additions & 0 deletions cloud_build/scripts/stage_site_and_comment_on_github.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash
set -e

deploy_to_firebase_staging_channel () {
echo "Deploying website to a staging channel on firebase..."

# Deploy to firebase, but save the output to a variable so we can grep the
# url from the output and save it to FIREBASE_STAGING_URL for future use.
FIREBASE_DEPLOY_RESPONSE=$(firebase hosting:channel:deploy --expires 7d pr$PR_NUMBER-$HEAD_BRANCH --project=$PROJECT_ID)
echo "$FIREBASE_DEPLOY_RESPONSE"
FIREBASE_STAGING_URL=$(grep -Eo "https://$PROJECT_ID--[a-zA-Z0-9./?=_%:-]*" <<< "$FIREBASE_DEPLOY_RESPONSE")
}

login_to_github() {
echo "Logging into github under bot account..."

echo $GH_PAT_TOKEN > token
gh auth login --with-token < token
}

comment_staging_url_on_github () {
echo "Commenting staging url on the PR..."
COMMENT_BODY=$(echo -e "Visit the preview URL for this PR (updated for commit $COMMIT_SHA):\n\n$FIREBASE_STAGING_URL")

# The github CLI throws an error if --edit-last doesn't find a previous
# comment, so this edits the last comment, but if it doesn't exist,
# leave a new comment.
gh pr comment $PR_NUMBER --edit-last --body "$COMMENT_BODY" --repo $REPO_FULL_NAME || \
gh pr comment $PR_NUMBER --body "$COMMENT_BODY" --repo $REPO_FULL_NAME
}

deploy_to_firebase_staging_channel
login_to_github
comment_staging_url_on_github
32 changes: 32 additions & 0 deletions cloud_build/stage.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
steps:
- name: gcr.io/cloud-builders/git
args: ['submodule', 'update', '--init', '--recursive']
- name: gcr.io/cloud-builders/docker
entrypoint: '/bin/bash'
args:
- '-c'
- |-
set -e
echo "Building the website using a makefile..."
make build
- name: gcr.io/flutter-dev-230821/firebase-staging
entrypoint: '/bin/bash'
args:
- '-c'
- |-
cloud_build/scripts/stage_site_and_comment_on_github.sh
secretEnv: ['GH_PAT_TOKEN']
env:
- 'PR_NUMBER=$_PR_NUMBER'
- 'HEAD_BRANCH=$_HEAD_BRANCH'
- 'PROJECT_ID=$PROJECT_ID'
- 'COMMIT_SHA=$COMMIT_SHA'
- 'REPO_FULL_NAME=$REPO_FULL_NAME'
availableSecrets:
secretManager:
- versionName: projects/$PROJECT_ID/secrets/dart-github-bot-comment-pat/versions/latest
env: 'GH_PAT_TOKEN'
timeout: 1200s
options:
logging: CLOUD_LOGGING_ONLY

0 comments on commit 9ceae6f

Please sign in to comment.