In my previous post I described how to manually deploy a docker image to the Heroku container registry, in this post I will take a look at how to automate this process by using a continuous integration service. The goal is that with every commit to the master branch, a CI server should build a new docker image and push it to Heroku.
In this case I will use CircleCI, and after creating an account and linking it to the project's repo in bitbucket, you need to add a configuration YAML file under a folder named '.circleci' to your repo. What follows are the contents of the config.yml and a description of each section.
version: 2 jobs: build: branches: only: - master machine: true steps: # checkout source code - checkout # build image - run: | docker info docker build -t daycare-app . # deploy the image - run: | docker login --username=$HEROKU_USERNAME --password=$HEROKU_API_KEY registry.heroku.com docker tag daycare-app registry.heroku.com/$HEROKU_APP_NAME/web docker push registry.heroku.com/$HEROKU_APP_NAME/web # release the image - run: | heroku -v heroku update heroku container:release web -a $HEROKU_APP_NAME
- Line #6 tells CircleCI to only build the image for commits from the master branch.
- At line #13 the meat of the build process starts, first running docker build and tagging the image. This will pickup the Dockerfile from the root directory that was described in a previous post.
- Line #18 runs a series of commands to upload the image to Heroku. First, signs in to the heroku registry by using a username and API key from environment variables. I will go over how to define these shortly. Then, the image is pushed into the heroku registry using another environment variable for the application name as defined in Heroku.
- Line #24 releases the image so that it is made public in Heroku. This turned out to be more of a hack, the only way I could figure out how to release the image was to use the Heroku CLI (my attempts to use a simple curl request failed). Luckily, CircleCI build machines already have the HerokuCLI installed, BUT it is an older version. So first the HerokuCLI is updated on the build machine and then we can call the release command.
Now, regarding those environment variables, head over to your account settings page in heroku to generate an API KEY:
Then, in CircleCI open the build project settings and add the environment variables:
That's all there is to it. Commit the config file to your repo, and watch as CircleCI starts the build and deploys the image to Heroku:
You can see the website hosted in Heroku here or browse the sources at the project page.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.