Gerrit에 업로드한 Jenkinsfile(groovy 파일)을 Jenkins pipeline script로 사용하도록 셋업하면서 겪은 시행 착오를 정리합니다.
1) Jenkins에서는 Pipeline script from SCM 에서 SCM 항목에 Gerrit이 따로 없다. 따라서 Git을 설정하면 됨
1. Repository: Gerrit SSH Repository URL
ssh://${GERRIT_USERNAME}@${GERRIT_SERVER}:${GERRIT_SSH_PORT}/${GERRIT_REPOSITORY_NAME}
ex) ssh://jenkins@my-gerrit-server:29418/my-gerrit-project
2. Credentials: Gerrit SSH Key 또는 HTTP 계정
ex) Gerrit에 등록한 SSH public key의 쌍인 private key 또는 Gerrit Web 접속할때 로그인하는 ID/PW
3. Branch Specifier
Jenkinsfile이 push된 branch
ex) main, master, dev ...
4. Script Path
Jenkinsfile의 위치
ex) jenkins/my-test-file.groovy
2) agent 가 jenkins master와 다른 host pc에 있더라도 SSH public key는 jenkins master에서 생성한 것 1개만 Gerrit에 등록하면 됨
왜냐하면 Pipeline Script from SCM은 SCM checkout을 수행하는 시점에만 Gerrit 접근이 필요한데, 이 과정에서는 "Jenkins Credential"(1-2) 을 사용한다.
Gerrit
↑
| (SSH) Jenkins Credential 사용
|
Jenkins Controller (Master)
↓
| 전달
|
Jenkins Agent (다른 Host PC)
3) 위와 같이 설정한 뒤 실행 시, Host key verification failed, could not read from remote repository 에러 발생하는 경우
원인1. Jenkinsfile 의 agent가 직접 git 명령어를 사용하는 경우 (이 때는 Agent 의 SSH key를 사용하므로, Gerrit에 등록되지 않으면 실패함)
원인2. agent에 Gerrit known_hosts 미등록된 경우
agent의 known_hosts에 Gerrit 추가
ssh-keyscan -p ${GERRIT_SSH_PORT} ${GERRIT_IP} >> ${known_hosts_PATH}
ex) ssh-keyscan -p 29418 gerrit.myserver.com >> ~/.ssh/known_hosts
4) Lightweight Checkout 끄기
- Pipeline script checkout 자체가 agent에서 수행됨
- 즉, agent가 직접 Gerrit repo 를 checkout 함
- 그 후 stages 도 같은 agent에서 실행되므로 문제 없음
| lightweight checkout ON | lightweight checkout OFF |
| controller가 Jenkinsfile만 가져옴 | agent에서 repository full clone |
| agent workspace 비어 있음 | agent workspace에 전체 소스 존재 |
| stage 내부에서 git checkout 필요 | stage에서 바로 파일 사용 가능 |
| SCM credential 사용 제한적 | SCM credential 완전 지원(SSH key 포함) |
| SSH host verification 문제 발생 가능 | 정상 작동 |
Relates to...
2023.06.13 - [Jenkins] - Jenkins Gerrit 서버 등록 에러 (id_rsa is not a valid key file)
Jenkins Gerrit 서버 등록 에러 (id_rsa is not a valid key file)
[문제] Jenkins Gerrit Trigger plugin을 사용하기 위해 Gerrit 서버를 등록하는데 "/var/lib/jenkins/.ssh/id_rsa" is not a valid key file. 에러 발생 [원인] id_rsa 키가 openssh private key로 등록되었기때문 $ cat id_rsa -----BEGIN
thxxyj.tistory.com
2024.02.28 - [Jenkins] - Jenkins Pipeline script from SCM
Jenkins Pipeline script from SCM
Pipeline script from SCM 설정 가이드 SCM: git Repoitory URL: https://...git Credentials 추가 - Kind: Username with password - Username: github 계정 이름 - Password: github token (발급방법: 2022.10.10 - [Jenkins] - Jenkins - GitHub 연동하
thxxyj.tistory.com
'Jenkins' 카테고리의 다른 글
| Jenkins JOB_NAME 가져오기 (0) | 2024.05.14 |
|---|---|
| Jenkins declarative parallel build pipeline (0) | 2024.04.30 |
| Jenkins stage result FAILURE 표시 후 다음 stage 진행하는 법 (0) | 2024.04.29 |
| Jenkins stage/step SKIP 하기 (0) | 2024.04.29 |
| 현재시간, 1시간 전, 1일 전 datetime 을 원하는 포맷으로 구하기 (0) | 2024.03.25 |