Deploy a GitLab runner on Clever Cloud

Philippe Charrière
4 min readJan 23, 2018

--

Clever Cloud (https://www.clever-cloud.com/) is a PaaS platform where you can easily deploy your WebApps with a simple git push of your source code (see https://www.clever-cloud.com/doc/clever-cloud-overview/add-application/).

But you can deploy something else than code. And on this rainy Sunday, I decided to deploy a GitLab runner on Clever Cloud. A GitLab runner is the executable that will make the builds of your project, will run the tests with each commits … and finally who will “discuss” with GitLab CI to give the results of builds, tests, … to the GitLab platform (GitLab.com or your own instance).

There are a lot of possibilities. But today I will limit myself to a simple runner: a “shell” runner, that will download the dependencies of my node.js project and run the tests.

You need a nodejs project

Here is an example of a project that you can use:

https://gitlab.com/wey-yu/hello

To start the project tests just run the command npm test

To enable continuous integration (the CI part) in your GitLab project, just add a .gitlab-ci.yml file to the root of your project:

It is now that we are going to need a runner.

Project setup for the use of a runner

Go to the settings of your project and choose CI/CD, then scroll down the Runners settings section (by clicking on the Expand button) and you will thus obtain 2 important parameters:

  • The GitLab instance url
  • A token that will allow the runner to “send” informations to your GitLab instance

So keep this token, it will be useful for the definition of our runner.

⚠️ Then, very important, you need to define a “Personal Access Token” in the settings of your GitLab profile (and keep this second token somewhere).

Runner creation on Clever Cloud platform

To create my runner on Clever Cloud I will use a Dokerfile and a shell script file that I would deploy as an application on Clever Cloud.

So, create a my-runnerdirectory with the following 2 files:

The Dockerfile will basically be used to install the gitlab-runner and nodejs with npm, then run the go.shfile:

What does go.sh do?

  • The script retrieves the list of runners in progress with the GitLab API (hence the necessity of the access token) and retrieves the id of the runner by doing a search on the description of the runner
  • Then we delete this entry (always with the API), if the list was empty it will generate an error (but it is not blocking)
  • Note: I am doing this deletion because each time we restart our application, we will create a new runner record, so it is to avoid unnecessary accumulation
  • Then the runner registers with the GitLab instance with the command gitlab-runner register
  • Then it starts with the gitlab-runner run command
  • And finally I launch an http server because the platform Clever Cloud expects to launch a WebApp listening on 8080 (otherwise the platform will refuse to launch my application)

Let’s deploy the runner on Clever Cloud 🚀

  • In the my-runner directory do a git init then a git add . and finally a git commit -m “add my runner”
  • Then go to the Clever Cloud platform and create a Docker application with the following environment variables:
  • The platform will provide you with a git url to add to your project to be able to deploy it (eg: git remote add clever git+ssh://git@push-par-clevercloud-customers.services.clever-cloud.com/app_8fe8296c-bad4-4467-8aee-5bc32f4c83bf.git)
  • So you can deploy: git push -u clever master
  • Wait a little
  • Once your deployment is complete, you can return to your GitLab project

And in the settings you will see that your runner is well registered:

Start a pipeline

Now go to the CI/CD section of your repository, select the sub-section Pipelines:

Click on the “Run Pipeline” button. You will then be able to create a pipeline:

So click on the “Create Pipeline” button. This will trigger the creation of the pipeline and start it starting with the installation of the dependencies:

By clicking on the job you are interested in (a step of the pipeline) you get the details of what has been executed by the runner:

From now, whenever you “push” commits to master branch, the pipeline will be triggered.

That’s it easy. Now you know how to deploy immutable GitLab runners on Clever Cloud 😃

--

--