Gitlab runner deletes some files/folders on the server - django

I'm deploying a Django project on Centos 7 server using gitlab runner.
Each time I commit and push the project, gitlab runner tries to remove folders (such as the media folder) that are not in the gitlab repository(are on .gitignore).
I don't want gitlab runner to delete media files.
How can I ignore deleting some files and folders when gitlab runner starts its job?
Thanks in advance.

I found the answer.
I've put the media folder inside the project in the root static/media,
so every time gitlab runner deletes all the files and folders that are not in the project directory in the gitlab (also the media folder).
Simply, I put that media folder outside of the project.
former MEDIA_ROOT in settings.py:
MEDIA_ROOT= os.path.join(BASE_DIR, 'static/media/')
changed it to:
MEDIA_ROOT = '/home/gitlab-runner/builds/path-to-some-folder-outside-of-the-project-root/media/'
former nginx configuration:
location /media/ {
alias /home/gitlab-runner/builds/path-to-my-project/static/media/ ;
}
changed to:
location /media/ {
alias /home/gitlab-runner/builds/path-to-some-folder-outside-of-the-project-root/media/ ;
}
Hope it helps anybody that has this problem.

Related

Where do you keep your media folder in a Django project?

I'm wondering where do you Django developers keep your media folder. I'm currently developing a project located at ~/apps/django-project. I am not using continuous delivery at the moment, but when I have to update the project, I ssh straight into the remote machine and pull the updated files using git (that's because it is placed in the project folder). The problem is that the media folder is always updating and I'll have to track them using git, which is not something I want to do. I don't think I'd like to .gitignore them too.
Would it be a good idea to structure the project like that?
Project: ~/apps/django-project
Media: ~/media
Statics: ~/static
If that's a good idea, could you give me a hint about how to set up my settings.py MEDIA_ROOT and STATIC_ROOT in order to accomplish this?
If that's not a good idea, I'd like to hear from you how would one structure the project and what other good principles should I take into account. (also, development / production tips are welcome)
Django3 with Python3.7
I create a folder public in my root directory and there I add the media and static directories
public/
media/
static/
I also add the specific paths to .gitignore, so they don't conflict between environments.
public/media
public/static
The good thing about this approach is that if you are using a webserver like nginx or uwsgi you can set the document root to public and serve any static file by default and resolve to django any other path that is not a file in public.
For example in nginx I do something like this.
server {
root /var/sites/myproject/public;
location #djangoapp {
proxy_redirect off;
proxy_pass http://localhost:8000;
}
location / {
try_files $uri #djangoapp;
}
}
This is very convenient because is easy to reason about public, everything that is in that folder will be served statically. For example I put there my robots.txt file and some others that I know they just need to be simple serve. It defaults nicely and really fast to django for any other request that is not an static file in public
public/
media/
attachs/
users/
static/
admin/
css/
js/
robots.txt
humans.txt
manifest.json
favicon.ico
I even once added support for .php files where I just put the files there and add the .php setup as an extension rule in nginx.

django static files not appearing with virtual environment

So I'm trying to push my website into production, but I have encountered a problem while in the virtual environment, where my static files are not being found (404 errors).
In the settings.py file, I have STATIC_ROOT = os.path.join(BASE_DIR, 'static/').
In the file /etc/nginx/sites-available/django, I have modified the static files location, to look like this:
location /static {
alias /home/user/myproject/static;
}
In the directory /home/user/myproject, I have another directory called static, and inside are several directories holding the actual static files.
i.e. /home/user/myproject/static/shopApp/shop_app.css
or
i.e. /home/user/myproject/static/officeApp/office_app.css
What I am trying to see is if any of my configurations are set up badly. I am very new to django and web development, so I would appreciate any of your help!
EDIT: After doing some research I think I have the same problem as the guy from stackoverflow in this link: Fetching static files failed with 404 in nginx.
I think the second answer might solve my issue but I don't know where to apply the command chown www-data:www-data .
STATIC_ROOT only for prod, static folder for you app or project. PROD static != project static. For example you have 10 apps, like django admin. Without STATIC_ROOT and collectstatic command, you need point to each static folder in each app.
For example
location /static/admin {
alias /home/user/myproject/env../admin/static;
}
location /static/someapp {
alias /home/user/myproject/someapp/static;
}
But Django does a lot of work for you. You need to point to folder, which collects all static from all apps. For example STATIC_ROOT = os.path.join(BASE_DIR, 'static_remote/') and run collectstatic
location /static {
alias /home/user/myproject/static_root;
}
It turns out, that the problem was with my settings.py file.
Specifically, when your still using the django development server with your virtual environment (0.0.0.0:8000), you should have DEBUG set to True, so that it can serve your static files.
Remember! This is only if your using a django development server. For production, it should be set to False.
I got this information from this django article: https://docs.djangoproject.com/en/2.0/howto/static-files/

