본문 바로가기
CI CD

Jenkins [2] gitlab 서버 연동

by jongSuN 2021. 10. 1.

방법 1

1) gitlab에서 Access Token 발급

  1.  개인 계정으로 접속
  2.  Settings -> Access Token
  3. 모든 값 입력
  4. Scopes 모두 체크
  5. Create project access token 활성화 되면 클릭하여 token 생성
  6. 해당 페이지를 계속 띄우고 있거나 토큰 복사

 

2) Jenkins에서 gitlab에서 받은 Token 넣기

  1. Jenkins 관리 -> Manage Credentials
  2. Domain 컬럼에 마우스 놓고 아래 화살표 뜨면 클릭하여 Add credentials 클릭
  3. Kind를 GitLab API token 으로 설정
  4. Scope는 Global (Jenkins, nodes, items all child items, etc) 로 설정
  5. API token에 1) 에서 받은 토큰 붙여넣기

 

3) Jenkins 에서 추가한 credentials을 setting에서 설정

  1. Jenkins 관리 -> 시스템 설정
  2. GItLab 부분으로 가서 세팅 시작
  3. Enable authentication for '/project' end-point 선택
  4. Connection name 입력
  5. gitlab Host URL
    1. GitLab IP 복사해서 입력
  6. Credentials : GitLab API token (gitlab)으로 설정

 

4) Jenkins에서 자동 Build trigger 되도록 설정

  1. 왼쪽 위의 Jenkins 클릭하여 첫 페이지로 이동
  2. 첫 페이지에서 해당 아이디에서 만든 Job(Jenkins의 단위)들이 나오는데 없으면 생성하기
  3. 해당 Job의 속성 들어가서 값 세팅
  4. Use alternative credential 클릭
  5. Credential 에서 Gitlab API token (gitlaba)으로 설정 (위에서 입력한 token 세팅)

6. 빌드 유발 세팅

  1. Build when a change is pushed to GitLab, GitLab webhook URL : http://ip/project경로/project 체크
  2. 위의 URL는 다음 단계에서 GitLab에 입력해야 하기 때문에 복사
  3. Push Events 체크
  4. Opened Merge Request Events 체크
  5. Approved Merge Requests (EE-only) 체크
  6. Comments 체크
  7. Comment (regex) for triggering a build : Jenkins please retry a build 로 세팅
  8. 나머지는 기본값으로 세팅

5) GitLab에서 webhook 생성

    (Gitlab의 프로젝트에 push 시 빌드를 할 수 있도록 만들어야 할 경우)

<webhook은 간단히 웹상의 trigger 라고 이해하면 됨>

  1. Gitlab 개인계정으로 접속
  2. 해당 프로젝트 -> settings -> Integrations 들어가기
  3. Webhooks have moved they can now be found under the Settings menu. 밑에 있는 Go to Webhooks 클릭
  4. 위의 6. 빌드 유발 세팅 - 1 의 URL 붙여넣기
  5. Secret toekn 값은 Jenkins에서 받아야 함
    1. Jenkins 접속
    2. 해당 프로젝트와 연결할 job의 구성 들어가기
    3. 위에 빌드 유발 클릭하면 해당 부분으로 화면이 이동함
    4. Comment (regex) for triggering a build 밑에 [고급. . .] 클릭
    5. 새로운 부분이 나오는데 Secret token 아래 오른쪽 [Generate] 버튼 클릭
    6. Secret token 생성되면 복사
  6. 복사한 Secret token을 Gitlab에 붙여넣기
  7. Add webhook 클릭

방법 2

  1. Jenkins 관리 -> Manage Credentials 들어가기
  2. Domain 속성에 (global)값에 마우스를 대면 Add credentials 클릭
  3. Kind : username with password 선택
  4. Scope : Global (Jenkins, nodes, items, all child items, etc)
  5. 나머지 Gitlab으로 작성 후 저장
  6. 연동할 Job 구성으로 가서 소스 코드 관리에서 Git 선택
  7. Repository URL 에서 해당 프로젝트 http url 복사하여 붙여넣기
  8. Credentials 에 위에서 저장한 credentials 넣고 저장

 

 


Error 해결

1. Add webhook 클릭을 했는데 생성이 안되고 에러가 나는 경우

url is blocked : requests to localhost are not allowed

해결방안

  1. GitlLab 에 root 계정으로 접속
  2. Settings -> Outbound requests 에서 Expand 클릭하고
  3. Allow requests to the local network from hooks and services 체크
  4. gitlab 서버에 접속하여 gitlab 재설정 후 재시작
sudo su gitlab-ctl reconfigure
sudo su gitlab-ctl restart

 


 

참조

https://tech.osci.kr/2020/01/16/86039236/

'CI CD' 카테고리의 다른 글

Jenkins [1] 설치  (0) 2021.10.01

댓글