Skip to content

Commit

Permalink
Merge pull request #132 from SADiLaR/ci/lighthouse
Browse files Browse the repository at this point in the history
chore(ci): set up Lighthouse auditing in CI
  • Loading branch information
Restioson authored Oct 28, 2024
2 parents 023fb81 + 6ea1ad2 commit a9d95a4
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 10 deletions.
13 changes: 13 additions & 0 deletions .env.lhci
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
SECRET_KEY='django-insecure-w!h85bp^$$e8gm%c23r!0%9i7yzd=6w$$s&ic+6!%306&kj8@k*5'
DEBUG=''
DB_HOST=localhost
ALLOWED_HOSTS=localhost
DB_PORT=5432
DB_NAME=term_db
DB_USER=sadilar
DB_PASSWORD=sadilar
LOGGING_FILE=/home/runner/work/term_platform/term_platform/app/debug.log
LOGGING_HANDLERS_LEVEL=INFO
LOGGING_LOGGERS_LEVEL=INFO
LOGGING_LOGGERS_DJANGO_LEVEL=INFO
TESTING_DIR=/home/runner/work/term_platform/term_platform/app/general/tests/files/
58 changes: 56 additions & 2 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ jobs:
- 5432:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: 3.12
- name: Install Dependencies
run: |
python -m pip install --upgrade pip
Expand Down Expand Up @@ -63,3 +65,55 @@ jobs:
run: |
cd app/
python manage.py check
lighthouse:
runs-on: ubuntu-latest # operating system your code will run on
services:
postgres:
image: postgres:16
env:
POSTGRES_USER: sadilar
POSTGRES_PASSWORD: sadilar
POSTGRES_DB: term_db
ports:
- 5432:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: 3.12
- uses: actions/setup-node@v4
with:
node-version: 20
- name: Install Dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements-test.txt
sudo apt-get install -y gettext
npm install -g @lhci/[email protected]
- name: Setup LHCI env
run: cp .env.lhci app/.env
- name: Run Django fixtures
run: |
cd app
python manage.py migrate --no-input
python manage.py loaddata fixtures/institution.json
python manage.py loaddata fixtures/language.json
python manage.py loaddata fixtures/projects.json
python manage.py loaddata fixtures/subjects.json
python manage.py import_documents general/tests/files/
- name: Run Lighthouse
run: cd app && lhci autorun
- name: Archive Lighthouse results
if: always() # Ensure results are uploaded on failure too
uses: actions/upload-artifact@v4
with:
name: lighthouse-report
path: app/.lighthouseci/
include-hidden-files: true
- name: Archive Django logs
if: always() # Ensure logs are uploaded on failure too
uses: actions/upload-artifact@v4
with:
name: django-logs
path: app/debug.log
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ app/media/
/logging/
/pdf_uploads/
/pdf_upload_completed/

# Generated Lighthouse CI artifacts
.lighthouseci/
6 changes: 5 additions & 1 deletion Dockerfile-dev
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@ RUN <<EOF
set -exo pipefail
apt-get update
apt-get -y upgrade
apt-get install -y --no-install-recommends libmagic1 gettext graphviz graphviz-dev chromium
apt-get install -y --no-install-recommends libmagic1 gettext graphviz graphviz-dev chromium npm
npm install -g @lhci/[email protected]
pip install --upgrade pip
pip install -r requirements-dev.txt
EOF

# Used in LHCI
COPY entrypoint.sh /

# Copy project
COPY ./app /app/

Expand Down
9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ ruff-fix:
@docker compose run --rm web ruff check --fix .

load-fixtures:
@docker compose run --rm web python manage.py loaddata fixtures/language.json
@docker compose run --rm web python manage.py loaddata fixtures/institution.json
@docker compose run --rm web python manage.py loaddata fixtures/projects.json
@docker compose run --rm web python manage.py loaddata fixtures/language.json
@docker compose run --rm web python manage.py loaddata fixtures/subjects.json

pre-commit-install:
Expand All @@ -96,8 +96,11 @@ dev-quick-install:
echo "Creating superuser"
@make create-super-user

dev-mass-pdf-upload:
@docker compose run --rm web python manage.py dev_pdf_mass_upload
dev-import-documents:
@docker compose run --rm web python manage.py import_documents general/tests/files/

lighthouse:
@docker compose run --rm web lhci autorun

dev_update_vector_search:
@docker compose run --rm web python manage.py dev_update_vector_search
Expand Down
3 changes: 2 additions & 1 deletion app/fixtures/projects.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"logo": "",
"start_date": "2020-01-01",
"end_date": "2020-01-01",
"institution": 2
"institution": 2,
"languages": [1, 2, 3]
}
}
]
4 changes: 3 additions & 1 deletion app/general/management/commands/import_documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from django.core.files.base import ContentFile
from django.core.management.base import BaseCommand

from general.models import Document
from general.models import Document, Language
from general.service.extract_text import GetTextError, pdf_to_text


Expand Down Expand Up @@ -50,5 +50,7 @@ def save_data(self, file_path, file_name):
institution_id=random.randint(1, 20),
)
instance.save()
instance.languages.set(Language.objects.all())
instance.save()
except GetTextError as e:
print(f"Error: {e}")
31 changes: 31 additions & 0 deletions app/lighthouserc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module.exports = {
ci: {
collect: {
// We don't care about variable page performance metrics in CI - just static asserts
numberOfRuns: 1,
startServerCommand: "../entrypoint.sh",
url: [
"http://localhost:8000",
"http://localhost:8000/search/",
"http://localhost:8000/institutions/",
"http://localhost:8000/projects/",
"http://localhost:8000/documents/",
"http://localhost:8000/languages/",
"http://localhost:8000/subjects/"
]
},
assert: {
"preset": "lighthouse:recommended",
"assertions": {
// Gunicorn doesn't do this, but the reverse proxy in front of it does
"uses-text-compression": false,

// We don't think these are worth worrying about for now
"unminified-javascript": false,
"unused-css-rules": false,
"unminified-css": false,
"render-blocking-resources": false,
}
}
}
}
2 changes: 1 addition & 1 deletion app/templates/app/documents.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ <h2>
</div>
<p class="limit-text-width">
{{ item.description|truncatewords:30 }}
<a href="{% url 'document_detail' item.document.id %}">{% trans "Read more" %}</a>
<a href="{% url 'document_detail' item.document.id %}">{% trans "More about the document" %}</a>
</p>

{% if item.languages %}
Expand Down
2 changes: 1 addition & 1 deletion app/templates/app/projects.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ <h2>
<div>
<p class="limit-text-width">
{{ item.description|truncatewords:30 }}
<a href="{% url 'project_detail' item.project.id %}">{% trans "Read more" %}</a>
<a href="{% url 'project_detail' item.project.id %}">{% trans "More about the project" %}</a>
</p>

{% if item.languages %}
Expand Down
Empty file modified entrypoint.sh
100644 → 100755
Empty file.

0 comments on commit a9d95a4

Please sign in to comment.