Setting up continuous integration with CircleCI and GitLab
Tags: circleci, gitlab, cicd, tutorial
Published: 2023-06-07 23:12:45

---
title: Setting up continuous integration with CircleCI and GitLab
published: true
description: Learn how to set up continuous integration pipelines with GitLab and CircleCI.
tags: circleci,gitlab,cicd,tutorial
cover_image: https://ctf-cci-com.imgix.net/3FPR2tmaqQeWPvKBQWQjTb/17ead46fd065f2f4c0fdf9447f2e3325/Tutorial-Intermediate-A.jpg
# Use a ratio of 100:42 for best results.
# published_at: 2023-06-07 22:35 +0000
canonical_url: https://circleci.com/blog/setting-up-continuous-integration-with-gitlab/
---

CircleCI supports [GitLab](https://circleci.com/blog/announcing-gitlab-support/) as a version control system (VCS). In this tutorial you will learn how to set up your first CircleCI CI/CD pipeline for a project hosted on GitLab. As GitLab can be used either as a SaaS tool, as well as self-managed on-premise installation, I will cover the steps to connect it with CircleCI for both.

## Prerequisites and application basics

To follow along with this tutorial, you will need:

- Basic knowledge of Git commands
- Git installed and accessible on your machine
- A [GitLab account](https://gitlab.com/users/sign_up) — either self-managed or SaaS
- A [CircleCI account](https://circleci.com/signup/)

### Starter app

Our starter application is a minimal Python Flask app with a single ‘hello world’ web page, which specifies required dependencies and includes a test. You can access a [publicly hosted version of the starter app](https://gitlab.com/cci-devrel-demos/python-app-base) on GitLab.com.

You can get the application by downloading it straight from the GitLab web interface or cloning it via Git.

For the purposes of this tutorial, you don’t need to write the application from scratch, but if you are interested in complete development instruction, you can read through the beginning of [this blog post](https://circleci.com/blog/setting-up-continuous-integration-with-github/.

## GitLab SaaS vs self-managed

As mentioned above, this tutorial will teach you how to configure and run your CircleCI pipeline for a project hosted either on a self-managed or hosted version of GitLab. You can learn more about the differences between the hosted and self-managed versions in the [GitLab docs](https://docs.gitlab.com/ee/install/migrate/compare_sm_to_saas.html).

Setting up GitLab SaaS requires you only to sign up at gitlab.com. You will also need to make sure you can push to GitLab repositories, either by setting up your [SSH key](https://docs.gitlab.com/ee/user/ssh.html) or [personal access token](https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html#personal-access-tokens) for using GitLab with either SSH or HTTPS, respectively.

If you use GitLab self-managed, you will connect to it via your installation URL. This is specific to you, so for the purposes of this tutorial, use the URL `yourgitlabinstance.com` as a placeholder.

Create a new project with the starter app source code. You can use the new project wizard, either at `gitlab.com/projects/new` or `yourgitlabinstance.com/projects/new`, for SaaS or self-managed respectively.

## Creating a CircleCI config file

Before you set up CircleCI, you can tell it what to eventually start building.

Create a new directory `.circleci` in the top level of the project.

Now, create a new file `config.yml` in that directory (`.circleci/config.yml`) — this is your main configuration file for CircleCI.

Paste the following in the file:

```yaml
version: 2.1
jobs:
  test:
    docker:
      - image: cimg/python:3.10.11
    steps:
      - checkout
      - restore_cache:
          key: deps1-{{ .Branch }}-{{ checksum "requirements.txt" }}
      - run:
          name: Install dependencies
          command: |
            python3 -m venv venv
            . venv/bin/activate
            pip install -r requirements.txt
      - save_cache:
          key: deps1-{{ .Branch }}-{{ checksum "requirements.txt" }}
          paths:
            - "venv"
      - run:
          name: Running tests
          command: |
            . venv/bin/activate
            python3 tests.py
      - store_artifacts:
          path: test-reports/
          destination: python_app

workflows:
  run-tests:
    jobs:
      - test
```

In the CircleCI config file, you define everything you need CircleCI to do. In this case, you have added a single job (`test`) and a workflow (`run-tests`) containing that job.

The `test` job runs in a [Docker container](https://circleci.com/blog/docker-image-vs-container/) with Python installed, checks out the code at that specific commit, downloads the dependencies required for the app (Flask, in this case), sets up a Python venv, and runs the tests in `tests.py`. It also caches the dependencies to make subsequent pipeline runs faster and stores the test artifacts for easy access from CircleCI. To read more about CircleCI configuration, consult the [configuration reference](https://circleci.com/docs/configuration-reference/).

Save, make a new commit, and push to your GitLab repository. Now it’s time to configure your CircleCI integration with GitLab.

## Configuring CircleCI with GitLab SaaS (GitLab.com)

If you don’t have a CircleCI account yet, create one now. Head to https://circleci.com/signup/ and follow the instructions to sign up using your preferred method. If you choose GitLab, you will also be prompted to select your GitLab instance. Press **Connect** next to GitLab.com.

![Connect to your code](https://ctf-cci-com.imgix.net/34Js8TAdWkl0nANRkvODxw/cdef5ce2194a0331bed75a31ed986889/2023-05-08-ci-gitlab-18.48.16.png)

This should now prompt you to authorize CircleCI with your GitLab account, giving CircleCI access to your repositories.

![Authorize CircleCI](https://ctf-cci-com.imgix.net/2VSZ1kg5MI1I1R9S9tYpVJ/3683095bd43b7f67dcbd4822452c8b4d/2023-05-08-ci-gitlab-19.09.38.png)

Authorizing CircleCI will take you to the project creation wizard. You should see your repository on the list, and it should detect the CircleCI config you committed.

![Create new project](https://ctf-cci-com.imgix.net/55pwGMRJZ5arUonK1Ru7fG/914d779cd5a35492652aac7c8e57b806/2023-05-08-ci-gitlab-19.16.46.png)

Click **Create Project** to start building. This has created the project in CircleCI, and what’s left is to commit and push a change to trigger your first CircleCI pipeline.

![Make a change](https://ctf-cci-com.imgix.net/4pjBH6grcr53Qs9Z3RpsvH/e2c5618dce322633f4739831674859fe/2023-05-09-ci-gitlab-01.27.40.png)

Make any change, such as an update to your README file, then commit and push. This will create your first pipeline and start building and running your test.

![Build status](https://ctf-cci-com.imgix.net/7FKLZa07roF52X4xpiLd3t/225b6190675a9adf76e6ceac52e647ea/2023-05-09-ci-gitlab-01.29.14.png)

Seconds later it should be marked as successful. The status of your CircleCI pipeline will be automatically updated in your GitLab UI as well. Navigate to Build/Pipelines (on the left-hand side in GitLab) and the pipeline should show up there as well. You can see an example based on this tutorial in this [public project](https://gitlab.com/cci-devrel-demos/cci-python-app/-/pipelines).

![Build/Pipelines](https://ctf-cci-com.imgix.net/5hodZ8zxiaNFW7TM7pNLTa/403fe6d9b33bcd1c25840c1f42534f59/2023-05-09-ci-gitlab-01.32.03.png)

Clicking it will show you further details of the pipeline. Clicking the button under external - button **CircleCI: Workflow run tests** will take you back full circle into your workflow inside CircleCI.

![Pipeline details](https://ctf-cci-com.imgix.net/WGuoPZr3wfwmwPLDhcL7o/bf8ae751b53e7a6e9728d6b385243e77/2023-05-09-ci-gitlab-01.32.41.png)

## Connecting CircleCI to a self-managed GitLab instance

Self-managed installations, unlike the hosted SaaS versions, run on infrastructure you control. They will of course also have a different URL to access them. In this tutorial, use `yourgitlabinstance.com` as a placeholder for your actual instance URL. This section closely follows the [GitLab integration instructions in the CircleCI docs](https://circleci.com/docs/gitlab-integration).

Assuming you have followed the steps to get the project into GitLab and committed your first CircleCI config file, you can proceed with connecting to GitLab.

If you don’t have a CircleCI account yet, create one now. Head to https://circleci.com/signup/ and follow the instructions to sign up with your preferred method.

If you choose to sign up with GitLab, you will be prompted to select your GitLab instance. Press **Connect** next to GitLab self-managed.

![Connect self-managed](https://ctf-cci-com.imgix.net/34Js8TAdWkl0nANRkvODxw/cdef5ce2194a0331bed75a31ed986889/2023-05-08-ci-gitlab-18.48.16.png)

### GitLab self-managed project setup

The wizard guides you through the setup process step by step.

First enter the URL of your GitLab instance and click **Verify**. It will suffix it with ``/api/v4`` to complete your instance’s API endpoint.

Create your [personal GitLab access token](https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html) with an API scope, paste it in the Personal Access Token field and click **Verify**.

Next, pass in the `known_hosts` value and click **Verify**. You can get this by running `ssh-keyscan yourgitlabinstance.com` in the terminal and pasting the entire output in the `known_hosts field`. This allows CircleCI to [verify the authenticity](https://circleci.com/docs/gitlab-integration/#establish-the-authenticity-of-an-ssh-host) of your GitLab instance.

Finally, select your project in the dropdown. This will likely be `cci-python-app`. You can also give it a different name. This will set your GitLab self-managed project up for building on CircleCI.

![New self-managed project](https://ctf-cci-com.imgix.net/3uHYOJan5yJSACrQlvHYCH/29d1adf431472cfa84bc2d9cf6a6a7be/2023-05-08-ci-gitlab-20.11.21.png)

Trigger a pipeline by committing a change and pushing it to your GitLab repo. This will start your workflow and show it in the next few seconds.

![Project workflows](https://ctf-cci-com.imgix.net/vKnYtN1PPmy4QUmF5XH3m/b07d56d8985838bd4e56941c8e1c9402/2023-05-09-ci-gitlab-01.37.59.png)

You can also see your pipeline in the GitLab UI by navigating to CI/CD > Pipelines on the left-hand side. Note that this might look different, depending on what version of GitLab you are using.

![GitLab pipelines view](https://ctf-cci-com.imgix.net/3yPlK18ZvGO75M2hc8dUsC/381d03ef962ea74f903ae24ad6512845/2023-05-09-ci-gitlab-01.33.49.png)

Clicking on its status it will take you to the pipeline details, from where you can navigate back to the CircleCI UI to give you a 360 degree view of your project and your CI/CD pipeline’s workflows and jobs.

![Workflow and jobs](https://ctf-cci-com.imgix.net/68JW7CkbSe8OZsdCnYjI19/a9bff076e54e4f84dff3054f38295f0e/2023-05-09-ci-gitlab-01.34.42.png)

Congratulations! You have successfully configured CircleCI to start building your project on a self-managed GitLab instance.

## Conclusion

In this tutorial you have learned how to begin building projects hosted on GitLab with CircleCI. We have covered both on-premise self-managed GitLab as well as the hosted SaaS version on GitLab.com.

Wishing you successful building!