Skip to content

feat: service elasticsearch #2

feat: service elasticsearch

feat: service elasticsearch #2

Workflow file for this run

name: Common CI for Django APIs

Check failure on line 1 in .github/workflows/ci-django-api.yml

View workflow run for this annotation

GitHub Actions / .github/workflows/ci-django-api.yml

Invalid workflow file

(Line: 91, Col: 9): Unexpected value 'if'
on:
workflow_call:
inputs:
# Required parameters
python-version:
description: 'Python version to use'
required: true
type: string
postgres-major-version:
description: 'PostgreSQL major version to use (supported versions are 13 and 14)'
required: true
type: number
# Optional parameters
use-postgis:
description: 'Set to true to use the PostGIS extension'
required: false
type: boolean
default: false
elasticsearch-version:
description: 'Set version to use the Elasticsearch service'
required: false
type: string
default: ''
extra-commands:
description: 'Extra setup commands or checks to run before running tests'
required: false
type: string
working-directory:
description: 'Working directory for the tests job.'
required: false
type: string
default: '.'
workflow_dispatch:
env:
SECRET_KEY: topsecret123
DATABASE_URL: ${{ inputs.use-postgis == true && 'postgis' || 'postgres' }}://test_user:test_password@localhost/test_db
# Used by django-searchable-encrypted-fields
FIELD_ENCRYPTION_KEYS: abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789
jobs:
commitlint:
name: Check commit messages
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Run commitlint
uses: wagoid/commitlint-github-action@3d28780bbf0365e29b144e272b2121204d5be5f3 # v6.1.2
pre-commit:
name: Run pre-commit hooks
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Python ${{ inputs.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ inputs.python-version }}
- name: Run pre-commit
uses: pre-commit/action@v3.0.1
tests:
name: Run tests and SonarQube Cloud scan
runs-on: ubuntu-latest
defaults:
run:
working-directory: ${{ inputs.working-directory }}
services:
postgres:
# The workflow supports only the PostgreSQL and PostGIS versions used in Platta
image: ${{ inputs.use-postgis &&
(inputs.postgres-major-version == '14' && 'postgis/postgis:14-3.2-alpine' ||
inputs.postgres-major-version == '13' && 'postgis/postgis:13-3.2-alpine') ||
(inputs.postgres-major-version == '14' && 'postgres:14-alpine' ||
inputs.postgres-major-version == '13' && 'postgres:13-alpine') ||
'postgres:alpine' }} # This fallback is used only so that we don't error out before the validation step
env:
POSTGRES_USER: test_user
POSTGRES_PASSWORD: test_password
POSTGRES_DB: test_db
ports:
- 5432:5432
# Needed because the postgres container does not provide a healthcheck
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
elasticsearch:
if: ${{ inputs.elasticsearch-version != '' }}
image: docker.elastic.co/elasticsearch/elasticsearch:${{ inputs.elasticsearch-version }}
env:
discovery.type: single-node
options: >-
--health-cmd "curl http://localhost:9200/_cluster/health"
--health-interval 10s
--health-timeout 5s
--health-retries 10
steps:
- name: Validate provided PostgreSQL major version
working-directory: ${{ github.workspace }}
run: |
if [[ "${{ inputs.postgres-major-version }}" != "13" && "${{ inputs.postgres-major-version }}" != "14" ]]; then
echo "Error: Unsupported PostgreSQL major version provided. Supported versions are 13 and 14."
exit 1
fi
- name: Set up locale
working-directory: ${{ github.workspace }}
run: |
sudo locale-gen fi_FI.UTF-8
- name: Checkout
uses: actions/checkout@v4
with:
# Required by SonarQube Cloud
fetch-depth: 0
- name: Install system packages
run: |
sudo apt-get update
sudo apt-get install libpq-dev gettext
- name: Install system packages for PostGIS
if: ${{ inputs.use-postgis }}
run: |
sudo apt-get install gdal-bin
- name: Setup Python ${{ inputs.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ inputs.python-version }}
cache: pip
- name: Cache pip packages
uses: actions/cache@v4
env:
cache-name: cache-pip-modules
with:
path: ~/.pip-cache
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/requirements.txt') }}-${{ hashFiles('**/requirements-dev.txt') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
- name: Install Python dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt -r requirements-dev.txt
- name: Run extra commands
if: ${{ inputs.extra-commands != ''}}
run: ${{ inputs.extra-commands }}
- name: Compile translations
run: |
if find . -name "*.po" | grep -q .; then
python manage.py compilemessages
fi
- name: Check migrations
run: |
python manage.py makemigrations --dry-run --check
- name: Run tests
run: |
pytest -ra -vvv --cov=. --cov-report=xml
# Without this workaround, SonarQube Cloud reports a warning about an incorrect source path
- name: Override coverage report source path for SonarQube Cloud
run: sed -i 's@'$GITHUB_WORKSPACE'@/github/workspace/@g' coverage.xml
- name: SonarQube Cloud Scan
uses: SonarSource/sonarqube-scan-action@v5.1.0
with:
projectBaseDir: ${{ inputs.working-directory }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}