Using Wercker with Cloud Foundry

Wercker is a Docker-Native CI/CD Automation platform for Kubernetes & Microservice Deployments

Casey Rogers
Casey Rogers
May 4, 2017

Wercker has fantastic integrations with a number of clouds and services. Yet, I recently found it missing an integration with Cloud Foundry.

Given the flexibility of Wercker, however, this was a challenge I could easily solve. I built my own container to enable Wercker to deploy to Cloud Foundry and similar platforms, such as Bluemix.

Below, I’ll show you how.

 

Objectives

  • Build a container that has the CF toolset for Wercker to use for deployments.

 

Signing Up for Wercker

Signing up for Wercker is incredibly quick and easy, and the best part is that a lot of the functionality is available for free, so you can quickly and easily start testing with no risk.

Just head over to http://www.wercker.com, and log in using your GitHub account. Alternatively, you can click Create an Account, provide your name, a password, and your email address, and you will be up and running before you know it.

 

Building our Docker Container

Building Docker containers may sound daunting to those who have only used the ones provided on Docker Hub, but luckily, it is actually an incredibly simple process.

The first step is to create a clean working directory. (We could call it “wercker-cf” for instance.)

 

> mkdir ~/wercker-cf
> cd ~/wercker-cf
 

 

You can now create a file inside the directory called “Dockerfile,” which is basically a file that outlines all the commands we want to run, along with images to use, and what command to start the container with.

In it, we are going to put a number of lines. I will paste the output of the full file and then break down what each line is doing.

 

 FROM ubuntu:14.04 
MAINTAINER <your name>
RUN apt-get update
RUN apt-get install -y apt-transport-https ca-certificates curl wget
RUN apt-key adv --keyserver hkp://eu.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
RUN echo "deb https://apt.dockerproject.org/repo ubuntu-trusty main" | sudo tee /etc/apt/sources.list.d/docker.list
RUN apt-get update
RUN apt-get install -y docker-engine
RUN wget -q -O - 'https://cli.run.pivotal.io/stable?release=linux64-binary' | tar -xzf - -C /usr/local/bin
RUN cf install-plugin -f https://static-ice.ng.bluemix.net/ibm-containers-linux_x64

ADD cf.sh /cf.sh
RUN chmod +x /cf.sh

CMD /cf.sh

 

FROM - This line defines the base image we are going to use—In our case, Ubuntu 14.04.

MAINTAINER - This line is who to shout at when things go wrong! (Who built the container.)

Next we see a number of RUN lines. These are the commands we are running within the container. In summary, we are adding some repositories, installing Docker within the container, and installing Cloud Foundry, along with the IBM Container Bluemix plugin.

Finally, we are adding a script we will create by the name of “cf.sh” (but this could be any file in our working directory into the container). Then, we’ll make this script executable.

CMD - The final line we see is telling the container to run this command when it is launched.

Next, we will work on our cf.sh script. (This is a very basic script that will simply log you into CF/Bluemix and start up your container.)

You will see the use of a number of variables in this script. These should all be passed through by setting the appropriate environment variables within Wercker under your application or pipeline.

Again, I will paste out the contents of cf.sh, and then talk through what’s being done.

 

#!/usr/bin/env bash 
cf login -u $CF_USER -p $CF_PASS -o $CF_ORG -s $CF_SPACE -a $CF_API
cf ic init
cf ic run -p $CF_PORT ${CF_REG}/${CF_CONTAINER} $LAUNCH_CMD

 

Here, we are simply logging into IBM Bluemix. We are using a number of variables which we set in Wercker.

CF_USER - This is the username to use to log in.

CF_PASS - The password to log in.

CF_ORG - The organization in Bluemix we wish to use.

CF_SPACE - The space under the organization in Bluemix we wish to use.

CF_API - The API endpoint, as we are using a non-standard one. This can be optional.

Next, we run an “init.” This initializes our certificates and makes our environment ready for use.

Lastly, we run a “run”—This is starting our container in Bluemix. (Again, we use a number of variables we set earlier.)

CF_PORT - The port we want our instance to listen on.

CF_REG - The Docker registry we will pull our container from.

CF_CONTAINER - The container name we wish to deploy, including tag.

Now that we have created both our Dockerfile and cf.sh script, we can simply run a “Docker build” in that directory to build the image. Once this is complete, we can tag it and push it to Docker Hub. Instructions for doing so can be found here: https://docs.docker.com/engine/getstarted/step_six/.

Once we have pushed this, we can head over to our wercker.yml to use it.

This is very easily done, and is best demonstrated with an extract of yaml from the Wercker file.

 

 dev:
   Box: username/containername
   steps:
     - script:
        name: CF Script
        code: /cf.sh

 

Here we are simply adding a step in our “dev” pipeline to use the box we just built, and executing the cf.sh script we created on the container. The rest of the work is handled by the script we put inside the container. When this step is run, it will launch the container and run our script, passing through all the environment variables we set in Wercker.

 

Summary

As you can see, being able to build and use your own containers with Wercker means there is very little that Wercker cannot do. Its extensibility makes it a fantastic tool—especially in the age of DevOps, when agility is everything.

 

About the Author

Casey Rogers is an experienced Senior Infrastructure Engineer with more than eight years of experience working with some of the UK’s biggest companies and multinationals. Largely self-taught, Casey has long had an interest in IT and has been brought up around technology. In his spare time, Casey is always tinkering with new technologies and experimenting with new ways to do things.

 

Like Wercker?

We’re hiring! Check out the careers page for open positions in Amsterdam, London and San Francisco.

As usual, if you want to stay in the loop follow us on twitter @wercker or hop on our public slack channel. If it’s your first time using Wercker, be sure to tweet out your #greenbuilds, and we’ll send you some swag! 

 

Topics: Tutorials