Django, Create-react-app, Heroku

I have 3 folders in a Django web application. The folders are as follows: the folder that contains settings.py(project), the folder that contains models.py(application), and a folder that contains a front end react application created by create-react-app.
I would like to build the react front end, copy the build artifacts into a static folder and then run the django application on heroku, but they have made this process practically impossible with my current folder structure. The alternative is to flatten the react application out and have build, src, node_modules, packagejson etc etc etc all at the root of the project, but this seems really bad.
Some config in settings.py:
STATICFILES_DIRS = (
os.path.join(PROJECT_ROOT, 'static'),
os.path.join(BASE_DIR, 'front-end/build/static')
)
What I run locally inside front-end:
npm run build
What I'm returning from views:
def index(request):
return HttpResponse(loader.get_template('build/index.html').render())
#above line returns index.html that is generated by npm run build
How do I deploy the project described above to Heroku so it can find all static resources?
You can use a symlink link that puts your react's build output into your django directory
For example if your directory structure is like this:
~/project_root/
~/project_root/django/static/
~/project_root/my_react_app/build
cd to ~/project_root/django/static/ and run
ln -s ../../my_react_app/build .
This will create ~/project_root/django/static/build that points to ~/project_root/my_react_app/build

Heroku deleting files upon git push?

I have a media folder that stores all my uploaded images during development and I'm pushing my django(1.5.1) project to a dev server on heroku. Inside the media folder I have
media/
# cache and images were commited before .gitignored was added
cache/ # store thumbnails
images/ # store images
.gitignore
the .gitignore has
*
!.gitignore
The problem is whenever I git push to heroku, all my testing uploads are wiped out by git. Is there a way to deal with this?
This isn't an issue with Git, but rather with Heroku. Heroku's file system is ephemeral, and is reset between deploys. Use a service like Amazon S3 to store uploaded files.

Serving static admin files for Django 1.5 in a shared hosting environment

I'm trying to get a fresh installation of Django 1.5 running on an Apache-Server. The WebServer is situated on a shared hosting platform called uberspace.de
which means I have no access to the Apache configuration itself I can however write .htaccess files if that's any help at all. Django is deployed via fast-cgi which is working as expected.
Whats not working however is the access to static files on the server like the .css files and graphics for the Django administration interface.
As mentioned in the official docs I used the following command to copy the static Files into my ~/html/static directory.
manage.py collectstatic
And these are the values from my settings.py:
STATIC_ROOT = '/home/bier/html/static/'
STATIC_URL = '/static/'
All I get is the infamous django 404 page when I try to access any of these Files.
I also followed the 'How to install and deploy Django' guide on my Webhosters Website to the letter. (sorry its only available in german I believe)
I already contacted the webhosters support but they don't know whats wrong.
All the solutions I've come up with so far suggest setting some sort of Alias in the Apache configuration. Which I can not do.
I'm thankful for any ideas you might have.
Try using a full address instead.
STATIC_ROOT = '/home/bier/html/static/'
STATIC_URL = 'http://www.mysite.com/static/'
Edit: Perhaps you could ask your host to setup /static/ in your Apache config:
sudo nano /etc/apache2/sites-enabled/mysite.com and add:
Alias /static/ /home/bier/html/static/
I've had a situation before where I've had to upload /static/ files manually because of a highly restrictive host (permissions). Perhaps you need to download a copy of django to your desktop and then upload the static admin file set into your /static/ directory manually?
Lastly, have you added the static files to your urls?
url(r'^static/(?P<path>.*)$','django.views.static.serve',{'document_root': '/home/bier/html/static'}),
I have more simpler solution. you need to create a directory named, let's say 'x' in "public_html" or similar location from which server serves the files by default.
Then upload all static files in directory x. (this can be done by running collectstatic locally and then upload all contents of directory STATIC_ROOT to x)
Then, change your STATIC_URL and STATIC_ROOT as follows:
STATIC_URL = '/x/'
STATIC_ROOT = os.path.join(BASE_DIR, '../public_html/x') # Path to folder