티스토리 뷰

개발/Infra

Gitlab-Runner로 빌드하기 Vue, Gradle

KyeongRok Kim 2021. 7. 12. 13:08

 

Gitlab-runner설치

https://sean-bradley.medium.com/installing-gitlab-runner-on-ubuntu-and-centos-80f3a2de0290

 

Installing GitLab Runner on Ubuntu and CentOS

Add the GitLab official repository to your servers package manager.

sean-bradley.medium.com

위 포스트 대로 했을 때 성공 했습니다.

 

gitlab공식 repository추가

curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh | sudo bash
apt-get update
apt-get install gitlab-runner

 

gitlab-runner등록하기

sudo gitlab-runner register --url https://<gitlab-url>:9443/ --registration-token $REGISTRATION_TOKEN

위와 같이 등록을 해도 되고 gitlab-runner register만 입력하면 대화식으로 runner를 띄울 수 있습니다 

 

Settings -> Rnners로 갑니다.

 

 

gitlab에서 ci/cd 빌드 스크립트 추가하기

gitlab으로 갑니다.

왼쪽 CI/CD에서 Pipelines를 선택 합니다.

아직 빌드 스크립트가 없기 때문에 위와 같이 나옵니다.

'Create new CI/CD pipeline'을 누릅니다.

 

위와 같이 스크립트의 기본 탬플릿이 생성 됩니다.

 

 

Commit changes를 누릅니다.

 

.gitlab-ci.yml 파일이 생깁니다. 클릭 해봅니다.

 

앞에서 생성된 탬플릿이 그대로 파일로 만들어져있습니다.

빌드를 실행하면 script: 블럭에 있는 echo가 실행 될 것입니다.

 

우측 상단 Run pipeline을 클릭 합니다.

 

Variables 세팅 하는 화면이 나옵니다. 나중에 변수를 설정할 일이 있으면 바꿔주고 일단은 Run pipeline을 클릭 하면 빌드가 실행 됩니다.

 

Pipelines 목록으로 가보면 방금 실행한 Job이 running으로 나옵니다. 깃랩에서는 각 빌드를 Job이라고 합니다.

 

 

방금 만든 job으로 들어가서 build-job을 눌러봅니다.

 

그러면 로그가 나오는 것을 볼 수 있습니다. 앞에서 echo 찍어놓은 부분이 잘 실행 된 것 같습니다.

 

 

config.toml 위치

관리자인 경우 /etc/gitlab-runner

 

 

cannot connect to the Docker daemon at unix:///var/run/docker.sock 에러나는 경우

위와 같이 cannot connect to the Docker daemon at unix:///var/run/docker.sock 에러나는 경우

 

아래 variables를 넣으면 해결됩니다.

variables:
  DOCKER_HOST: tcp://localhost:2375

아래 링크를 참조 했습니다.

https://gitlab.com/gitlab-org/charts/gitlab/-/issues/478

 

 

 

주의

sudo gitlab-runner register로 해주지 않으면 New runner. Has not connected yet. 가 나올 수 있습니다.

이렇게 나오는 이유는 주로 sudo로 안띄웠기 때문

 

 

vue앱 빌드하고 배포(cache 사용)

cache:
  key: ${CI_COMMIT_REF_SLUG}
  paths:
  - node_modules/
  - .next/

stages:          # List of stages for jobs, and their order of execution
  - install
  - build
  - deploy

build:       # This job runs in the build stage, which runs first.
  stage: build
  tags:
    - eh3
  script:
    - echo "Compiling the code..."
    - npm run build
    - cp -r dist/* /var/www/html/

deploy-job:      # This job runs in the deploy stage.
  stage: deploy  # It only runs when *both* jobs in the test stage complete successfully.
  tags:
    - eh3
  script:
    - echo "deploy"
    # - rm -r /var/www/html/*
    - pwd

 

Gradle

image: gradle:jdk11

stages:
  - build
  - package
  # - deploy

cache:
  paths:
    - .gradle/wrapper
    - .gradle/caches
    - build/

build:
  stage: build
  script:
    - gradle wrap
    - ./gradlew clean
    - ./gradlew assemble
  artifacts:
    paths:
      - build/libs/*.jar
    expire_in: 1 week
  only:
    - main

docker-build:
  only:
    - main
  image: docker:latest
  stage: package
  services:
    - docker:dind
  before_script:
    - apk update && apk add git
    - echo $CI_REGISTRY_PASSWORD | docker login $DOCKER_REGISTRY -u $CI_REGISTRY_USER --password-stdin
    - TAG=$(git log -2 -1 --pretty=%h)
  script:
    - docker pull registry.gitlab.com/leo.kim1/osc-prj-backend:latest || true
    - docker image tag registry.gitlab.com/leo.kim1/osc-prj-backend:latest registry.gitlab.com/leo.kim1/osc-prj-backend:$TAG
    - docker build --tag registry.gitlab.com/leo.kim1/osc-prj-backend:latest .
    - docker push registry.gitlab.com/leo.kim1/osc-prj-backend:$TAG
    - docker push registry.gitlab.com/leo.kim1/osc-prj-backend:latest

 

대상 디렉토리 권한 바꾸기

gitlab-runner로 배포 할 때 

chown -R gitlab-runner html

 

docker그룹에 gitlab-runner추가

usermod -aG docker gitlab-runner

 

문제 해결

로그보기

journalctl -u gitlab-runner

 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함