(작성중) jenkins
2026. 3. 18. 15:51네, Jenkins와 GitHub를 연동하여 GitHub Issues에 댓글을 남기는 것이 가능합니다.
방법 개요
필요한 것:
GitHub Personal Access Token (PAT) — repo 스코프 필요
Jenkins Credentials에 PAT 등록
GitHub REST API 호출 (POST /repos/{owner}/{repo}/issues/{issue_number}/comments)
1. GitHub PAT 생성
GitHub → Settings → Developer settings → Personal access tokens → repo 권한 부여
2. Jenkins Credentials 등록
Jenkins → Manage Jenkins → Credentials → Secret text로 PAT 등록
ID 예시: github-pat
3. Jenkinsfile 예시
pipeline {
agent any
environment {
GITHUB_TOKEN = credentials('github-pat') // Jenkins Credential ID
REPO_OWNER = 'your-org'
REPO_NAME = 'your-repo'
ISSUE_NUMBER = '42'
}
stages {
stage('Comment on GitHub Issue') {
steps {
script {
def comment = "✅ Jenkins 빌드 성공! Build #${env.BUILD_NUMBER}"
postGithubComment(comment)
}
}
}
}
post {
failure {
script {
def comment = "❌ Jenkins 빌드 실패! Build #${env.BUILD_NUMBER} - [로그 확인](${env.BUILD_URL})"
postGithubComment(comment)
}
}
}
}
def postGithubComment(String body) {
def payload = groovy.json.JsonOutput.toJson([body: body])
sh """
curl -s -X POST \\
-H "Authorization: token \${GITHUB_TOKEN}" \\
-H "Accept: application/vnd.github+json" \\
-H "Content-Type: application/json" \\
-d '${payload}' \\
"https://api.github.com/repos/\${REPO_OWNER}/\${REPO_NAME}/issues/\${ISSUE_NUMBER}/comments"
"""
}
4. 동적으로 Issue 번호를 파라미터로 받는 방법
pipeline {
agent any
parameters {
string(name: 'ISSUE_NUMBER', defaultValue: '', description: 'GitHub Issue 번호')
}
environment {
GITHUB_TOKEN = credentials('github-pat')
}
stages {
stage('Comment') {
when {
expression { params.ISSUE_NUMBER != '' }
}
steps {
script {
def body = groovy.json.JsonOutput.toJson([
body: "🔧 빌드 #${env.BUILD_NUMBER} 완료"
])
sh """
curl -s -X POST \\
-H "Authorization: token \${GITHUB_TOKEN}" \\
-H "Accept: application/vnd.github+json" \\
-d '${body}' \\
"https://api.github.com/repos/YOUR_ORG/YOUR_REPO/issues/${params.ISSUE_NUMBER}/comments"
"""
}
}
}
}
}
주의사항
항목
내용
Token 보안
sh 블록 내 ${GITHUB_TOKEN} 직접 출력 금지, credentials() 사용
Groovy sandbox
JsonOutput 사용 시 Approve 필요할 수 있음
API Rate Limit
GitHub API는 인증 시 시간당 5,000 요청 제한
Fine-grained PAT
Classic PAT 대신 사용 시 issues: write 권한 필요
추가로 PR 댓글, Issue 상태 변경(close/open), 라벨 추가 등도 같은 방식으로 REST API 호출이 가능합니다. 필요하시면 말씀해 주세요!