Skip to content

GitLab CI

GitLab includes utilities that allows us to create pipelines that can be used for CI (Continuous Integration) and CD (Continuous Delivery).

Gitlab as a complete DevOps lifecycle application provides its users with an integrated CI/CD feature which is managed through a single file placed at the repository’s root.

In a nutshell, the users will put the CI/CD configuration in a file named .gitlab-ci.yml and every time a code change is pushed to the Gitlab repository, Gitlab with run the configured CI/CD script.

GitLab runner

Scripts configured for Gitlab CI/CD will be executed by the Gitlab runner. Gitlab runner is an open-source project with various features on how to run the CI/CD script. Gitlab runner has several executor options such as SSH, Docker, and VirtualBox, to fit the users' needs.

Pipelines

Pipelines are the top-level component of continuous integration, delivery, and deployment. A pipeline will be created when a new push is made to the Gitlab repository. The pipeline will immediately run when there is an idle Gitlab runner or be put in a queue if all available runners are being used by someone else.

!CI pipeline
A pipeline will consist of jobs and stages.

Create a new file called “.gitlab-ci.yml” in the root of the repository. The .gitlab-ci file is a yaml file that contains the scripts that will be executed by the GitLab Runner.

Jobs

Jobs is something that defines what will be run.

Below is an example of .gitlab-ci-yml file with one job named test. We also specify that the job will run with node:12 docker image. By default, when running a job Gitlab will spawn a docker container (image can be specified as shown below), clone your repository inside the container, and run the specified script.

image: "node:12"
test:
  script:
    - npm install
    - npm run tests

Stages

Stages serve as categorization of jobs. It defines when and how a job will be run.

By default, a pipeline will have three stages: build, test, and deploy. Having default stages does not mean the users have to create a job for each stages, for stages with no jobs will simply be ignored.

Below we show a configuration with build and test stages:

image: "node:12"

before_script:
  - npm install

test:
  script:
    - npm run tests

build:
  stage: build
  script:
    - npm run build

Gitlab CI/CD also support the creation of custom stages.

stages:
  - test
  - pre-build
  - build
  - deploy
backend-test:
  stage: test
  ...
backend-pre-build:
  stage: pre-build
  ...
backend-build:
  stage: build
  ...
backend-deploy:
  stage: deploy
  ...

By default, the next stage will run after the previous one has succeeded. So when the previous failed, the next stage will not run.


Vue app to pages example

It is relatively simple to build and publish your vue app using GitLab pages.

Here is a simple .gitlab-ci.yml file.

pages:
  stage: deploy
  script:
    - echo "NODE_ENV=production" > .env
    - npm run build
  artifacts:
    paths:
    - public
  only:
    - master
  tags:
    - general
  cache: 
    untracked: true

Read more