GitLab CI

GitLab CI

GitLab CI is a feature of GitLab that allows users to have actions triggered upon pushing a repository to it's remote. For example, it can be used to execute all tests defined in the code, containerize the application and deploy it. A popular alternative to GitLab CI is Jenkins.

Install GitLab Runner

GitLab CI requires GitLab runner to execute the actions defined by the user. On Ubuntu, GitLab runner can be installed as so:

curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh | sudo bash
sudo apt install gitlab-runner

Registering runner

sudo gitlab-runner register

The command will prompt for the following information:

  • URL of the GitLab instance

  • Token (available from the Admin Area of the GitLab instance Overview > Runners)

  • A description for the runner

  • Tags for the runner

  • Executor

Usin the shell executor is the simplest way to use gitlab runner. If using the shell executor, commands are run in the host shell as the gitlab-runner user. To interact with docker, the gitlab-runner must be in the docker group:

sudo usermod -aG docker gitlab-runner

More info about the shell executor here

.gitlab-ci.yml

To trigger actions, a .gitlab-ci.yml file needs to be present in the project directory.

Excellent tutorial here

Here is an example .gitlab-ci.yml file used for the testing and dockerization of a NodeJS app:

stages:
  - install
  - test
  - dockerize
  - run

cache:
  paths:
    - node_modules/

install_dependencies:
  stage: install
  script:
    - npm install
  artifacts:
    paths:
      - node_modules/

testing_testing:
  stage: test
  script: npm test

dockerization:
  stage: dockerize
  script:
    - docker build -t gitlab-ci-dockerization-test .

run_container:
  stage: run
  script:
    - docker stop gitlab-ci-dockerization-test || true && docker rm gitlab-ci-dockerization-test || true
    - docker run --name=gitlab-ci-dockerization-test -d -p 3334:3334 gitlab-ci-dockerization-test