Using Gitlab's Free Shared Runners for CI/CD

Using Gitlab's Free Shared Runners for CI/CD

Daily short news for you
  • Privacy Guides is a non-profit project aimed at providing users with insights into privacy rights, while also recommending best practices or tools to help reclaim privacy in the world of the Internet.

    There are many great articles here, and I will take the example of three concepts that are often confused or misrepresented: Privacy, Security, and Anonymity. While many people who oppose privacy argue that a person does not need privacy if they have 'nothing to hide.' 'This is a dangerous misconception, as it creates the impression that those who demand privacy must be deviant, criminal, or wrongdoers.' - Why Privacy Matters.

    » Read more
  • There is a wonderful place to learn, or if you're stuck in the thought that there's nothing left to learn, then the comments over at Hacker News are just for you.

    Y Combinator - the company behind Hacker News focuses on venture capital investments for startups in Silicon Valley, so it’s no surprise that there are many brilliant minds commenting here. But their casual discussions provide us with keywords that can open up many new insights.

    Don't believe it? Just scroll a bit, click on a post that matches your interests, check out the comments, and don’t forget to grab a cup of coffee next to you ☕️

    » Read more
  • Just got played by my buddy Turso. The server suddenly crashed, and checking the logs revealed a lot of errors:

    Operation was blocked LibsqlError: PROXY_ERROR: error executing a request on the primary

    Suspicious, I went to the Turso admin panel and saw the statistics showing that I had executed over 500 million write commands!? At that moment, I was like, "What the heck? Am I being DDoSed? But there's no way I could have written 500 million."

    Turso offers users free monthly limits of 1 billion read requests and 25 million write requests, yet I had written over 500 million. Does that seem unreasonable to everyone? 😆. But the server was down, and should I really spend money to get it back online? Roughly calculating, 500M would cost about $500.

    After that, I went to the Discord channel seeking help, and very quickly someone came in to assist me, and just a few minutes later they informed me that the error was on their side and had restored the service for me. Truly, in the midst of misfortune, there’s good fortune; what I love most about this service is the quick support like this 🙏

    » Read more

Problem

CI/CD involves the frequent and rapid integration of code and the delivery of new releases. CI/CD makes the process from coding to deploying applications faster and more automated.

Typically, we go through steps such as writing code -> testing (unit testing) -> code commit -> build -> deploy... But with integrated CI/CD, many steps in the code writing -> deployment process can be automated.

CI/CD is supported on code management platforms like Gitlab, Github... and is triggered by events like code commit. To use CI/CD, Runners need to be set up to connect to your account. The Runners play a role in processing your code and application logic.

In this article, I will discuss how to use Gitlab's Shared Runner.

Gitlab Shared Runner

If you use Gitlab for code management, you can use Shared Runners with free time of up to 400 minutes for Private projects and unlimited for Public and Community projects.

It may not seem like a lot, but for personal projects that save costs and can integrate CI/CD, it is worth using. My blog is currently using this method for automated deployment to the Development environment.

To activate Shared runners, go to Project > Settings > CI/CD and check Enable shared runners for this project.

Enable gitlab shared runners

Below the Available shared runners, you will see the number of Shared runners provided by Gitlab along with their statuses. Pay attention to the "label" below each Runner, as we will need it to specify the necessary Runner for each stage's usage.

gitlab runner

How to use

.gitlab-ci.yml is the yml file to activate Gitlab's CI/CD. It contains the configuration and directives for our automation process.

The 2coffee blog uses Docker to build and push the image to the Gitlab registry, then sends an HTTP request to the server to update the service, creating a complete CI/CD process.

I divide it into 2 stages: build/push image and HTTP request:

stages:  
  - build
  - curl-docker-ci

In the build stage, the Runner that supports Docker is used, so I set the tags as docker. Parameters such as login information/registry password are passed through Variables, which are set up in Gitlab. Instead of specifying the steps to build & push the image directly in the yml file, I created a file named build-dev.sh which contains bash commands to build & push the image.

