24-10-18 CICD

gateisbug·2024년 10월 31일

Learn

목록 보기
4/4

linux에서 Gitlab-Runner 도입하기

Gitlab-Runner를 설치하는 과정

Ubuntu 환경에서 gitlab 설치

curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh" | sudo bash
sudo apt install gitlab-runner
sudo apt --fix-broken install # 이거 하라길래 함
apt-cache madison gitlab-runner
sudo apt install gitlab-runner=16.5.0
gitlab-runner -v # 버전 확인

gitlab-runner 등록

gitlab-runner register  --url https://rtl.acryl.ai  --token glrt-4ssjoCmBSYyMch3sojxP

Enter the GitLab instance URL (for example, https://gitlab.com/): 
https://rtl.acryl.ai

Enter a name for the runner. This is stored only in the local config.toml file:
[dev]: glrt-4ssjoCmBSYyMch3sojxP

Enter an executor: docker-windows, ssh, docker+machine, kubernetes, custom, docker, parallels, shell, virtualbox, docker-autoscaler, instance:
shell

Configuration (with the authentication token) was saved in "/home/dev/.gitlab-runner/config.toml"

gitlab-runner 실행

gitlab-runner run

# Screen 에서 실행
screen -R gitlab-runner
gitlab-runner run
# 스크린 나가기는 ctrl+a, d
screen -ls # 작동중인 스크린 확인

Gitlab-Runner 가져와서 쓰기

Repository에서 Runner 활성화

  1. Repository로 이동한다
  2. 왼쪽 하단 > Settings > CI/CD > Runners > Expand
  3. Project Runners를 보면 Other available runners에서 runner 확인
  4. Repository에서 설정이 되어있지 않다면 Enable for this project 버튼이 있을텐데 클릭하여 활성화

CI/CD script 작성

  • Gitlab-runner는 기본값으로 {PROJECT_ROOT}/.gitlab-ci.yml을 읽어온다.
    - 예를 들어 따로 설정하지 않았다면, my-project/.gitlab-ci.yml을 읽어옴
    - 이 값을 변경하고 싶다면 Settings > CI/CD > General pipelines > Expand > CI/CD configuration file 에서 위치를 수정하면 된다.
    - 내 프로젝트의 레포지토리에선 .gitlab/.gitlab-ci.yml 을 읽어오도록 수정했다.

.env를 사용하는 경우(optional)

gitlab-ci를 이용하는 경우

  • 이 경우엔 variables에 선언된 VITE_API_URL 를 수정하면 된다.

Gitlab CI/CD Variable 이용

  • Gitlab CI/CD Settings > Variables > Add variable 에서 EnvironmentKey, Value를 선언한다
    - 아래 예제에선 Environmentdev, KeyAPI_KEY, Value는 임의의 https://my-project/api/v1로 선언했다.
  • yml에서 variablesVITE_API_URL을 삭제한다
  • 주석이 없는 prepare_dev를 주석처리하고, 주석처리된 prepare_dev를 활성화한다.

예시

stages: # 이 ci/cd는 아래와 같은 스테이지를 거친다  
  - prepare  
  - install # 설치 스테이지  
  - deploy # 배포 스테이지  
  
variables: # 이 ci/cd에선 아래와 같은 variable이 사용된다  
  BUILD_DIR: "build" # 빌드 디렉토리의 이름  
  DEPLOY_DIR: "/data/my-project" # dev 서버에서 프로젝트의 설치 위치 # 뒤에 / 붙이면 안됨  
  PACKAGE_MANAGER: "yarn" # 이 프로젝트의 패키지 매니저 = yarn / npm 만 지원  
  VITE_API_URL: "https://my-project/api/v1" # dev 서버에서 이용할 API_URL  

prepare_dev:  
  tags:  
    - my-dev  
  stage: prepare  
  only:  
    - dev  
  script:  
    - echo "VITE_API_URL=$VITE_API_URL" > .env  
  artifacts:  
    paths:  
      - .env  
    expire_in: 24 hour  
  
installation:  
  stage: install # 설치 스테이지  
  tags:  
    - my-dev # 깃랩 러너 지정  
  only: # dev 브랜치만 작동  
    - dev  
  dependencies:  
    - prepare_dev # dev 환경에서는 prepare_dev 잡에서 artifacts 사용  
  script: # 아래 스크립트를 순차적 실행  
    - if [ "$PACKAGE_MANAGER" == "yarn" ]; then yarn install; else npm install; fi  
    - if [ "$PACKAGE_MANAGER" == "yarn" ]; then yarn build; else npm run build; fi  
  artifacts:  
    paths:  
      - $BUILD_DIR # 배포 스테이지에 전달할 빌드 디렉토리  
    expire_in: 24 hour # 필요에 따라 만료 시간을 조정 가능  
  
deploy_to_dev:  
  stage: deploy  
  tags:  
    - my-dev  
  only:  
    - dev  
  dependencies:  
    - installation # 이전 스테이지의 artifacts 사용  
  script:  
    - mkdir -p $DEPLOY_DIR  
    - rm -rf $DEPLOY_DIR/$BUILD_DIR # 기존 빌드 삭제  
    - cp -r ./$BUILD_DIR $DEPLOY_DIR/$BUILD_DIR

참고자료

profile
쉽고, 짧고, 실용적인 코드를 지향하는 코드 디스펜서입니다.

0개의 댓글