본문 바로가기
Develop/etc

[Jenkins] Git과 연동하기

by 연로그 2021. 2. 24.
반응형

목차

1. 연동하기

2. git webhook 설정하기

(push가 일어나면 Jenkins 자동 build 되게 하기)

 

 


Jenkins와 Git 연동하기

1. Item 생성

 

2. Git 추가

 

2-1 Failed to connect to repository: Command "git.exe ls-remote -h -- git@..." returned status code 128: stdout: stderr: Host key verification failed. fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists. 에러

 repository에 대한 permission이 없다. 권한이 있는 credentials를 등록하지 않았거나 올바르지 않은 레포지토리 링크를 넣었을 경우 나타나는 문제

-> Credentials Add로 해결

 

3. Build를 이용해 Git 연동 확인

3-1.  ERROR: Error fetching remote repo 'origin' ... 에러

올바르지 않은 깃 링크를 추가해서 나타난 문제였다.

2번으로 돌아가서 유효한 깃 링크와 credential을 추가하면 된다.

 

3-2. ERROR: Error cloning remote repo 'origin'... 에러

pc에 git설치되어 있지 않아 나타난 문제였다.ubuntu 기준으로 아래 명령어를 입력하면 된다.

$ sudo apt-get install git

 


Git webhook 설정

github master 브랜치에서 push가 일어나면 jenkins에서 자동적으로 빌드가 되도록 설정해보자.

 

 

1. profile - settings - developer settings - personal access tokens - generate new token

 

repo와 admin:repo_hook 선택 후 generate하면 token에 대한 문자열을 발급 받는다.

 

2. Jenkins로 돌아온 뒤, Jenkins 관리 - 시스템 설정 - GitHub Server Add - Credentials Add

 

 

3. Git Repository에서 Settings - Webhooks - Add webhook

Payload URL: 젠킨스주소/github-webhook/

Add webhook
성공 화면

 

Error #1 > There was an error setting up your hook: Sorry, the URL host localhost is not supported because it isn't reachable over the public Internet 

payload URL에 localhost나 127.0.0.1를 입력하면 안된다. (내 pc가 아닌 깃허브 서버의 localhost로 인식됨)

 

Error #2 > Last delivery was not successful. failed to connect

  1. 외부 IP에서 접속할 수 있는 Jenkins URL로 입력한다. (ngrok 등을 이용)
  2. 설정에 오타가 없나 검수한다.
  3. Payload URL에서 젠킨스링크/github-webhook/ 까지 붙여줬는지 확인

 

ngrok을 이용해 외부 ip로 만들기 ▼

더보기

설치 환경: Ubuntu 18.04.3

1. ngrok 설치

$ sudo snap install ngrok

 

2. Jenkins port 오픈

port 변경을 한 적이 없다면 default로 8080이 되어있을 것이다.

주어진 링크가 사용 가능한 시간은 8h이니 보통 테스트 용으로 사용한다고 한다.

$ ngrok http 8080
명령어 수행 결과
접속 테스트

 

 

4. Jenkins 프로젝트 설정 변경 (구성 - 빌드 유발)

 

5. Git에 push 작업을 한 뒤, Jenkins가 빌드되는 것을 확인하면 성공.


참조

설치: gintrie.tistory.com/7

ngrok 사용: vlee.kr/3907

반응형