Skip to content

Build, Test, & Deploy #27

Build, Test, & Deploy

Build, Test, & Deploy #27

Workflow file for this run

# This workflow will install Python dependencies, run tests and lint with a single version of Python
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python
name: build-test-deploy
run-name: Build, Test, & Deploy
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
permissions:
contents: read
jobs:
ci:
name: Build & Test
runs-on: ubuntu-latest
services:
postgres:
image: 'postgres:11'
ports:
- 5432:5432
env:
POSTGRES_USER: test
POSTGRES_PASSWORD: postgres
POSTGRES_DB: test
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v3
- name: Install poetry
run: pipx install poetry
- uses: actions/setup-python@v4
with:
python-version: '3.10'
cache: 'poetry'
- run: poetry install --without vscode
- run: poetry run black --check ./api/
- run: poetry run isort --check ./api/
- run: poetry run pytest --cov=api --cov-config=.coveragerc --cov-report=xml
env:
# Configuration details for connecting to Dockerized postgres
POSTGRES_HOST: localhost
POSTGRES_PORT: 5432
POSTGRES_USER: test
POSTGRES_PASSWORD: postgres
POSTGRES_DB: test
SECRET_KEY: testsecret
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
fail_ci_if_error: true
flags: unittest
cd:
name: Deploy
runs-on: ubuntu-latest
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
needs: ci
env:
SCRIPT_PATH: ${{ secrets.SCRIPT_PATH }}
steps:
- name: Configure SSH
run: |
mkdir -p ~/.ssh/
echo "$SSH_KEY" > ~/.ssh/deploy.key
chmod 600 ~/.ssh/deploy.key
cat >>~/.ssh/config <<END
Host deploy-server
HostName $SSH_HOST
User $SSH_USER
IdentityFile ~/.ssh/deploy.key
StrictHostKeyChecking no
END
env:
SSH_USER: ${{ secrets.SSH_USER }}
SSH_KEY: ${{ secrets.SSH_KEY }}
SSH_HOST: ${{ secrets.SSH_HOST }}
- name: Check out source
run: ssh deploy-server "cd \"${SCRIPT_PATH}/${REPO_NAME}\" && git fetch && git reset --hard origin/main"
env:
REPO_NAME: ${{ github.event.repository.name }}
- name: Update dependencies & run migrations
run: ssh deploy-server "cd \"$SCRIPT_PATH\" && ./update"
- name: Stop the server
run: ssh deploy-server "cd \"$SCRIPT_PATH\" && ./stop"
- name: Start the server
if: ${{ always() }}
run: ssh deploy-server "cd \"$SCRIPT_PATH\" && ./start"