Eldar Khaitov Eldar Khaitov - 1 month ago 6
Git Question

How to save uploaded files in git repository after pushing?

I use Openshift. It uses git - you just push and the server makes all deployments. Then I made an app that uploads pictures. Obviously, I upload them in git repo and after push all the pictures erase. What is the best practice to save pictures?

I tried:

1) pulling before pushing - no sense

2) gitignoring /media folder - no sense

I could try:

1) to deploy another service for photos only. My friends say it is weird decision and I will have the same problem if I want update that new server

2) somehow save the photos outside git repo but I have no idea how to implement it and no idea whether it is a good practice

Any adviŅe? Looking forward to reading you.

Answer

When you push any git, it is saved in a special directory which is stored in environment variable OPENSHIFT_REPO_DIR. This directory always holds currently deployed version of the application.

Now, Openshift has other special directories which you can access via following environment variables OPENSHIFT_HOMEDIR, OPENSHIFT_DATA_DIR, OPENSHIFT_TMP_DIR, OPENSHIFT_LOG_DIR. OPENSHIFT_DATA_DIR is the persistent data directory. Contents of this directory don't get erased after every git push. You should always save files in this directory, and this is a good practice. This is how Openshift works.

If your application handles uploaded images, modify the application code to save it in the data directory. You can use OPENSHIFT_DATA_DIR environment variable to get the data directory location. How you can access environment variables in your application, will depend on the programming language you used.

in php,

$data_dir = getenv('OPENSHIFT_ENV_VAR');

in node.js

var dataDir = process.env.OPENSHIFT_DATA_DIR

For a list of environment variables used by Openshift, see here.

Comments