Skip to content
This repository has been archived by the owner on Jan 2, 2024. It is now read-only.

Docker tests & previews #4111

Docker tests & previews

Docker tests & previews #4111

name: Docker tests & previews
on:
pull_request:
branches: [master]
types: [opened, synchronize, reopened]
merge_group:
types: [checks_requested]
permissions:
packages: write
actions: read
env:
IMAGE_NAME: 'ghcr.io/${{ github.repository }}'
TAG: ${{ github.sha }}
BRANCH_NAME: ${{ github.head_ref || github.ref_name }}
TF_VAR_admin_key: ${{ secrets.ADMIN_KEY_DEV }}
jobs:
build_backend:
name: 'Build backend'
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v41
with:
files: |
backend/**
files_ignore: |
backend/.gitignore
backend/.env.example
since_last_remote_commit: true
- name: List all changed files
run: |
for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
echo "$file was changed"
done
echo "any_changed = ${{ steps.changed-files.outputs.any_changed }}"
- name: Login to GitHub Container Registry
if: steps.changed-files.outputs.any_changed == 'true'
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build backend with cache and push to registry
if: steps.changed-files.outputs.any_changed == 'true'
run: |
cd backend
docker build \
--cache-from "$IMAGE_NAME/backend:latest" \
-t "$IMAGE_NAME/backend:$TAG" \
--build-arg BUILDKIT_INLINE_CACHE=1 \
.
docker push "$IMAGE_NAME/backend" --all-tags
env:
DOCKER_BUILDKIT: 1
kotest_tests:
name: 'Kotest tests'
runs-on: ubuntu-latest
needs: [build_backend]
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get changed files
id: changed-files
uses: tj-actions/changed-files@v41
with:
files: |
backend/**
files_ignore: |
backend/.gitignore
backend/.env.example
since_last_remote_commit: true
- name: List all changed files
run: |
for file in ${{ steps.changed-files.outputs.all_changed_files }}; do
echo "$file was changed"
done
echo "any_changed = ${{ steps.changed-files.outputs.any_changed }}"
- name: Pull Docker image
if: steps.changed-files.outputs.any_changed == 'true'
run: docker pull "$IMAGE_NAME/backend:$TAG"
- name: Run Kotest tests with Docker Compose
if: steps.changed-files.outputs.any_changed == 'true'
run: docker compose -f backend/docker-compose.kotest.yaml up --exit-code-from=backend
env:
ADMIN_KEY: admin-passord
cypress_e2e_tests:
name: 'Cypress end-to-end tests'
runs-on: ubuntu-latest
needs: [build_backend]
env:
ADMIN_KEY: admin-passord
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: 18
- name: Get changed files, frontend
id: changed-files-frontend
uses: tj-actions/changed-files@v41
with:
files: |
frontend/**
yarn.lock
package.json
.github/workflows/docker_tests_and_previews.yaml
files_ignore: |
*.md
*.png
*.ico
*.svg
*.test.tsx
frontend/.gitignore
frontend/.env.example
since_last_remote_commit: true
- name: List all changed files, frontend
run: |
for file in ${{ steps.changed-files-frontend.outputs.all_changed_files }}; do
echo "$file was changed"
done
echo "any_changed = ${{ steps.changed-files-frontend.outputs.any_changed }}"
- name: Get changed files, backend
id: changed-files-backend
uses: tj-actions/changed-files@v41
with:
files: |
backend/**
files_ignore: |
backend/.gitignore
backend/.env.example
since_last_remote_commit: true
- name: List all changed files, backend
run: |
for file in ${{ steps.changed-files-backend.outputs.all_changed_files }}; do
echo "$file was changed"
done
echo "any_changed = ${{ steps.changed-files-backend.outputs.any_changed }}"
- name: Backend changed, pull Docker image
if: steps.changed-files-frontend.outputs.any_changed == 'true' && steps.changed-files-backend.outputs.any_changed == 'true'
run: docker pull "$IMAGE_NAME/backend:$TAG"
- name: Backend didn't change, pull Docker image
if: steps.changed-files-frontend.outputs.any_changed == 'true' && steps.changed-files-backend.outputs.any_changed != 'true'
run: docker pull "$IMAGE_NAME/backend:latest"
- name: Start backend
if: steps.changed-files-frontend.outputs.any_changed == 'true' || steps.changed-files-backend.outputs.any_changed == 'true'
run: |
cd backend
docker compose up &
env:
BACKEND_TAG: ${{ steps.changed-files-backend.outputs.any_changed == 'true' && github.sha || 'latest' }}
ENVIRONMENT: preview
- name: Run Cypress end-to-end tests
if: steps.changed-files-frontend.outputs.any_changed == 'true' || steps.changed-files-backend.outputs.any_changed == 'true'
uses: cypress-io/[email protected]
with:
working-directory: frontend
install-command: yarn --frozen-lockfile
build: yarn build
start: yarn start
wait-on: 'http://localhost:3000, http://localhost:8080/status'
config: video=false
browser: firefox
env:
VERCEL_ENV: preview
SANITY_DATASET: testing
NEXTAUTH_URL: http://localhost:3000
NEXTAUTH_SECRET: very-secret-string-123
FEIDE_CLIENT_ID: ${{ secrets.FEIDE_CLIENT_ID }}
FEIDE_CLIENT_SECRET: ${{ secrets.FEIDE_CLIENT_SECRET }}
- name: Upload Cypress screenshots (only on failure)
uses: actions/upload-artifact@v4
if: failure()
with:
name: cypress-screenshots
path: frontend/cypress/screenshots
backend_preview:
name: 'Deploy backend preview'
runs-on: ubuntu-latest
if: github.event_name == 'pull_request'
needs: [build_backend]
outputs:
preview_backend_url: ${{ steps.preview_deploy.outputs.BACKEND_URL || steps.default_value.outputs.BACKEND_URL }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set BACKEND_URL default value
id: default_value
run: echo "BACKEND_URL=$BACKEND_URL" >> $GITHUB_OUTPUT
env:
BACKEND_URL: 'https://echo-web-dev.greenisland-73b91cd1.norwayeast.azurecontainerapps.io'
- name: Get changed files, backend
id: changed-files-backend
uses: tj-actions/changed-files@v41
with:
files: |
backend/**
files_ignore: |
backend/.gitignore
backend/.env.example
since_last_remote_commit: true
- name: List all changed files, backend
run: |
for file in ${{ steps.changed-files-backend.outputs.all_changed_files }}; do
echo "$file was changed"
done
echo "any_changed = ${{ steps.changed-files-backend.outputs.any_changed }}"
- name: Get changed files, Terraform
id: changed-files-tf
uses: tj-actions/changed-files@v41
with:
files: |
terraform/**
.github/workflows/docker_tests_and_previews.yaml
since_last_remote_commit: true
- name: List all changed files, Terraform
run: |
for file in ${{ steps.changed-files-tf.outputs.all_changed_files }}; do
echo "$file was changed"
done
echo "any_changed = ${{ steps.changed-files-tf.outputs.any_changed }}"
- name: Setup Terraform
if: steps.changed-files-backend.outputs.any_changed == 'true' || steps.changed-files-tf.outputs.any_changed == 'true'
uses: hashicorp/setup-terraform@v2
with:
terraform_wrapper: false
- name: Set environment variables
id: env_vars
if: steps.changed-files-backend.outputs.any_changed == 'true' || steps.changed-files-tf.outputs.any_changed == 'true'
run: |
echo "BRANCH_NAME_FORMATTED=$(echo "$BRANCH_NAME" | sed -e 's/-*login-*//g' -e 's/renovate\///g' -e 's/frontend/f/g' -e 's/backend/b/g' -e 's/[\/_\.]/-/g')" >> $GITHUB_OUTPUT
echo "BACKEND_TAG=$BACKEND_TAG" >> $GITHUB_OUTPUT
env:
BACKEND_TAG: ${{ steps.changed-files-backend.outputs.any_changed == 'true' && github.sha || 'latest' }}
- name: Deploy preview container
id: preview_deploy
if: steps.changed-files-backend.outputs.any_changed == 'true' || steps.changed-files-tf.outputs.any_changed == 'true'
run: |
cd terraform/preview
terraform init -lockfile=readonly
terraform workspace new $WORKSPACE_NAME || terraform workspace select $WORKSPACE_NAME
terraform apply -auto-approve -lock-timeout=15m
# Get backend url and output it
echo "BACKEND_URL=$(terraform output -raw backend_url)" >> $GITHUB_OUTPUT
env:
TF_VAR_rg_name: 'echo-web-${{ steps.env_vars.outputs.BRANCH_NAME_FORMATTED }}'
TF_VAR_db_password: ${{ secrets.DB_PASSWORD_DEV }}
TF_VAR_backend_image: '${{ env.IMAGE_NAME }}/backend:${{ steps.env_vars.outputs.BACKEND_TAG }}'
TF_VAR_auth_secret: ${{ secrets.AUTH_SECRET }}
ARM_CLIENT_ID: ${{ secrets.ARM_CLIENT_ID }}
ARM_CLIENT_SECRET: ${{ secrets.ARM_CLIENT_SECRET }}
ARM_SUBSCRIPTION_ID: ${{ secrets.ARM_SUBSCRIPTION_ID }}
ARM_TENANT_ID: ${{ secrets.ARM_TENANT_ID }}
WORKSPACE_NAME: ${{ steps.env_vars.outputs.BRANCH_NAME_FORMATTED }}
- name: Find comment
id: fc
if: steps.changed-files-backend.outputs.any_changed == 'true' || steps.changed-files-tf.outputs.any_changed == 'true'
uses: peter-evans/find-comment@v2
with:
issue-number: ${{ github.event.pull_request.number }}
body-includes: '<!-- BACKEND PREVIEW BOT -->'
- name: Create comment
if: steps.fc.outputs.comment-id == '' && (steps.changed-files-backend.outputs.any_changed == 'true' || steps.changed-files-tf.outputs.any_changed == 'true')
uses: peter-evans/create-or-update-comment@v2
with:
token: ${{ secrets.REPO_PAT }}
issue-number: ${{ github.event.pull_request.number }}
body: |
<!-- BACKEND PREVIEW BOT -->
**beep boop!**
I just deployed a backend preview to ${{ steps.preview_deploy.outputs.BACKEND_URL }} :rocket:
- [ ] check this box to restart the backend preview, if you are getting errors.
- name: Update comment
if: steps.fc.outputs.comment-id != '' && (steps.changed-files-backend.outputs.any_changed == 'true' || steps.changed-files-tf.outputs.any_changed == 'true')
uses: peter-evans/create-or-update-comment@v2
with:
token: ${{ secrets.REPO_PAT }}
comment-id: ${{ steps.fc.outputs.comment-id }}
edit-mode: replace
body: |
<!-- BACKEND PREVIEW BOT -->
**beep boop!**
I just deployed a backend preview to ${{ steps.preview_deploy.outputs.BACKEND_URL }} :rocket:
- [ ] check this box to restart the backend preview, if you are getting errors.
vercel_preview:
name: 'Deploy Vercel preview'
runs-on: ubuntu-latest
if: github.event_name == 'pull_request'
needs: [backend_preview]
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Deploy to Vercel
uses: snaplet/vercel-action@v3
with:
await-for-deployment: true
env: |
SANITY_DATASET=production
ADMIN_KEY=${{ secrets.ADMIN_KEY_DEV }}
NEXT_PUBLIC_BACKEND_URL=${{ needs.backend_preview.outputs.preview_backend_url }}
NEXTAUTH_SECRET=secret123AsDef£123
env:
VERCEL_ACCESS_TOKEN: ${{ secrets.VERCEL_TOKEN }}
VERCEL_PROJECT_ID: prj_8VUZLEU0bcCYk0j9aHHgorqmtuM0
VERCEL_TEAM_ID: team_BalajEhvjfBqbkouJFI9761w
GITHUB_TOKEN: ${{ secrets.REPO_PAT }}
- name: Seed backend with Sanity data
run: curl -v ${{ needs.backend_preview.outputs.preview_backend_url }}/sanity -u admin:$TF_VAR_admin_key --basic || true