Setup Gitlab CI

chauhoangminhnguyen

chauhoangminhnguyen

Posted on September 19, 2024

Setup Gitlab CI

Introduction

Gitlab is a comprehensive platform designed for software development and version control using git. It provides a user-friendly web interface that enhances the speed of working with git, making it easier to manage Git repositories. Gitlab offers a range of features including:

  • Free public and private repositories: You can host your code securely and privately or share it with the world.
  • Continuous Integration/Continuous Deployment (CI/CD): Automate the testing and deployment of your code.
  • Free private docker image storage on Container Registry

In this article, I'll guide you on how to push a Docker image to the Gitlab Container Registry and set up CI to automatically build and push Docker images when you push code to a Gitlab repository.

Gitlab CI

Pushing a Docker Image to the Gitlab Container Registry

First, you'll need a Gitlab account and a repository (either public or private will work).

Use the NodeJS Typescript Server project I introduced earlier, or any project you have that includes a Dockerfile for building a Docker image.

Next, create a Personal Access Token to prepare for pushing the image to the Container Registry.

After building your Docker image, tag the image appropriately to ensure it can be pushed to the Container Registry.

docker tag {image name after build} registry.gitlab.com/{gitlab username}/{repository}:{version}

#ex:
docker tag express-ts registry.gitlab.com/username/express-ts:latest
Enter fullscreen mode Exit fullscreen mode

Logging into the Gitlab Container Registry

docker login -u {gitlab username} -p {access token} registry.gitlab.com
Enter fullscreen mode Exit fullscreen mode

Push image to Registry Container

docker push registry.gitlab.com/{gitlab username}/{repository}:{version}

#ex:
docker push registry.gitlab.com/username/express-ts:latest
Enter fullscreen mode Exit fullscreen mode

The results are as follows:
Container registry

Setting up Gitlab CI

To set up steps that run whenever you push code to your Gitlab repository, add a .gitlab-ci.yml file to your project with the following content:

image: docker:24.0.7

services:
  - docker:24.0.7-dind

stages:
  - build

before_script:
  - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY

build:
  stage: build
  script:
    - docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA -t $CI_REGISTRY_IMAGE:latest .
    - docker image push --all-tags $CI_REGISTRY_IMAGE
Enter fullscreen mode Exit fullscreen mode

Explanation:

- image and services: Define the Docker environment to execute Docker commands.

- stages: Specify the stages that will execute when code is pushed. Here, there's only one stage: build.

- before_script: Runs before each stage. This step logs into the Container Registry.

- stage build: Builds the image with two tags: the SHA commit ID and "latest", then pushes all tags to the Container Registry.

Gitlab provides environment variables, which are information that can be used when stages are executed:

  • CI_REGISTRY_USER: username
  • CI_REGISTRY_PASSWORD: password
  • CI_REGISTRY: Gitlab register
  • CI_REGISTRY_IMAGE: image name
  • CI_COMMIT_SHA: SHA commit id

After that, just push the code to the Gitlab repository to automatically trigger the CI process.

Job running

Job done

Job detail

See you in the next articles!


If you found this content helpful, please visit the original article on my blog to support the author and explore more interesting content.

BlogspotBlogspotDev.toFacebookX


Some series you might find interesting:

💖 💪 🙅 🚩
chauhoangminhnguyen
chauhoangminhnguyen

Posted on September 19, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Setup Gitlab CI
gitlab Setup Gitlab CI

September 19, 2024