소개
Jenkins에서 Github Organization을 연결하는 방법을 알아보자
1.Github setting
1.1. github setting 클릭
![](https://velog.velcdn.com/images/padomay1352/post/55c83fd2-43de-4430-ab5c-1352a3c35b78/image.png)
1.2. developer setting에서 GitHub Apps 클릭
![](https://velog.velcdn.com/images/padomay1352/post/5862290f-afa8-442c-82ea-d3b65fc42dbc/image.png)
1.3. New GitHub App 클릭
![](https://velog.velcdn.com/images/padomay1352/post/ccc18fb8-47af-4926-b574-d5597ca8f8e1/image.png)
1.4. GitHub App 생성
1.4.1. GitHub App Name : 원하는 이름입력
1.4.2. Homepage URL : Gihub Organization 주소입력
![](https://velog.velcdn.com/images/padomay1352/post/98908569-f4e5-4143-90b9-9948251e5309/image.png)
1.4.3. webhook 설정 : jenkins 주소를 입력하고 /github-webhook/ 을 붙인다
![](https://velog.velcdn.com/images/padomay1352/post/69e74d35-2fce-4904-a82a-13aab9e8dfbf/image.png)
1.4.4. Repository permission 설정
- Administration : Readonly
- Checks : Read and Write
![](https://velog.velcdn.com/images/padomay1352/post/574c93a5-09cb-48f0-bbda-cabfc036a7c1/image.png)
- Metadata : Read-only
![](https://velog.velcdn.com/images/padomay1352/post/48851236-420e-49c1-8ce1-d8c5cf6547cc/image.png)
- Pull Requests : Read-only
![](https://velog.velcdn.com/images/padomay1352/post/983f43c4-d51a-4081-b1f0-46010a412e44/image.png)
- Commit statuses : Read and write
![](https://velog.velcdn.com/images/padomay1352/post/19a1558f-2905-40d5-96d5-d1d8903af534/image.png)
- Webhooks : Read and write
1.4.5 Subscribe to events
- Check run
- Check suite
- Pull Request
- Push
- Repository
![](https://velog.velcdn.com/images/padomay1352/post/883e889a-346a-419d-8a0c-8e5532179fde/image.png)
![](https://velog.velcdn.com/images/padomay1352/post/973fde72-db9d-427e-b4f1-b9771f313239/image.png)
1.4.5. Create GitHub App
![](https://velog.velcdn.com/images/padomay1352/post/45893952-3d64-458e-87d5-873103ea9955/image.png)
1.5. GitHub App 설정
1.5.1 private key 생성
- Generate a privte key
![](https:/./velog.velcdn.com/images/padomay1352/post/9b933752-35f0-48ec-b743-21710168b836/image.png)
- 생성 & private key 다운로드
github
![](https://velog.velcdn.com/images/padomay1352/post/ea4a185f-eb21-45fd-abdd-20a388bab4a3/image.png)
download
![](https://velog.velcdn.com/images/padomay1352/post/43b2c67b-c4f8-444e-8cc3-f193105e54ca/image.png)
- 다운 받은 private key 변환
openssl pkcs8 -topk8 -inform PEM -outform PEM -in [downdload-key.pem] -out new-key.pem -nocrypt
cat new-key.pem
1.5.2. install app
- Install App 클릭
![](https://velog.velcdn.com/images/padomay1352/post/b1db2c3d-1f7e-4d33-8811-0906e67f2c55/image.png)
- repository 선택
전체 repository를 선택하거나 원하는 repository만 등록
![](https://velog.velcdn.com/images/padomay1352/post/edc06e00-d697-406f-ac15-3a1dc650a431/image.png)
- intall
2. Jenkins 설정
2.1. credentials 생성
![](https://velog.velcdn.com/images/padomay1352/post/acf43c93-9a08-414f-ac85-dd4e8e02f2a2/image.png)
2.1.2. Credentials 클릭
![](https://velog.velcdn.com/images/padomay1352/post/8338dcbc-d211-40a9-9af5-ba3948cfbf3b/image.png)
2.1.3. Global 클릭
![](https://velog.velcdn.com/images/padomay1352/post/01664cc6-dd4a-488a-941e-cd057311ae86/image.png)
2.1.4. Add Credentials 클릭
![](https://velog.velcdn.com/images/padomay1352/post/64dc74c6-c20a-4ffe-99ab-7a84feedccef/image.png)
2.1.5.값 입력
![](https://velog.velcdn.com/images/padomay1352/post/4b5e1a9a-c5f6-4d44-b9ea-fe11ede81dae/image.png)
2.1.5.1. Kind 선택
GitHub App 선택
2.1.6.1. 원하는 id 입력
ex) my-github-app
2.1.7.1. Description 추가
ex) this is my-github-app
2.1.8.1. App ID 입력
1. GitHub Organization에서 Settings 클릭
![](https://velog.velcdn.com/images/padomay1352/post/42df18fe-a12e-4e50-b5c5-f35b34507058/image.png)
2. GitHub Apps 클릭
![](https://velog.velcdn.com/images/padomay1352/post/e3136e37-7b0c-46d5-9e51-aa135f294a0b/image.png)
3. developer setting에서 GitHub Apps 클릭
![](https://velog.velcdn.com/images/padomay1352/post/5862290f-afa8-442c-82ea-d3b65fc42dbc/image.png)
![](https://velog.velcdn.com/images/padomay1352/post/aa0304a7-af20-4ac2-82d2-a730e9492063/image.png)
5. Edit 클릭
![](https://velog.velcdn.com/images/padomay1352/post/d9eddf05-0811-4979-b53b-f08d488dda30/image.png)
6. App id 복사
![](https://velog.velcdn.com/images/padomay1352/post/10fcf07a-828b-4ae9-add6-37b90b434a91/image.png)
7. jenkins에 App id 입력
8. new-key.pem 입력
9. Create
2.2. jenkins item 생성
2.2.1. 새로운 item 생성 클릭
![](https://velog.velcdn.com/images/padomay1352/post/61d8c916-52c7-4564-8443-a64e74a96f49/image.png)
2.2.2. item 생성
![](https://velog.velcdn.com/images/padomay1352/post/a3491f1a-27db-4e24-b5a5-7dd6499fad91/image.png)
2.2.3. project 값 입력
- Api endpoint : 비워둘 경우 알아서 채워짐
- credentials 앞에 생성한 것 선택
- owner : organization 이름 입력
![](https://velog.velcdn.com/images/padomay1352/post/0a615165-9ec7-42d3-8a30-011e64582864/image.png)
- 특정 레포지토리만 가져오도록 하도록 세팅
GitHub Organization -> Add 클릭 -> Repository -> filter by name (with wildcards)
![](https://velog.velcdn.com/images/padomay1352/post/5757b4d4-b508-43ad-8b5d-3e76202a5c7c/image.png)
2.2.4 원할 경우 쭉 내린뒤 특정 branch만 필터링
GitHub Organization -> Add 클릭 -> -> Winthin repository -> filter by name (with wildcards)
![](https://velog.velcdn.com/images/padomay1352/post/1a6962e3-f893-4578-be0a-3b4ca7bff932/image.png)
2.2.4 생성후 스캔
레포지토리는 잘 스캔 되지만 Jenkinsfile을 찾을 수가 없어서 CI를 수행 할 수 없다.
![](https://velog.velcdn.com/images/padomay1352/post/1f5ebb95-2d98-4bc3-a14f-daed07da448b/image.png)
3. Jenkinsfile 생성 및 테스트
3.1. repository 최상위에 Jenkinsfile 생성 & 작성
![](https://velog.velcdn.com/images/padomay1352/post/ef0e5d19-baff-4697-b171-45a66f612501/image.png)
3.2. 테스트를 위한 branch 생성 후 이동
git switch -c feature/jenkins
3.3. commit후 push
git commit -a -m "[test] Jenkinsfile 테스트"
git push
3.4. Scan Organization Now
Jenkinsfile을 올린 후 다시 scan 해본다.
![](https://velog.velcdn.com/images/padomay1352/post/a715aea1-049e-487f-b3ed-8c732ff41bbb/image.png)
3.5. 확인
picky-backend repository에 feature/jenkins branch가 확인 된다.
![]()
3.6 빌드 결과
![](https://velog.velcdn.com/images/padomay1352/post/c750f22e-2844-4303-8e6a-3d6684d6425f/image.png)
4. Jenkinsfile stage 작성 완료
def PROJECT_NAME = 'picky-0.0.1-SNAPSHOT
pipeline{
agent any
stages{
stage('Prepare'){
steps {
sh 'gradle clean'
}
}
stage('Build') {
steps {
sh 'gradlew build -x test'
}
}
stage('Test') {
steps {
sh 'gradlew test'
}
}
stage('Deploy Prepare'){
steps{
sh 'sudo kill $(pgrep -f ${PROJECT_NAME})'
}
}
stage('Deploy') {
steps {
sh 'nohup java -jar ./build/libs/${PROJECT_NAME}.jar &'
}
}
}
}
참조
https://www.youtube.com/watch?v=LbXKUKQ24T8&t=282s