What are deploy previews?

AirQo Engineering
3 min readJul 15, 2022

--

Github logo
Photo by Rubaitul Azad on Unsplash

By Mike Mwanje

In software engineering, deploy previews are preview environments that are a replication of the staging/production environment in which new changes can be run in isolation.

Preview environments enable development teams to test changes in Pull Requests(PRs) before they can be merged. This is an additional step to ensuring that all changes made are safe and do not break anything in the production/staging environment.

Deploy previews also speed up the PR review process given that individual team members don’t have to always pull and run changes on their computers to test them out since that is already done for them and so they can simply just test out new changes. It is important to isolate the production, staging, and testing environments and that also goes for the databases.

Deploy previews at AirQo

At AirQo, we use GitHub actions for our CI/CD and that also includes the deploy previews. We set our workflow to create preview environments on Cloud Run and set up a subsequent clean-up process in the pipeline that automatically deletes the preview environments on Cloud Run when the PR is merged. For secret management and environment variable injection into these deploy previews, we leverage Secret Manager which enables us to fully automate the process without having to manually set these values each time a new PR is raised for a microservice.

Below is an example of the workflow file for the authentication microservice

### auth_service ###
auth-service:
name: build-push-deploy-auth-service
needs: [check,branch-name]
if: needs.check.outputs.run_auth_service == 'true'
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
outputs:
url: ${{ steps.preview-url.outputs.url }}
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Google Auth
id: auth
uses: google-github-actions/auth@v0
with:
credentials_json: ${{ secrets.GCP_SA_CREDENTIALS }}

- name: Setup Cloud SDK
uses: google-github-actions/setup-gcloud@v0

- name: Docker Auth
id: docker-auth
uses: docker/login-action@v1
with:
registry: ${{ secrets.REGISTRY_URL }}
username: _json_key
password: ${{ secrets.GCP_SA_CREDENTIALS }}

- name: Build and Push Container
run: |
cd src/auth-service/
docker build --target=staging --tag ${{ secrets.REGISTRY_URL }}/${{ secrets.PROJECT_ID }}/pr-previews/auth-service-pr-previews:${{ github.sha }} ./
docker push ${{ secrets.REGISTRY_URL }}/${{ secrets.PROJECT_ID }}/pr-previews/auth-service-pr-previews:${{ github.sha }}

- name: Deploy to Cloud Run
run: |-
gcloud run deploy ${{ needs.branch-name.outputs.lowercase }}-auth-service-preview \
--region=${{ secrets.REGION }} \
--max-instances=10 \
--timeout=60 \
--concurrency=10 \
--image=${{ secrets.REGISTRY_URL }}/${{ secrets.PROJECT_ID }}/pr-previews/auth-service-pr-previews:${{ github.sha }} \
--port=3000 \
--cpu=1000m \
--memory=256Mi \
--update-secrets=/etc/env/.env=sta-env-auth-service:latest,/etc/config/firebase_admin_sdk.json=sta-key-auth-service-firebase-admin-sdk:latest \
--command="/bin/sh","-c","cat /etc/env/.env >> /usr/src/app/.env; npm run stage-mac" \
--allow-unauthenticated

- name: Get preview service url
id: preview-url
run: |
read service_url < <(gcloud run services describe ${{ needs.branch-name.outputs.lowercase }}-auth-service-preview \
--format='value(status.url)' \
--platform managed \
--region ${{ secrets.REGION }})
echo "::set-output name=url::${service_url}"

auth-service-pr-comment:
name: auth-service-preview-link-comment
if: needs.check.outputs.run_auth_service == 'true'
needs: [auth-service]
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v6
with:
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: 'Auth-service changes in this PR available for preview [here](${{ needs.auth-service.outputs.url }})'
})

This file has the entire workflow that creates the deploy previews for all the microservices

Once a contributor raises a PR, the GitHub action builds the docker image of the changes and deploys them to Cloud Run, and adds a comment to the PR as below;

using GitHub actions to setup deploy previews
Using GitHub actions to set up deploy previews

Other team members then click on the link under ‘here’ and can access the changes. The base url created in this case was https://calibrate-basic-version-calibrate-preview-w7kzhvlewq-ew.a.run.app and team members then use this endpoint to test out the changes in Postman.

I hope you found this informative and that you can also start creating deploy previews for your projects if you don’t already have them.

Explore our digital tools. Learn about the quality of air around you. Click here to get started.

--

--

AirQo Engineering

Engineering and technology articles for developers, written and curated by AirQo engineers