Search code examples
djangodjango-compressor

Django-compressor not finding blocks to compress in a GitHub Action


I have a Django project that's using django-compressor to minify and concatenate CSS files, using offline compression. I haven't yet put it on a server, but when I run manage.py compress in a GitHub Action, before running tests, it can't find the {% compress %} block, even though it can find the template containing them.

I have a single template that uses CSS files, templates/myapp/layouts/base.html:

<!DOCTYPE html>
{% load compress static %}
<html lang="en">
<head>
  <!-- ... -->
  {% compress css file global %}
    <link rel="stylesheet" href="{% static 'oohdir/css/global/variables.css' %}">
    <link rel="stylesheet" href="{% static 'oohdir/css/global/reset.css' %}">
    <link rel="stylesheet" href="{% static 'oohdir/css/global/form.css' %}">
    <!-- etc -->
  {% endcompress %}

When running the site locally I can have these Django settings:

DEBUG = False
COMPRESS_ENABLED = True
COMPRESS_OFFLINE = True

and when I run manage.py compress it generates a single CSS file, as expected, in static_collected/CACHE/CSS/. That file is linked in the HTML when I view pages in the browser (using manage.py runserver --insecure).

But when I run my GitHub Action many tests fail because of a missing file, which django-compressor didn't generate.

If I run manage.py compress --verbosity 2 in the Action I can see it finds the correct template among all the others:

Found templates:
        ...
        myapp/inc/form_field.html
        myapp/layouts/base.html
        admin/tree_list.html
        ...

But it doesn't think there's anything to compress in it:

Compressing... done
Compressed 0 block(s) from 9 template(s) for 1 context(s).

That should be 1 block(s). I'm stumped.


Solution

  • Thanks to Dan's cheat sheet I found the problem: In my GitHub Action I was running manage.py compress before manage.py collectstatic - thinking that collectstatic would need to collect the generated, compressed, CSS file(s).

    But it needs to be the other way round, so that all the static files are collected in place before compress can do its work on them.

    So now my tests step looks something like this:

          - name: Run Tests
            run: |
              ./manage.py collectstatic --verbosity=0 --noinput
              ./manage.py compress --verbosity=0
              flake8 myproject
              black . --check
              coverage run ./manage.py test
            env:
              [all my env vars]