Docker를 사용한 GitLab 설치 및 CI/CD 통합 (GitLab-Runner)

GitLab 및 GitLab-Runner 설정

먼저 GITLAB_HOME 환경 변수를 설정합니다.

export GITLAB_HOME=/srv/gitlab

BASH 환경을 사용하는 경우, 위 설정을 ~/.bash_profile 파일에 추가하여 source ~/.bash_profile 명령으로 영구적으로 적용할 수 있습니다.

docker run --detach \
  --hostname 192.168.1.205 \
  --publish 9443:443 --publish 9080:9080 --publish 9022:22 \
  --name gitlab \
  --restart always \
  --volume $GITLAB_HOME/config:/etc/gitlab:Z \
  --volume $GITLAB_HOME/logs:/var/log/gitlab:Z \
  --volume $GITLAB_HOME/data:/var/opt/gitlab:Z \
  --shm-size 256m \
  gitlab/gitlab-ee:latest

docker run -d --name gitlab-runner --restart always \
  -v /srv/gitlab-runner/config:/etc/gitlab-runner \
  -v /var/run/docker.sock:/var/run/docker.sock \
  gitlab/gitlab-runner:latest

GitLab 기본 비밀번호 확인

sudo docker exec -it gitlab grep 'Password:' /etc/gitlab/initial_root_password

Runner 등록

gitlab-runner 컨테이너 내에서 등록

gitlab-runner register

# GitLab 인스턴스 주소 입력
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com )
http://192.168.1.200

# 토큰 입력
Please enter the gitlab-ci token for this runner
xxxxxxxx

# 설명 정보 입력
Enter a description for the runner:
[2f5631485bf3]: 이것은 설명입니다

# 태그 입력
Enter tags for the runner (comma-separated):
test_tag

# 실행기 선택
Enter an executor: docker-ssh, ssh, virtualbox, docker-ssh+machine, kubernetes, custom, docker, parallels, shell, docker+machine:
shell

# 위 실행기가 docker인 경우, 프로젝트 루트의 .gitlab-ci.yml에서 특정 docker 버전을 지정해야 합니다
#Enter the default Docker image (for example, ruby:2.6):
#alpine:latest

토큰은 GitLab 관리자 페이지에 로그인 후 확인할 수 있습니다.

등록 후 Runner가 자동으로 GitLab에 연결됩니다.

워크플로우 생성

프로젝트 루트 디렉토리에 .gitlab-ci.yml 파일을 생성하고 다음 내용을 추가합니다:

cache:
  paths:
    - node_modules/
    - dist/

stages:
  - build_deploy

variables:
  NPM_REGISTRY: "https://registry.npmmirror.com/"
  REMOTE_SERVER: "root@192.168.1.205:/www/wwwroot/xxx.xxx.com"
  
before_script:
  - npm config set registry ${NPM_REGISTRY}

build:
  stage: build_deploy
  script:
    - npm install  # 의존성 설치
    - npm run build  # 빌드 실행
    - sshpass -p 12345678 scp -o StrictHostKeyChecking=no -r dist/* $REMOTE_SERVER # 배포
    - echo '성공!'

커밋 및 CI/CD 파이프라인 트리거

git add .gitlab-ci.yml
git commit -m "CI/CD 구성 추가"
git push origin master

등록된 Runner 확인

cat /etc/gitlab-runner/config.toml

이미지 중복 다운로드 방지

/etc/gitlab-runner/config.toml 파일의 [runners.docker] 섹션에 pull_policy = "if-not-present"를 추가합니다.

GitLab 클론 주소 변경

방법 1

/opt/gitlab/embedded/service/gitlab-rails/config/gitlab.yml 파일에서 gitlab 섹션의 hostport를 수정합니다

수정 후 gitlab-ctl restart를 실행하여 설정을 적용합니다 단점: reconfigure 후 설정이 초기화되어 다시 설정해야 합니다.

방법 2

/etc/gitlab/gitlab.rb 파일에서 external_url을 수정합니다

수정 후 gitlab-ctl reconfigure를 실행하여 설정을 적용합니다

Docker로 배포한 경우, 내부 외부 매핑 포트가 다르므로 http 리스닝 포트가 80이 아닐 수 있습니다, 파일에서 nginx['listen_port'] = 실제_리스닝_포트로 수정해야 하며, 그렇지 않으면 GitLab이 정상적으로 시작되지 않습니다.

GitLab-Runner 환경 설정

Docker로 실행한 환경은 Ubuntu이므로 apt install로 소프트웨어를 설치할 수 있습니다.

이 프로젝트는 Node.js 기반이므로 Node.js 설치 절차를 여기에 기재합니다.

apt update
apt install nodejs
node -v
apt install npm

Ubuntu 20.04 기본 소프트웨어 저장소에는 10.19 버전의 Node.js가 제공됩니다. 이 버전은 다소 오래되었습니다. 아래 방식으로 설치할 수도 있습니다.

curl -sL https://deb.nodesource.com/setup_16.x -o nodesource_setup.sh
bash nodesource_setup.sh
apt install nodejs
node -v

추천 자료

GitLab-CI에서 artifacts 사용에 대한 연구: http://zacksleo.top/archives/

Gitlab CI 고급 사용법: https://www.jianshu.com/p/3c0cbb6c2936

GitLab 환경 설정, CI/CD 구성, Docker 이미지 자동 빌드 완벽 가이드: https://www.cnblogs.com/hzhhhbb/p/13966904.html?share_token=4dfe4dbe-caac-4437-b2b4-ea59b03c67d1

GitLab CI/CD 지속적 통합 https://www.cnblogs.com/linagcheng/p/14707967.html

Docker로 GitLab-Runner 설치 https://www.cnblogs.com/lvlinguang/p/15191669.html

태그: docker GitLab CI/CD GitLab-Runner DevOps

5월 30일 19:50에 게시됨