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.

image: clojure:lein-2.7.0
- postgres:latest


- 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

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

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.

- apt-get update -y
- lein deps

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

- 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.

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

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


Originally published at