Skip to content

FastAPI CI/CD

FastAPI CI/CD #74

Workflow file for this run

name: FastAPI CI/CD
on:
push:
branches:
- main
jobs:
CI:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.9
- name: Install Python Virtual ENV
run: pip3.9 install virtualenv
- name: Virtual ENV
uses: actions/cache@v2
id: cache-venv
with:
path: venv
key: ${{ runner.os }}-venv-${{ hashFiles('**/requirements*.txt') }}
restore-keys: |
${{ runner.os }}-venv-
# - name: Run Tests
# run: >
# python3 -m venv venv && source venv/bin/activate &&
# python3 -m pip install --upgrade pip &&
# python3 -m pip install pytest && python3 -m pip install pytest-cov &&
# python3 -m pytest -s -vv -m "not slow" --cov=tests --cov=balancelib -W ignore::DeprecationWarning --cov-report term-missing:skip-covered
- name: Activate Virtual ENV
run: >
python3 -m venv venv && source venv/bin/activate &&
python3 -m pip install --upgrade pip &&
pip3 install -r requirements.txt --only-binary=:all: --no-deps
--platform manylinux2010_x86_64 --target=dependencies --python 3.9
- name: Install Cryptography
run: >
python3 -m venv venv && source venv/bin/activate &&
python3 -m pip install --upgrade pip &&
pip3 install --platform manylinux2014_x86_64 --target=dependencies
--implementation cp --python 3.9 --only-binary=:all: --upgrade cryptography==38.0.0
- name: Create archive of dependencies
run: |
cd ./venv/lib/python3.9/site-packages
zip -r9 ../../../../api.zip .
- name: Add API files to Zip file
run: >
zip -g ./api.zip -r balancelib &&
zip -g ./api.zip -r database &&
zip -g ./api.zip -r main.py &&
cd dependencies && zip -g ../api.zip -r . && cd ..
- name: Upload zip file artifact
uses: actions/upload-artifact@v2
with:
name: api
path: api.zip
CD:
runs-on: ubuntu-latest
needs: [CI]
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
steps:
- name: Install AWS CLI
uses: unfor19/install-aws-cli-action@v1
with:
version: 1
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_SECRET_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_DEFAULT_REGION: ${{ secrets.AWS_DEFAULT_REGION }}
- name: Download Lambda api.zip
uses: actions/download-artifact@v2
with:
name: api
- name: Upload to S3
run: aws s3 cp api.zip s3://${{ secrets.AWS_BUCKET_FASTAPI }}/api.zip
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_SECRET_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_DEFAULT_REGION: ${{ secrets.AWS_DEFAULT_REGION }}
- name: Deploy new Lambda
run: aws lambda update-function-code --function-name ${{ secrets.AWS_FUNCTION_NAME }} --s3-bucket ${{ secrets.AWS_BUCKET_FASTAPI }} --s3-key api.zip
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_SECRET_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_DEFAULT_REGION: ${{ secrets.AWS_DEFAULT_REGION }}