diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 000000000..87b344d79 --- /dev/null +++ b/.github/workflows/build.yml @@ -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 \ No newline at end of file diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 9d2b93a06..69e2e68bb 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -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 \ No newline at end of file + docker exec seshat_testing python3 /seshat/manage.py test seshat.apps.core --keepdb diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..19620a997 --- /dev/null +++ b/Dockerfile @@ -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]" \ No newline at end of file diff --git a/README.md b/README.md index f8834abef..8edd87751 100644 --- a/README.md +++ b/README.md @@ -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 \ No newline at end of file +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. \ No newline at end of file diff --git a/docs/testing.md b/docs/testing.md index dee0c8902..13ca5a46c 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -19,4 +19,12 @@ python manage.py test --keepdb ``` - Where `` 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 \ No newline at end of file + - `--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`. \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index f117b95b1..b9483eb3b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 diff --git a/seshat/settings/base.py b/seshat/settings/base.py index 21b2470f5..9ac7fd941 100644 --- a/seshat/settings/base.py +++ b/seshat/settings/base.py @@ -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': { @@ -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', @@ -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') diff --git a/seshat/settings/local.py b/seshat/settings/local.py index dddca1cbd..3ccf0244a 100644 --- a/seshat/settings/local.py +++ b/seshat/settings/local.py @@ -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 @@ -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')