Improve handling of missing auth env vars #98
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Django CI | |
on: | |
push: | |
branches: ["main"] | |
pull_request: | |
branches: ["main"] | |
env: | |
PYTHON_VERSION: "3.11" | |
POETRY_VERSION: "1.7.1" # Remember to also update in pyproject.toml | |
IMAGE_NAME: unicorn-backend | |
jobs: | |
validate: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Check out repository | |
uses: actions/checkout@v3 | |
- name: Set up python | |
uses: actions/setup-python@v4 | |
with: | |
python-version: ${{ env.PYTHON_VERSION }} | |
- name: Install Pre-Commit | |
run: python -m pip install pre-commit && pre-commit install | |
- name: Load cached Pre-Commit Dependencies | |
id: cached-poetry-dependencies | |
uses: actions/cache@v3 | |
with: | |
path: ~/.cache/pre-commit/ | |
key: pre-commit-4|${{ env.pythonLocation }}|${{ hashFiles('.pre-commit-config.yaml') }} | |
- name: Execute Pre-Commit | |
run: pre-commit run --show-diff-on-failure --color=always --all-files | |
test: | |
runs-on: ubuntu-latest | |
env: | |
DATABASE_USER: unicorn | |
DATABASE_NAME: unicorn | |
DATABASE_PASSWORD: insecure-password-0sy3wBnJs.#%@h2TrDsy?I*p#wU+MV | |
POSTGRES_USER: postgres | |
POSTGRES_PASSWORD: insecure-password-1KUkde2hxN4d3AvxhDsOkdsQTh4LE53c | |
services: | |
db: | |
image: postgres:15 | |
env: | |
POSTGRES_USER: ${{ env.POSTGRES_USER }} | |
POSTGRES_PASSWORD: ${{ env.POSTGRES_PASSWORD }} | |
DATABASE_USER: ${{ env.DATABASE_USER }} | |
DATABASE_NAME: ${{ env.DATABASE_NAME }} | |
DATABASE_PASSWORD: ${{ env.DATABASE_PASSWORD }} | |
options: >- | |
--health-cmd pg_isready | |
--health-interval 10s | |
--health-timeout 5s | |
--health-retries 5 | |
--name test_db | |
ports: | |
- 5432:5432 | |
steps: | |
- name: Check out repository | |
uses: actions/checkout@v3 | |
- name: Run database initialization script | |
run: docker exec -i test_db /bin/bash < scripts/dbinit/initialize-database.sh | |
- name: Set up python | |
uses: actions/setup-python@v4 | |
with: | |
python-version: ${{ env.PYTHON_VERSION }} | |
- name: Install Poetry | |
uses: snok/install-poetry@v1 | |
with: | |
version: ${{ env.POETRY_VERSION }} | |
virtualenvs-create: true | |
virtualenvs-in-project: true | |
installer-parallel: true | |
- name: Load cached venv | |
id: cached-poetry-dependencies | |
uses: actions/cache@v3 | |
with: | |
path: .venv | |
key: v1-venv-${{ runner.os }}-${{ env.PYTHON_VERSION }}-${{ hashFiles('**/poetry.lock') }} | |
- name: Install dependencies | |
run: poetry install --no-interaction --no-root | |
- name: Set pythonpath | |
run: echo "PYTHONPATH=$PWD" >> $GITHUB_ENV | |
- name: Test | |
run: DATABASE_HOST=localhost poetry run python unicorn/manage.py test unicorn | |
build: | |
needs: [validate, test] | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Build image | |
run: docker build . -f Dockerfile --tag image | |
- name: Log in to the Container registry | |
uses: docker/login-action@v3 | |
with: | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Push image | |
run: | | |
IMAGE_ID=ghcr.io/${{ github.repository }}/$IMAGE_NAME | |
# Strip git ref prefix from version | |
VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,') | |
# Strip "v" prefix from tag name | |
[[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//') | |
# Use Docker `latest` tag convention | |
[ "$VERSION" == "main" ] && VERSION=latest | |
echo IMAGE_ID=$IMAGE_ID | |
echo VERSION=$VERSION | |
docker tag image $IMAGE_ID:$VERSION | |
docker push $IMAGE_ID:$VERSION |