Skip to content

Run build and deploy steps in parallel using the matrix strategy #123

Run build and deploy steps in parallel using the matrix strategy

Run build and deploy steps in parallel using the matrix strategy #123

Workflow file for this run

name: Continuous Integration and Delivery
on: [push]
env:
WEB_IMAGE: ghcr.io/$(echo $GITHUB_ACTOR | tr '[A-Z]' '[a-z]')/web
WORKER_IMAGE: ghcr.io/$(echo $GITHUB_ACTOR | tr '[A-Z]' '[a-z]')/worker
jobs:
test:
name: Test and check quality
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Build and run containers
run: docker-compose up -d
- name: Wait for database
run: sleep 30
- name: Apply database migrations
run: docker-compose exec -T web alembic upgrade head
- name: Check for pending database schema upgrade
run: docker-compose exec -T web alembic check
- name: Check code linting with ruff
run: docker-compose exec -T -u root web ruff check .
- name: Check code formatting with black
run: docker-compose exec -T -u root web black --check .
- name: Test code with pytest
run: docker-compose exec -T -u root web pytest -n auto .
- name: Cleanup
run: docker-compose down -v
build-and-deploy:
name: Build Docker Images and Deploy to Heroku
runs-on: ubuntu-latest
needs: [test]
strategy:
matrix:
image: [web, worker]
env:
HEROKU_APP_NAME: fastapi-tdd-docker-spyker77
HEROKU_REGISTRY_WEB_IMAGE: registry.heroku.com/${HEROKU_APP_NAME}/web
HEROKU_REGISTRY_WORKER_IMAGE: registry.heroku.com/${HEROKU_APP_NAME}/worker
steps:
- name: Delete the tools folder to free up space for CUDA
run: rm -rf /opt/hostedtoolcache
- name: Checkout
uses: actions/checkout@v4
- name: Log in to GitHub Packages
run: echo ${GITHUB_TOKEN} | docker login ghcr.io -u ${GITHUB_ACTOR} --password-stdin
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Pull images
run: |
docker pull ${{ env['${{ matrix.image }}_IMAGE'] }}-builder:latest || true
docker pull ${{ env['${{ matrix.image }}_IMAGE'] }}-cuda-devel:latest || true
docker pull ${{ env['${{ matrix.image }}_IMAGE'] }}-final:latest || true
- name: Build images
run: |
# Build Builder Stage
docker build \
--target builder \
--cache-from ${{ env['${{ matrix.image }}_IMAGE'] }}-builder:latest \
--tag ${{ env['${{ matrix.image }}_IMAGE'] }}-builder:latest \
--file ./Dockerfile.${{ matrix.image }} \
"."
# Build CUDA-Devel Stage
docker build \
--target cuda_devel \
--cache-from ${{ env['${{ matrix.image }}_IMAGE'] }}-cuda-devel:latest \
--tag ${{ env['${{ matrix.image }}_IMAGE'] }}-cuda-devel:latest \
--file ./Dockerfile.${{ matrix.image }} \
"."
# Build Final Stage
docker build \
--cache-from ${{ env['${{ matrix.image }}_IMAGE'] }}-final:latest \
--tag ${{ env['${{ matrix.image }}_IMAGE'] }}-final:latest \
--tag ${{ env['HEROKU_REGISTRY_${{ matrix.image | uppercase }}_IMAGE'] }}:latest \
--file ./Dockerfile.${{ matrix.image }} \
"."
- name: Push images to the packages
run: |
docker push ${{ env['${{ matrix.image }}_IMAGE'] }}-builder:latest
docker push ${{ env['${{ matrix.image }}_IMAGE'] }}-cuda-devel:latest
docker push ${{ env['${{ matrix.image }}_IMAGE'] }}-final:latest
# 🚨 Disable continuous delivery to Heroku 🚨
# - name: Log in to the Heroku Container Registry
# run: docker login -u _ -p ${HEROKU_AUTH_TOKEN} registry.heroku.com
# env:
# HEROKU_AUTH_TOKEN: ${{ secrets.HEROKU_AUTH_TOKEN }}
# - name: Push images to the registry
# run: docker push ${{ env['HEROKU_REGISTRY_${{ matrix.image | uppercase }}_IMAGE'] }}:latest
# - name: Set environment variables
# run: |
# echo "HEROKU_REGISTRY_${{ matrix.image | uppercase }}_IMAGE=${{ env['HEROKU_REGISTRY_${{ matrix.image | uppercase }}_IMAGE'] }}" >> $GITHUB_ENV
# echo "HEROKU_AUTH_TOKEN=${{ secrets.HEROKU_AUTH_TOKEN }}" >> $GITHUB_ENV
# - name: Release
# run: |
# chmod +x ./scripts/release.sh
# ./scripts/release.sh