Skip to content

Commit

Permalink
Merge pull request #73 from edwardchalstrey1/CI
Browse files Browse the repository at this point in the history
Add GH actions workflow (docker)
  • Loading branch information
edwardchalstrey1 authored Feb 19, 2024
2 parents 6a90e69 + 8401dfc commit 4c36480
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 35 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
name: build_docker_image

on:
push:
branches: [dev]
paths:
- 'Dockerfile'
pull_request:
branches: [dev]
paths:
- 'Dockerfile'

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Login to GitHub Packages
uses: docker/login-action@v1
with:
registry: docker.pkg.github.com
username: ${{ github.actor }}
password: ${{ secrets.GH_TOKEN }}
- name: Build and push Docker image
uses: docker/build-push-action@v2
with:
context: .
push: true
tags: docker.pkg.github.com/edwardchalstrey1/seshat/tests-image:latest
33 changes: 17 additions & 16 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
---
name: Test code
name: test_code

on:
push:
branches: [develop]
branches: [dev]
pull_request:
branches: [develop]
branches: [dev]

jobs:
test_core:
strategy:
matrix:
os: ['ubuntu-latest']
python-version: ['3.8']
runs-on: ${{ matrix.os }}
runs-on: ['ubuntu-latest']
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
- name: Login to GitHub Packages
uses: docker/login-action@v1
with:
python-version: ${{ matrix.python-version }}
- name:
Install dependencies
registry: docker.pkg.github.com
username: ${{ github.actor }}
password: ${{ secrets.GH_TOKEN }}
- name: Pull and run Docker image
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install "django-geojson [field]"
docker pull docker.pkg.github.com/edwardchalstrey1/seshat/tests-image:latest
docker run -d -p 5432:5432 -v ${{ github.workspace }}:/seshat -e DJANGO_SETTINGS_MODULE=seshat.settings.local -e POSTGRES_PASSWORD=postgres -e PGDATA=/var/lib/postgresql/data/db-files/ -e GITHUB_ACTIONS='true' --name seshat_testing docker.pkg.github.com/edwardchalstrey1/seshat/tests-image:latest
- name: Sleep, then check PostgreSQL connectivity
run: |
sleep 10
docker exec seshat_testing psql -h localhost -U postgres -c 'SELECT 1'
- name: Run tests
run: |
python manage.py test seshat.apps.core --keepdb
docker exec seshat_testing python3 /seshat/manage.py test seshat.apps.core --keepdb
22 changes: 22 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM postgis/postgis

# Update the package lists for upgrades for packages that need upgrading, as well as new packages that have just come to the repositories.
RUN apt-get update -y

# Install the packages
RUN apt-get install -y gdal-bin libgdal-dev libgeos++-dev libgeos-c1v5 libgeos-dev libgeos-doc

# Install pip
RUN apt-get install -y python3-pip

# Upgrade pip
RUN python3 -m pip install --upgrade pip

# Copy requirements.txt file into the Docker image
COPY requirements.txt .

# Install Python dependencies
RUN pip install -r requirements.txt

