Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heroku release phase commands are not executed

I try to deploy a django application on heroku using a build manifest. The application seems to be deployed correctly, but the commands in the release phase do not seem to be executed.

This is my heroku.yml:

build:
  docker:
    web: Dockerfile
release:
  image: web
  command:
    - python manage.py migrate
    - python manage.py collectstatic --noinput
run:
  web: gunicorn hello_django.wsgi:application --bind 0.0.0.0:$PORT

This is my Dockerfile:

FROM python:3.9-slim

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONBUFFERED 1

RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        postgresql-client \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /usr/src/app

# install dependencies
RUN pip install --upgrade pip
COPY requirements.txt ./
RUN pip install -r requirements.txt

# copy source code to container
COPY ./src .

# create directory for statics
RUN mkdir staticfiles

The commands specified in the release phase are not executed. I know that, because the database is not being migrated and the staticfiles directory is empty. I also know that the rest of my application is actually configured correctly, because when I include this line in my Dockerfile at the end:

RUN python manage.py collectstatic

then the statics are collected and the application is running.

I also installed the plugin heroku-manifest with this command: heroku plugins:install @heroku-cli/plugin-manifest and set the stack of my app to container with heroku stack:set container -a <app name>

What am I missing?

Update

actually it seems the migrate command is executed, but not collectstatic. Why is that?

like image 973
movileanuv Avatar asked Sep 06 '25 03:09

movileanuv


1 Answers

I had the same issue and came up with a partial solution. I was able to get the collectstatic command to run by using the && command. Here's what your heroku.yml file would look like.

build:
  docker:
    web: Dockerfile
release:
  image: web
  command:
    - python manage.py migrate && python manage.py collectstatic --noinput
run:
  web: gunicorn hello_django.wsgi:application --bind 0.0.0.0:$PORT

My guess is that only one command is allowed but it's just a guess. I checked the documentation for release on heroku but it didn't say anything like that. Although, in their example only one command was used.

Anyway, using && worked for me and I can see from the release logs that the collectstatic command was executed.

However: Even though the collectstatic command was executed, my newest release did not include any of the style changes I had made. So I still ended up having to run collectstatic before pushing the code to Heroku. I still don't have a solution for how add the collect static step into an automated workflow.

Update: After digging around some more I think the best option for me is to add collectstatic to github actions. As mentioned in the comment by Eduardo it seems that the release phase of Heroku does not persist the collected files data...

like image 188
MattB09 Avatar answered Sep 07 '25 20:09

MattB09