Using Gitlab CI with a Clojure project

Gitlab allows you not only to have free private repositories, but also to test them using free runners. These can run automatically, on push, for any branch or tag.

I keep a few private repositories with them, for personal projects and small experiments. I decided to give Gitlab CI a shot for a PostgreSQL-backed Clojure project.

There’s a basic example on the Gitlab CI repository. It gets and installs lein, which isn’t necessary. Instead, we’ll build use the clojure:lein Docker image.

Let’s start with an empty .gitlab-ci.yml file on your repository’s root.

1
2
3
image: clojure:lein-2.7.0
services:
- postgres:latest

You’ll see I’ve also included Postgres as a service since it’s what I’m using for the database.

We’ll then need to add a section for any environment variables

1
2
variables:
DATABASE_URL: "postgresql://postgres/dbname?user=uname&password=pwd"

Gitlab also allows you to define secret variables on a per-project basis, but there’s no need for that here.

Finally, we’ll add our before_script section, which just updates apt-get and loads the dependencies.

1
2
3
before_script:
- apt-get update -y
- lein deps

Update: Notice that I’m updating the package lists using apt-get. This is necessary because I’ll also need to install the Postgres client. If you don’t need to install anything using apt, you might save some build time by removing that line.

Before proceeding to test, we’ll install the Postgres client, initialize the database (adding some plugins), and run the migrations.

1
2
3
4
5
6
test: 
script:
- apt-get install postgresql-client -y
- psql -h postgres -U postgres < db-setup.sql
- lein with-profile test run migrate
- lein test

And voilá! Tests will run on push. You can find a history for the status of the Pipelines section of your project.

Pipelines

If for any reason you don’t see a Pipelines option, make sure that Builds are enabled in the project’s settings.

You’ll also get a build history

Pipelines

Here you’ll find the complete file as a snippet.

Cheers!


Published: 2016-10-10