Nếu như bạn đang quản lý source code của mình trên các công cụ như Gitlab, Github hoặc bitbucket thì việc thực hiện tự động đưa code lên server và build là việc hết sức cần thiết. Thay vì sau khi chúng ta development xong 1 chức năng thì chúng ta commit code lên git để lưu trữ các phiên bản và muốn để khách hàng xem được chức năng đó chạy như thế nào thì chúng ta phải copy code lên server thông qua FTP, sau đó login vào server để build, vân vân và mây mây :)).
Bạn thử tưởng tượng với 1 project lớn và team dev hàng chục con người và mỗi ngày có khoảng hàng trăm commit thì việc test và build là vấn đề rất lớn đòi hỏi nhiều công sức, và tiềm ẩn nhiều rủi ro.
Nhưng điều đó sẽ được đơn giản hóa với Gitlab CI/CD. Vậy Gitlab CI/CD là gì? Hiểu đơn giản là:
- CI (Continuous Integration): là quá trình mà code của chúng ta được build, test trước khi tích hợp vào repository chung. Áp dụng với bất kì commit nào vào repository
- CD (Continuous Delivery): là quá trình xảy ra sau CI, đó là khi ta triển khai code ra môi trường thật (
staging,production,…)
Ở bài viết này chúng ta sẽ không đi sâu vào tìm hiểu xem các khái niệm là gì mà chúng ta sẽ đi sâu vào từng bước cách setup CI/CD trên Gitlab như thế nào. Let’s goooo!
Trước tiên để thực hiện chúng ta cần 1 repository trên Gitlab, 1 server có thể là google clouds, AWS, hoặc Digital Ocean Droplet và đảm bảo rằng chúng ta có quyền truy cập vào server bằng SSH và có quyền install các package.
Tạo 1 Repository trên Gitlab
Trước tiên chúng ta sẽ tạo 1 repository trên gitlab và commit code của chúng ta lên đó (hoặc là nếu bạn đã có sẵn 1 repository rồi thì có thể sử dụng luôn repo đó ). ở đây chúng ta sẽ tạo 1 project có tên là devops-deploy

SSH vào server
bạn cần truy cập vào server qua ssh để cài đặt gitlab runner
$ ssh user@YOUR_IP
Sau khi đã login vào server chúng ta tiến hành cài đặt gitlab runner:
$ curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh" | sudo bash
$ sudo apt-get install gitlab-runner
Kiểm tra runner vừa cài:
$ sudo gitlab-runner -version
output:
Version: 16.0.2
Git revision: 85586bd1
Git branch: 16-0-stable
GO version: go1.19.9
Built: 2023-06-02T17:31:27+0000
OS/Arch: linux/amd64
Register runner
tiếp theo chúng ta cần đăng ký runner:
$ sudo gitlab-runner register

sau khi chạy lệnh trên màn hình sẽ xuất hiện yêu cầu nhập Gitlab instance URL:
chúng ta sẽ nhập: https://gitlab.com/ và Enter
Tiếp theo sẽ là Enter the registration token: bạn cần nhập token của gitlab ở CI/CD settings -> Runners

Tiếp theo Enter a description for the runner: nhập description cho runner và Enter
Tiếp theo Enter tags for the runner (comma-separated): nhập tag cho runner và Enter
Tiếp theo Enter optional maintenance note for the runner: có thể để trống và Enter
Tiếp theo Enter an executor: parallels, docker-autoscaler, docker+machine, kubernetes, custom, docker-windows, ssh, virtualbox, instance, docker, shell: Nhập shell và Enter
các bạn có thể chọn docker hoặc các option khác nếu các bạn quen.
quá trình register đã hoàn thành chúng ta cần start runner:
$ sudo gitlab-runner start
bây giờ chúng ta quay lại gitlab để xem đã register thành công chưa

Hiển thị như ảnh trên là chúng ta đã register thành công.
Tạo Script deploy
Gitlab cung cấp cho chúng ta file template .gitlab-ci.yml file này chứa các script để scan và deploy project
# This file is a template, and might need editing before it works on your project.
# This is a sample GitLab CI/CD configuration file that should run without any modifications.
# It demonstrates a basic 3 stage CI/CD pipeline. Instead of real tests or scripts,
# it uses echo commands to simulate the pipeline execution.
#
# A pipeline is composed of independent jobs that run scripts, grouped into stages.
# Stages run in sequential order, but jobs within stages run in parallel.
#
# For more information, see: https://docs.gitlab.com/ee/ci/yaml/index.html#stages
#
# You can copy and paste this template into a new `.gitlab-ci.yml` file.
# You should not add this template to an existing `.gitlab-ci.yml` file by using the `include:` keyword.
#
# To contribute improvements to CI/CD templates, please follow the Development guide at:
# https://docs.gitlab.com/ee/development/cicd/templates.html
# This specific template is located at:
# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Getting-Started.gitlab-ci.yml
stages:
-test
-master
include:
-template:Security/SAST.gitlab-ci.yml
clamscan:
image:alpine:latest
script:
-apkupdate&&apkaddclamavclamav-libunrar
-freshclam--show-progress--no-warnings
-clamscan-zira|teescan-results.txt
allow_failure:true
artifacts:
when:on_failure
paths:
-scan-results.txt
deploy_server:
stage:master
variables:
APP_STG_FOLDER:"/var/www/html/testing_project"
script:
-echo"Deploying code server"
-echo"rsync code from $CI_PROJECT_DIR to $APP_STG_FOLDER"
# rsync only updated and new files, delete file if it's longer in repo need recursive flags
# also exluced file/dir included in excluded_list.txt
-rsync-ur--delete--exclude-from"$CI_PROJECT_DIR""$CI_PROJECT_DIR""$APP_STG_FOLDER"
-cd$APP_STG_FOLDER/testing_project
-yarn --version && yarn install && yarn cache clean && yarn build # câu lệnh build project
tags:
-"<TAGS>" # tag chúng ta đã nhập lúc đăng ký runner
environment:
name:master
url: <URL website>
only:
-master
Lưu ý: chúng ta cần phải nhập đúng Tags khi đăng ký runner trên server
Như vậy là chúng ta đã setup xong auto deploy cho dự án, bây giờ chờ kết quả chạy job chúng ta vào pipelines để xem kết quả.
Chúc các bạn thành công.
