Skip to content

Deploy Preview

Deploy Preview #143

name: Deploy Preview
on:
workflow_run:
workflows:
- Start Preview Deployment
types:
- completed
permissions:
id-token: write
contents: read
deployments: write
jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}
env:
PROJECT_ID: slowreader-418220
REGION: europe-west1
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Download web assets from prepare step
uses: actions/download-artifact@v4
with:
name: preview-assets
repository: ${{ github.repository_owner }}/${{ github.event.repository.name }}
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ secrets.PREVIEW_ARTIFACT_PAT }}
- name: Extract PR number
run: |
PR_NUMBER=$(cat ./preview-id)
echo "PR_NUMBER=$PR_NUMBER" >> $GITHUB_ENV
- name: Notify about new deployment
uses: bobheadxi/deployments@v1
id: status
with:
step: start
token: ${{ secrets.GITHUB_TOKEN }}
ref: ${{ github.event.workflow_run.head_commit.id }}
env: preview-${{ env.PR_NUMBER }}
- name: Auth Google Cloud
uses: google-github-actions/auth@v2
with:
workload_identity_provider: projects/225773869708/locations/global/workloadIdentityPools/github/providers/slowreader
service_account: github-deploy@${{ env.PROJECT_ID }}.iam.gserviceaccount.com
- name: Install Google Cloud
uses: google-github-actions/setup-gcloud@v2
- name: Auth Docker
run: gcloud auth configure-docker ${{ env.REGION }}-docker.pkg.dev --quiet
- name: Build Docker image
run: docker build -t ${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/staging/staging:preview-${{ env.PR_NUMBER }} ./web
- name: Push Docker image
run: docker push ${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/staging/staging:preview-${{ env.PR_NUMBER }}
- name: Deploy to Cloud Run
id: deploy
uses: google-github-actions/deploy-cloudrun@v2
with:
service: staging
region: ${{ env.REGION }}
image: ${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/staging/staging:preview-${{ env.PR_NUMBER }}
tag: preview-${{ env.PR_NUMBER }}
no_traffic: true
flags: |
--allow-unauthenticated
--service-account=github-deploy@${{ env.PROJECT_ID }}.iam.gserviceaccount.com
- name: Update deployment status
uses: bobheadxi/deployments@v1
if: always()
with:
step: finish
token: ${{ secrets.GITHUB_TOKEN }}
status: ${{ job.status }}
env: ${{ steps.status.outputs.env }}
env_url: ${{ steps.deploy.outputs.url }}/ui/
deployment_id: ${{ steps.status.outputs.deployment_id }}
- name: Delete previous images
run: |
untagged=$(gcloud artifacts docker images list $REGION-docker.pkg.dev/$PROJECT_ID/staging/staging --include-tags --format="get(version)" --filter="tags = ''")
for digest in $untagged; do
image=$REGION-docker.pkg.dev/$PROJECT_ID/staging/staging@$digest
echo "Deleting unused image: $image"
gcloud artifacts docker images delete $image --quiet
done