Search code examples
djangopython-3.xweb-deploymentwebfaction

Django Deployment Process to Webfaction.com


Trying to streamline a deployment process to webfaction.com for my django application, I have a master (working copy) and a development branch.

currently I'm doing the following:

  1. Make changes to my development branch in my local dev environment
  2. When changes are working, test with run local server, then merge with my master branch
  3. git push so the code is in my remote repo (this has other issues such as passwords, keys etc which I've not quite solved yet) (also i dont believe its possible to scp code to webfaction and I'm not really a fan of any of the FTP services I've used so far)
  4. SSH into my webfaction server and do a git pull and git merge
  5. Test to see if everything is still working (it never is)
  6. Make anychanges required to get everything working again
  7. commit any changes I've had to do to fix everything then push back to the remote repo
  8. Go back to my development environment and sync the code up with the production code
  9. Rinse Repeat for the next feature

obviously I've missed the efficient development train, for the record I've only been working with django for a couple of months as a hobby project.

Can anyone suggest a django deployment process that would be more conducive to sane development?


Solution

  • I would strongly suggest Fabric to handle your deployments to WebFaction: http://docs.fabfile.org/en/1.11/tutorial.html

    By using Fabric you can deploy code and do other server side operations from your local terminal with no need to manually ssh to the server. First install Fabric:

    pip install Fabric
    

    Create fabfile.py in your project root folder. Here is an example fabfile that can get you started:

    from fabric.api import task, env, run, cd
    from fabric.context_managers import prefix
    
    env.hosts = ('wf_username@wf_username.webfactional.com',)
    env.forward_agent = True
    
    MANAGEPY = '~/webapps/my_project/code/my_project/manage.py'
    PY = '~/webapps/my_project/env/bin/python2.7'
    
    @task
    def deploy():
        with cd('~/webapps/my_project/code/'):
            with prefix('source production'):
                run('git pull --rebase origin master')
                run('pip install -r requirements.txt')
                run('{} {} migrate'.format(PY, MANAGEPY))
                run('{} {} collectstatic --noinput'.format(PY, MANAGEPY))
                run('touch my_project/my_project/wsgi.py')
    

    You can run fab task from your terminal with:

    fab deploy
    

    In my opinion, making code changes directly on server is a bad practice. Fabric can improve your development flow so that you make code edits only locally, quickly deploy them and test them.