For private repositories managed on Github and Bitbucket, we will implement CI (Continuous Integration) and CD (Continuous Delivery). Here, I’ll note how to automate testing and deployment using wercker. wercker supports both Github and Bitbucket private repositories, and is currently available for free.

The deployment target is heroku. heroku is basically free and allows you to easily use a PaaS environment. However, when using the free tier, note that it goes to sleep if there’s no access for 30 minutes, and operating time is limited to 1,000 hours per month.

The assumed application is a web service implemented with Python3 and Flask (a web application framework for Python).

First, add three configuration files (wercker.yml, Procfile, runtime.txt) to the repository. wercker.yml is the configuration file used by wercker, and is broadly composed of two sections: build and deploy. The build section describes the installation of dependencies and the execution of unit tests. On the other hand, the deploy section specifies the name of the SSH key pair to reference when deploying to heroku (key-name: HEROKU). The procedure for creating an SSH key pair named HEROKU is described later. Also, to use the latest version of the heroku CLI, write install-toolbelt: true. In Procfile, write the startup command in the format application name: command. Furthermore, specify the Python version in runtime.txt. Note that if not specified, Python 2 series will be installed.

# wercker.yml
box: python
services:
build:
    steps:
        - script:
            name: install dependencies
            code: |
                pip3 install -r requirements.txt                

        - script:
            name: test
            code: |
                python3 -m unittest discover -v && flake8 .                
deploy:
    steps:
        - heroku-deploy:
            install-toolbelt: true
            key-name: HEROKU
# Procfile
web: python -m web.main
# runtime.txt
python-3.6.1

Next, in wercker’s web UI, configure the linkage between wercker and the repository, and set up the pipeline for CI. After registering as a wercker user, add an application from the dashboard and link it to the repository. After adding the application, select the Workflows tab and add deploy from Add new pipeline. Furthermore, on the web UI, connect deploy immediately after build so that build and deploy run consecutively when pushed to the master branch. Also, to log in to heroku during deploy execution, generate an SSH key pair from the Generate SSH Keys button on the Environment tab, and store it as environment variables (HEROKU_PRIVATE and HEROKU_PUBLIC). In addition, from the Environment tab, register an environment variable that specifies the heroku application name (variable name is HEROKU_APP_NAME, value is the heroku application name).

Finally, from heroku’s web UI, register the SSH public key generated by wercker to heroku. When pushed to master, if build to deploy works properly, it’s complete. Looking at other reference pages, it says that heroku’s API KEY is necessary, but for this application, looking at wercker’s Environment tab or Pipeline environment variables in the Workflows tab, there was no such description. However, I remember setting something using the API KEY, so I probably need it. But I don’t remember where I set it…