3 min read, 500 words

Cookiecutter-flask and GitLab-CI

I frequently use cookiecutter-flask for small personal projects and as I am kicking around ideas for my latest project, I thought it made sense to get a baseline project for my specific needs and to then document the process so I don’t forget to use it again next time.

I started out by tackling Gitlab CI, wanting to mimic as much of the stock Travis-CI config as possible. This meant understanding the gist of how the Travis configuration works and mapping those concepts to the gitlab-ci.yaml format. Tasks out of the box include running tests on multiple versions of python (2.7, multiple 3.x), linting, building webpack assets, pulling in node dependencies, etc. Furthermore I also wanted this to generate a deployable artifact, giving me a bit more control over what is actually running when I deploy things.

Rather than taking a stock python image and installing everything needed to build, I decided to go ahead and build some custom docker images in another project that would basically take the base python:x.y images and build in a 10.x node runtime. This turned out to be relatively trivial, at least for my use case. Basically take the stock python image, have it run node’s Debian repo configuration script and apt-get install nodejs. I threw some tags on it and I had tagged images for 2.7, 3.4, 3.6, all building in parallel. Well, not really, but if my runner was configured right they would be. Details. These images were all pushed to my GitLab registry for future consumption.

This is the resulting .gitlab-ci.yml for just the testing phase:

variables:
    FLASK_APP: "autoapp.py"
    FLASK_DEBUG: "1"
    PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache"

cache:
  paths:
    - .cache/pip
    - venv/
    - node_modules/

before_script:
  - pip install virtualenv
  - virtualenv venv
  - source venv/bin/activate
  - cp .env.example .env
  - pip install -r requirements/dev.txt
  - npm install
  - npm run lint
  - npm run build
  - flask lint

test:2.7:
    image: $CI_REGISTRY/dlloyd/cookiecutter-flask-docker:2.7
    script:
        - flask test

test:3.4:
    image: $CI_REGISTRY/dlloyd/cookiecutter-flask-docker:3.4
    script:
        - flask test

test:3.6:
    image: $CI_REGISTRY/dlloyd/cookiecutter-flask-docker:3.6
    script:
        - flask test

Basically, setting up the environment in each case is identical (for now) outside of picking which image to base from so I was able to put most of the activity in the before_script section. When reading the docs on gitlab-ci.yml syntax, I discovered that I should be able to move even the flask test script into a base state and use extends, but after testing discovered that the docs cover features that are still in release candidate state and 11.3 has yet to ship.

Note: for the purposes of getting a quick and easy environment up, this just uses the example .env file when running everything, obviously as the environment becomes more complex this should be switched to utilize an actual test environment.

Anyway, this is only a first stab at getting everything where I want it, there will obviously be more work down the road as I start to flesh out an actual application and bring in more of the python ecosystem.


Creating Debian packages for reproducible Node installations Haproxy, HHVM, and Varnish


comments powered by Disqus