CI/CD with Gitlab

Install Gitlab Repositories

install dependencies:

sudo yum install -y curl policycoreutils-python openssh-server
sudo systemctl enable sshd
sudo systemctl start sshd
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo systemctl reload firewalld

install repo:

curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash

install gitlab community edition

EXTERNAL_URL="https://gt.doco.dgprasetya.net" yum install -y gitlab-ce

setelah selesai, bisa diakses via url https://gt.doco.dgprasetya.net:

image

Install Gitlab Runner

install dependencies:

yum install -y yum-utils   device-mapper-persistent-data   lvm2

install docker untuk gitlab runner:

yum-config-manager     --add-repo     https://download.docker.com/linux/centos/docker-ce.repo
yum install docker-ce docker-ce-cli containerd.io

jalankan daemon docker:

systemctl start docker

install runner gitlab:

curl -LJO https://gitlab-runner-downloads.s3.amazonaws.com/latest/rpm/gitlab-runner_amd64.rpm
yum install git -y
rpm -i gitlab-runner_amd64.rpm

konfiurasi/register gitlab-runner

jalan perintah berikut:

gitlab-runner register

lalu akan muncul questions seperti berikut:

Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com)
https://example.digitalocean.com
Enter the token you obtained from your GitLab instance:

Please enter the gitlab-ci token for this runner
sample-gitlab-ci-token
Enter a description that will help you recognize it in the GitLab web interface. We recommend naming this instance something unique, like runner-bastion for clarity.

Please enter the gitlab-ci description for this runner
[yourhostname] runner-bastion
If relevant, you may enter the tags for code you will build with your runner. However, we recommend this is left blank at this stage. This can easily be changed from the GitLab interface later.

Please enter the gitlab-ci tags for this runner (comma separated):
code-tag
Choose whether or not your runner should be able to run untagged jobs. This setting allows you to choose whether your runner should build repositories with no tags at all, or require specific tags. Select true in this case, so your runner can execute all repositories.

Whether to run untagged jobs [true/false]: true
Choose if this runner should be shared among your projects, or locked to the current one, which blocks it from building any code other than those specified. Select false for now, as this can be changed later in GitLab’s interface:

Whether to lock Runner to current project [true/false]: false
Choose the executor which will build your machines. Because we’ll be creating new Droplets using Docker, we’ll choose docker+machine here, but you can read more about the advantages of each approach in this compatibility chart:

Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell:
docker+machine
You’ll be asked which image to use for projects that don’t explicitly define one. We’ll choose a basic, secure default:

Please enter the Docker image (e.g. ruby:2.1):
alpine:latest

status runner yg sudah terkoneksi:

image

Contoh sederhana konsep ci/cd di gitlab:

  1. Create Repo Test di Gitlab.
  2. Create file .gitlab-ci.yml , isikan seperti berikut: Untuk memahami bagian ini, kita harus tahu dulu referensi pipeline yg digunakan di gitlab. dokumentasi lengkap bisa dibaca di https://docs.gitlab.com/ce/ci/yaml/ dalam contoh ini define stages ke dalam kedua kelompok, compile dan deploy .

    stages: - compile - deploy

setelah itu dari masing-masing kelompok, berikan task dalam variable script dan masukkan kelompok compile ke dalam stage:

compile:
  stage: compile
  script:
    - pwd;uname -a;uptime

untuk kelompok yg lain (deploy) sama seperti berikut:

deploy:
  stage: deploy
  script:
    - ls /;uname -a

sesuaikan perintah task yg ada di script.

hasil akhir .gitlab-ci.yml:

stages:
  - compile
  - deploy

compile:
  stage: compile
  script:
    - pwd;uname -a;uptime

deploy:
  stage: deploy
  script:
    - ls /;uname -a
  1. commit untuk memproses secara otomatis ci/cd

hasil:

image

image

Next, kita akan coba deployment, automation testing, deliver apps dg konsep tsb lewat ci/cd gitlab.