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 -R gitlab-runner
gitlab-runner run
screen -ls
Gitlab-Runner 가져와서 쓰기
Repository에서 Runner 활성화
- Repository로 이동한다
- 왼쪽 하단 >
Settings > CI/CD > Runners > Expand
Project Runners를 보면 Other available runners에서 runner 확인
- 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 에서 Environment와 Key, Value를 선언한다
- 아래 예제에선 Environment는 dev, Key는 API_KEY, Value는 임의의 https://my-project/api/v1로 선언했다.
yml에서 variables의 VITE_API_URL을 삭제한다
- 주석이 없는
prepare_dev를 주석처리하고, 주석처리된 prepare_dev를 활성화한다.
예시
stages:
- prepare
- install
- deploy
variables:
BUILD_DIR: "build"
DEPLOY_DIR: "/data/my-project"
PACKAGE_MANAGER: "yarn"
VITE_API_URL: "https://my-project/api/v1"
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
dependencies:
- prepare_dev
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
script:
- mkdir -p $DEPLOY_DIR
- rm -rf $DEPLOY_DIR/$BUILD_DIR
- cp -r ./$BUILD_DIR $DEPLOY_DIR/$BUILD_DIR
참고자료