deployment tasks that populate a python virtualenv

This is a common pattern for our deployment tasks:

  • checkout a git repo
  • pip install one or more requirements.txt files that exist in that repo to populate a virtualenv

Obviously we want to pip install when there is a new commit.

For the cases when the repo commit changes but the requirements.txt files do not change I would prefer not incur the cost of pip installing it. pip won’t make changes but it is still rather expensive to run through all of the requirements.

Here is one way to avoid the overhead:

  • git: (check out the repo)

  • shell: md5sum path/to/requirements.txt
    register: req_md5

  • stat: path=/var/tmp/requirements.txt.{{ req_md5.stdout }}
    register: req_stat

  • pip: requirements=path/to/requirements.txt virtualenv=/venv state=present
    when: not req_stat.stat.exists

  • file: path=/var/tmp/requirements.txt.{{ req_md5.stdout }} state=touch

At the very least I was thinking of creating an md5sum module to replace the first task since I hate having tasks report “changed” for read operations. Can anyone think of a better way to do this?

-John

Hi,

First time do a git clone and install a git hook post-checkout that does pip install and all other migrations. Next time just fetch and checkout new changes and the hook will get automatically triggered.

Greetings,
gw

T

The point here though is that I only want to re-populate the virtualenv if the requirements.txt file changes (not every time the repo changes). Is this possible with a git hook?