Lastly, since I only want to set up CI/CD for the Development environment, I specify that CI should only run when code is committed to the develop branch.

docker-build:  
  stage: build
  image: docker:20.10.9
  tags:  
    - docker
  services:  
    - docker:dind
  before_script:  
    - echo "$DOCKER_REGISTRY_PASS" | docker login $DOCKER_REGISTRY --username $DOCKER_REGISTRY_USER --password-stdin
  script:  
    - if [ "$CI_COMMIT_REF_NAME" = "develop" ]; then
      ./build/bin/build-dev.sh;
      fi
  only:  
    - develop

The content of build-dev.sh is simple:

#!/bin/sh -e

IMAGE=registry.gitlab.com/2coffee/page
VERSION=dev

name=${IMAGE}:${VERSION}

docker build -t ${name} -f ./Dockerfile .  

docker push ${name}

In the next step, I also run a curl-docker-ci.sh file that contains the CURL command for an HTTP request to the server to update the service. Since this bash command only needs a Linux environment to run, I specify the tags as linux.

curl-docker-ci:  
  stage: curl-docker-ci
  tags:  
    - linux
  script:  
    - ./build/bin/curl-docker-ci.sh
  only:  
    - develop

curl-docker-ci.sh simply contains one curl command:

#!/bin/sh -e

curl --location --request POST $DOCKER_CI_ENDPOINT \
--header "Authorization: Basic $DOCKER_CI_BASIC_AUTH" \
--header 'Content-Type: application/json' \
--data '{"name": "'$DOCKER_CI_SERVICE_NAME'"}'

Putting it all together, I have a complete .gitlab-ci.yml file like this:

stages:  
  - build
  - curl-docker-ci

docker-build:  
  stage: build
  image: docker:20.10.9
  tags:  
    - docker
  services:  
    - docker:dind
  before_script:  
    - echo "$DOCKER_REGISTRY_PASS" | docker login $DOCKER_REGISTRY --username $DOCKER_REGISTRY_USER --password-stdin
  script:  
    - if [ "$CI_COMMIT_REF_NAME" = "develop" ]; then
      ./build/bin/build-dev.sh;
      fi
  only:  
    - develop

curl-docker-ci:  
  stage: curl-docker-ci
  tags:  
    - linux
  script:  
    - ./build/bin/curl-docker-ci.sh
  only:  
    - develop

Now, let's try committing code to the develop branch and go to Project > CI/CD > Pipelines to see if the CI is activated.

shared runner running

Conclusion

CI/CD is the process of frequent and rapid integration and delivery of new releases. Thanks to CI/CD, many steps from coding to deploying applications can be automated.

If you use Gitlab for code management, you can use up to 400 minutes of free CI/CD. Although it may not seem like a lot, you can register a Runner to remove this limitation.

Premium
Hello

The secret stack of Blog

As a developer, are you curious about the technology secrets or the technical debts of this blog? All secrets will be revealed in the article below. What are you waiting for, click now!

As a developer, are you curious about the technology secrets or the technical debts of this blog? All secrets will be revealed in the article below. What are you waiting for, click now!

View all

Subscribe to receive new article notifications

or
* The summary newsletter is sent every 1-2 weeks, cancel anytime.

Comments (3)

Leave a comment...
Avatar
Thành Đỗ2 years ago
Cho mình xin cách dùng curl để update service với ad
Reply
Avatar
Thành Đỗ2 years ago
Cảm ơn ạ        
Avatar
Xuân Hoài Tống2 years ago
À mình viết một rest service bằng docker sdk để chọc vào docker mà update service thôi đó bạn
Avatar
Hương Trịnh2 years ago
Không đăng nhập được bằng github ad ơi
Reply
Avatar
Xuân Hoài Tống2 years ago
Ok bạn mình kiểm tra lại ạ
Avatar
Tùng Nguyễn2 years ago
Bạn có bài viết nào nói về ci cd không
Reply
Avatar
Xuân Hoài Tống2 years ago
Chào bạn, hiện chưa có ạ