Deploy #860
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
name: Deploy | |
on: | |
workflow_run: | |
# `workflow_run` events have access to secrets | |
workflows: [Build] | |
types: | |
- completed | |
jobs: | |
deploy_live_website: | |
runs-on: ubuntu-latest | |
if: > | |
${{ github.event.workflow_run.event == 'pull_request' && | |
github.event.workflow_run.conclusion == 'success' }} | |
steps: | |
- name: "Get PR information" | |
uses: potiuk/get-workflow-origin@751d47254ef9e8b5eef955e24e79305233702781 | |
id: source-run-info | |
with: | |
token: ${{ secrets.GITHUB_TOKEN }} | |
sourceRunId: ${{ github.event.workflow_run.id }} | |
- name: Checkout repository | |
uses: actions/checkout@v3 | |
- name: 'Download artifact' | |
uses: actions/github-script@v6 | |
with: | |
script: | | |
let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
run_id: context.payload.workflow_run.id, | |
}); | |
let matchArtifact = allArtifacts.data.artifacts.filter((artifact) => { | |
return artifact.name == "page-build" | |
})[0]; | |
let download = await github.rest.actions.downloadArtifact({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
artifact_id: matchArtifact.id, | |
archive_format: 'zip', | |
}); | |
let fs = require('fs'); | |
fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/page-build.zip`, Buffer.from(download.data)); | |
- run: | | |
unzip page-build.zip -d public | |
- name: change URLs for large files | |
if: ${{ steps.source-run-info.outputs.sourceEvent == 'push' && steps.source-run-info.outputs.targetBranch == 'master'}} | |
shell: bash | |
run: | | |
sed -i 's|search/search_index.json|https://storage.googleapis.com/cp-algorithms/search_index.json|g' public/assets/javascripts/*.js | |
- id: 'auth' | |
if: ${{ steps.source-run-info.outputs.sourceEvent == 'push' && steps.source-run-info.outputs.targetBranch == 'master'}} | |
uses: 'google-github-actions/auth@v0' | |
with: | |
credentials_json: '${{ secrets.GCP_CREDENTIALS }}' | |
- uses: 'google-github-actions/upload-cloud-storage@v1' | |
if: ${{ steps.source-run-info.outputs.sourceEvent == 'push' && steps.source-run-info.outputs.targetBranch == 'master'}} | |
with: | |
path: 'public/search/search_index.json' | |
destination: 'cp-algorithms' | |
- uses: FirebaseExtended/action-hosting-deploy@v0 | |
id: firebase-deploy | |
if: env.FIREBASE_SERVICE_ACCOUNT | |
env: | |
PREVIEW_NAME: "preview-${{ steps.source-run-info.outputs.pullRequestNumber }}" | |
LIVE_NAME: "live" | |
FIREBASE_SERVICE_ACCOUNT: ${{ secrets.FIREBASE_SERVICE_ACCOUNT }} | |
with: | |
repoToken: "${{ secrets.GITHUB_TOKEN }}" | |
firebaseServiceAccount: "${{ secrets.FIREBASE_SERVICE_ACCOUNT }}" | |
projectId: cp-algorithms | |
channelId: ${{ steps.source-run-info.outputs.sourceEvent == 'push' && steps.source-run-info.outputs.targetBranch == 'master' && env.LIVE_NAME || env.PREVIEW_NAME }} | |
- name: comment URL to PR | |
if: ${{ steps.source-run-info.outputs.sourceEvent == 'pull_request' }} | |
uses: actions/github-script@v6 | |
with: | |
script: | | |
const body = `Visit the preview URL for this PR (for commit ${{ steps.source-run-info.outputs.sourceHeadSha }}): | |
${{ steps.firebase-deploy.outputs.details_url }} | |
(expires ${{ steps.firebase-deploy.outputs.expire_time }})`; | |
github.rest.issues.createComment({ | |
issue_number: ${{ steps.source-run-info.outputs.pullRequestNumber }}, | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
body: body | |
}) |