‹‹ All posts

Build and deploy with CircleCI 2.0

29 of May, 2017


CircleCI opened its 2.0 beta a while ago (it’s still is beta at the time of writing) and I’ve got to try it out few weeks ago.

I did not experience technical issues with it, just some missing features - looks like it’s stable enough and very usable.

I am automating packaging and deployment of dockerised Scala application, which I described in my previous post. There are these two natural steps:

  1. Build and publish docker image;
  2. Trigger a deployment of that image into Google Container Engine (my Kubernetes cluster).

This could have been done with previous circle as well, but would require some heavy configuration and customisation of default ubuntu box, which would be quite annoying.

With new process it allows to use separate jobs for each step. In each we can use different images, which simplifies life a lot.

One slight irritation is that at the moment that the only way to transition from one job to another is sending a curl request to start it. So, for instance, on successful build deployment will be invoked by curling the next job. I’m sure there will be better way to do it later, but current workaround is well described in official documentation (it still took some poking around to realise that task sequencing is not default).

Once you join the beta, all you need is place a config in .circleci/config.yml in new format (see example below). All the other steps are the same as with v1.

version: 2
jobs:
  build:
    working_directory: /tmp/build
    docker:
      - image: spikerlabs/scala-sbt:scala-2.12.1-sbt-0.13.13
    steps:
      - checkout
      - setup_remote_docker
      - run:
          name: Run test
          command: sbt verify
      - deploy:
          name: Publish to Docker Hub
          command: |
            if [ "${CIRCLE_BRANCH}" == "master" ]; then
              apk add --no-cache docker
              docker login -u ${DOCKER_LOGIN} -p ${DOCKER_PASSWORD}
              sbt docker:publish
              curl --user ${CIRCLE_API_TOKEN}: \
                --data build_parameters[CIRCLE_JOB]=deploy_to_gce \
                --data revision=$CIRCLE_SHA1 \
                https://circleci.com/api/v1.1/project/github/$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME/tree/$CIRCLE_BRANCH
            fi
  deploy_to_gce:
    working_directory: /
    docker:
      - image: google/cloud-sdk
    steps:
      - run:
          name: Deploy to GCE
          command: |
            echo $GCE_SERVICE | base64 --decode --ignore-garbage > ${HOME}/gcloud-service-key.json
            export GOOGLE_APPLICATION_CREDENTIALS=${HOME}/gcloud-service-key.json
            gcloud auth activate-service-account --key-file ${HOME}/gcloud-service-key.json
            gcloud config set project pairing-buddy
            gcloud container clusters get-credentials pairing-buddy-2 --zone us-east1-d --project pairing-buddy
            kubectl rolling-update pairing-buddy-latest --image=asarturas/pairing-buddy:latest --image-pull-policy Always

Hope this helps, let me know what was your impressions of new circleci workflow.

comments powered by Disqus