Skip to content

Commit 9ceae6f

Browse files
Add website staging GCB template (#5418)
* 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.
1 parent 6b3321e commit 9ceae6f

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/bash
2+
set -e
3+
4+
deploy_to_firebase_staging_channel () {
5+
echo "Deploying website to a staging channel on firebase..."
6+
7+
# Deploy to firebase, but save the output to a variable so we can grep the
8+
# url from the output and save it to FIREBASE_STAGING_URL for future use.
9+
FIREBASE_DEPLOY_RESPONSE=$(firebase hosting:channel:deploy --expires 7d pr$PR_NUMBER-$HEAD_BRANCH --project=$PROJECT_ID)
10+
echo "$FIREBASE_DEPLOY_RESPONSE"
11+
FIREBASE_STAGING_URL=$(grep -Eo "https://$PROJECT_ID--[a-zA-Z0-9./?=_%:-]*" <<< "$FIREBASE_DEPLOY_RESPONSE")
12+
}
13+
14+
login_to_github() {
15+
echo "Logging into github under bot account..."
16+
17+
echo $GH_PAT_TOKEN > token
18+
gh auth login --with-token < token
19+
}
20+
21+
comment_staging_url_on_github () {
22+
echo "Commenting staging url on the PR..."
23+
COMMENT_BODY=$(echo -e "Visit the preview URL for this PR (updated for commit $COMMIT_SHA):\n\n$FIREBASE_STAGING_URL")
24+
25+
# The github CLI throws an error if --edit-last doesn't find a previous
26+
# comment, so this edits the last comment, but if it doesn't exist,
27+
# leave a new comment.
28+
gh pr comment $PR_NUMBER --edit-last --body "$COMMENT_BODY" --repo $REPO_FULL_NAME || \
29+
gh pr comment $PR_NUMBER --body "$COMMENT_BODY" --repo $REPO_FULL_NAME
30+
}
31+
32+
deploy_to_firebase_staging_channel
33+
login_to_github
34+
comment_staging_url_on_github

cloud_build/stage.yaml

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
steps:
2+
- name: gcr.io/cloud-builders/git
3+
args: ['submodule', 'update', '--init', '--recursive']
4+
- name: gcr.io/cloud-builders/docker
5+
entrypoint: '/bin/bash'
6+
args:
7+
- '-c'
8+
- |-
9+
set -e
10+
11+
echo "Building the website using a makefile..."
12+
make build
13+
- name: gcr.io/flutter-dev-230821/firebase-staging
14+
entrypoint: '/bin/bash'
15+
args:
16+
- '-c'
17+
- |-
18+
cloud_build/scripts/stage_site_and_comment_on_github.sh
19+
secretEnv: ['GH_PAT_TOKEN']
20+
env:
21+
- 'PR_NUMBER=$_PR_NUMBER'
22+
- 'HEAD_BRANCH=$_HEAD_BRANCH'
23+
- 'PROJECT_ID=$PROJECT_ID'
24+
- 'COMMIT_SHA=$COMMIT_SHA'
25+
- 'REPO_FULL_NAME=$REPO_FULL_NAME'
26+
availableSecrets:
27+
secretManager:
28+
- versionName: projects/$PROJECT_ID/secrets/dart-github-bot-comment-pat/versions/latest
29+
env: 'GH_PAT_TOKEN'
30+
timeout: 1200s
31+
options:
32+
logging: CLOUD_LOGGING_ONLY

0 commit comments

Comments
 (0)