Django collectstatic keeps waiting when run through Github Action

Viewed 325

We are facing a very weird issue. We ship a django application in a docker container through Github Actions on each push. Everything is working fine except collectstatic.

We have the following lines at the end of our CD github action:

docker exec container_name python manage.py migrate --noinput
docker exec container_name python manage.py collectstatic --noinput

migrate works perfectly fine, but collectstatic just keeps on waiting if ran through the github action. If I run the command directly on the server then it works just fine and completes with in few minutes.

Can someone please help me figuring out what could be the issue?

Thanks in advance.

1 Answers

Now I am far from the most experienced but I did this recently and I have some suggestions of where to look. I'm definitely not the greatest authority though.

I wasn't using docker so I can't say anything about that. From the issues, I had here are some suggestions I can recommend to try.

Take note that all of this was for a self-hosted runner. Things would be very different otherwise.

  • Check to make sure STATIC_ROOT and MEDIA_ROOT variables are set correctly in the settings file.
  • If the STATIC and MEDIA root variables are environment variables make sure you are serving the correct environment variables file like a .env file which I used.
    • I used django-environ to serve my environment variables. From the docs, it says to have the .env file in the same directory as the settings file. Well if you are putting the project on a production server with github actions, you won't be able to put the .env file anywhere in the project because it will get overwritten every time new code is pushed.
    • So to fix that you need to specify the correct .env file from somewhere else on the server. Do that by specifying ENV_PATH.
    • Another resource that was helpful:
    • I put my .env file in a proj directory I made in the virtualenvironment folder for the project.
      • I don't know if it's a good place to put it but that's how I did it. I didn't find much great info online for this stuff. Had to figure out a lot on my own.
    • Make sure the user which is running the github action has permissions to read the .env file.
  • Also like .env file, if you have the static files being collected into the base directory of your project you might have an issue with github actions overwriting those files every time new code is pushed. If you have a media directory where the user uploads files to then that will really be an issue because those files won't get overwritten. They'll just disappear.
    • Now if this was an issue it shouldn't cause github actions to just get stuck on the collect static command. It would just cause files to get overwritten every time the workflow runs and the media files will disappear.
    • If you do change the directory of where the static and media files are located as stated before, make sure all the variables for the paths are correct in the settings file and the .env file.
    • You will also need to update the nginx config file for the static and media root directories if you used nginx. Not sure about how apache does this.
      • You can do that with this command:
      • sudo nano /etc/nginx/sites-available/myproject
      • Don't forget to restart the nginx server after doing that.
  • If you are writing static and media files at a different location from the base project directory on the server, also check permissions on those directories. Make sure the user running the github action has permissions to write to those directories. I suspect that might cause it to hang but it very well might just cause an error.
  • Check all the syntax in the github actions yml file. Make sure everything is correct and it's not hanging cause it had an incomplete command or something like that.

But yeah, that's some things I had to take a look at. Honestly, none of this might be relevant for you. All of these issues should cause an error somewhere for the most part.

I couldn't really offer many external resources for you to look deeper into this because I'm just speaking from personal experience.

Hope I could help.

Heres my github repo for the project I did: https://github.com/pkudlanov/personal-portfolio-django

I hosted it on digitalocean on a linux server using nginx and gunicorn.

Related