# Install django-geojson
RUN pip install "django-geojson[field]"
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,11 @@ Follow the steps on [docs/setup](docs/setup.md).
3. Checkout branch on changes on Azure VM and test there (see [Azure Setup](docs/setup.md#azure-setup)).
- ATI VM is set up currently under the `Sustainable Scholarly Communities around Data and Software` subscription
4. Merge branch into `dev` on this fork
5. Repeat the above until satisfied, then PR `dev` to upstream `dev` branch
5. Repeat the above until satisfied, then PR `dev` to upstream `dev` branch

## Tests and checks

On this fork, currently GH actions is set up to run django tests for the following apps when pushing or PR-ing to the `dev` branch:
- Core

See [docs/testing](docs/testing.md) on how to run locally.
10 changes: 9 additions & 1 deletion docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,12 @@
python manage.py test <app name> --keepdb
```
- Where `<app name>` is e.g. `seshat.apps.core` (or leave this off to run all tests)
- `--keepdb` ensures you can rerun tests quickly if the setup hasn't changed
- `--keepdb` ensures you can rerun tests quickly if the setup hasn't changed
## CI
GitHub actions is set up to run on this repo. It uses a custom Docker image that gets built on every push or PR to `dev` if the Dockerfile has changed. See `.github/workflows` and the `Dockerfile`. The tests (`.github/workflows/tests.yml`) and any subsequently introduced workflows should always run on push/PR to `dev`.
To set up pushing the docker image using the GH action workflow, I first did the following:
- Generated a new GitHub token with the `read:packages` and `write:packages` scopes. Under my `Settings > Developer settings > Personal access tokens` (classic token).
- Stored the GitHub token as a secret in the Seshat GitHub repository, `Settings > Secrets`, named `GH_TOKEN`.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ django-allauth==0.51.0
django-cors-headers==3.11.0
django-crispy-forms==1.14.0
django-debug-toolbar==3.2.4
django-environ==0.11.2
django-filter==21.1
django_geojson==4.0.0
django-heroku==0.3.1
Expand Down
6 changes: 3 additions & 3 deletions seshat/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@

#SOCIALACCOUNT_AUTO_SIGNUP = False

if not os.path.exists(local_env_path):
if not os.path.exists(local_env_path) and not os.getenv('GITHUB_ACTIONS') == 'true':
SOCIALACCOUNT_PROVIDERS = {
'google': {
'APP': {
Expand Down Expand Up @@ -198,7 +198,7 @@
# DATABASES['default'].update(db_from_env)

# Qing data database
if not os.path.exists(local_env_path):
if not os.path.exists(local_env_path) and not os.getenv('GITHUB_ACTIONS') == 'true':
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
Expand Down Expand Up @@ -255,7 +255,7 @@
LOCALE_PATHS = [BASE_DIR / "locale"]

# Email config BACKUP:
if not os.path.exists(local_env_path):
if not os.path.exists(local_env_path) and not os.getenv('GITHUB_ACTIONS') == 'true':
EMAIL_FROM_USER = config('EMAIL_FROM_USER')
EMAIL_HOST = config('EMAIL_HOST')
EMAIL_HOST_USER = config('EMAIL_HOST_USER')
Expand Down
45 changes: 31 additions & 14 deletions seshat/settings/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,39 @@
import environ
import os
import sys
# Initialise environment variables
env = environ.Env()
environ.Env.read_env()

#MIDDLEWARE.insert(0, "debug_toolbar.middleware.DebugToolbarMiddleware")
#MIDDLEWARE.insert(0, "debug_toolbar.middleware.DebugToolbarMiddleware")


# Databases
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': env('NAME'),
'USER': 'postgres',
'HOST': env('HOST'),
'PORT': env('PORT'),
'PASSWORD': env('PASSWORD')
# We use the local database for development and the GitHub Actions database for testing
if os.getenv('GITHUB_ACTIONS') == 'true':
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': 'github_actions',
'USER': 'postgres',
'HOST': 'localhost',
'PORT': '5432',
'PASSWORD': 'postgres'
}
}
else:

# Initialise environment variables
env = environ.Env()
environ.Env.read_env()

DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': env('NAME'),
'USER': 'postgres',
'HOST': env('HOST'),
'PORT': env('PORT'),
'PASSWORD': env('PASSWORD')
}
}
}

# Shapefile spatial stuff
GEOGRAPHIC_DB = True
Expand All @@ -32,7 +46,10 @@
GEOS_LIBRARY_PATH = '/opt/homebrew/opt/geos/lib/libgeos_c.dylib'
else: # linux
GDAL_LIBRARY_PATH = '/usr/lib/libgdal.so'
GEOS_LIBRARY_PATH = '/usr/lib/aarch64-linux-gnu/libgeos_c.so'
if os.getenv('GITHUB_ACTIONS') == 'true':
GEOS_LIBRARY_PATH = '/usr/lib/x86_64-linux-gnu/libgeos_c.so'
else:
GEOS_LIBRARY_PATH = '/usr/lib/aarch64-linux-gnu/libgeos_c.so'

django_settings_module = os.environ.get('DJANGO_SETTINGS_MODULE')

Expand Down

0 comments on commit 4c36480

Please sign in to comment.