109 lines
4.1 KiB
Python
109 lines
4.1 KiB
Python
"""Jenkins job Gitea 트리거 설정 + Deploy 스테이지 검증"""
|
|
import paramiko, sys, json, time
|
|
sys.stdout.reconfigure(encoding='utf-8', errors='replace')
|
|
c = paramiko.SSHClient(); c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
c.connect('101.79.17.164', username='root', password='1q2w3e!Q', timeout=15)
|
|
sftp = c.open_sftp()
|
|
|
|
J = 'http://127.0.0.1:9080'
|
|
A = 'admin:Admin@2026!'
|
|
|
|
def run(label, cmd, timeout=30):
|
|
print(f'\n[{label}]')
|
|
_, o, _ = c.exec_command(cmd, timeout=timeout)
|
|
out = o.read().decode('utf-8','replace').strip()
|
|
if out: print(out[:800])
|
|
return out
|
|
|
|
# crumb
|
|
_, o, _ = c.exec_command(f'curl -sf -u "{A}" {J}/crumbIssuer/api/json 2>/dev/null', timeout=10)
|
|
try:
|
|
cd = json.loads(o.read().decode('utf-8','replace').strip())
|
|
CRUMB_H = f'{cd["crumbRequestField"]}: {cd["crumb"]}'
|
|
except:
|
|
CRUMB_H = 'Jenkins-Crumb: x'
|
|
|
|
# 1. build #3 Deploy 스테이지 결과 확인
|
|
run('build #3 콘솔 로그 (Deploy)',
|
|
f'curl -sf -u "{A}" {J}/job/guardia-itsm/3/consoleText 2>/dev/null | '
|
|
"grep -E 'Deploy|Branch|GIT_BRANCH|skipped|rsync|SUCCESS|FAILURE' | head -15")
|
|
|
|
# 2. Jenkins Groovy Script으로 Gitea trigger 설정
|
|
groovy = """
|
|
import jenkins.model.*
|
|
import com.cloudbees.jenkins.gitea.*
|
|
|
|
def jenkins = Jenkins.instance
|
|
def repos = ['guardia-itsm', 'zioinfo-web', 'guardia-manager', 'guardia-messenger', 'guardia-docs']
|
|
|
|
repos.each { name ->
|
|
def job = jenkins.getItem(name)
|
|
if (!job) { println "NOT FOUND: ${name}"; return }
|
|
|
|
// Gitea webhook trigger 추가
|
|
try {
|
|
def triggerClass = GiteaWebHookTrigger.class
|
|
def existing = job.triggers.find { it.class == triggerClass }
|
|
if (!existing) {
|
|
def trigger = new GiteaWebHookTrigger()
|
|
job.addTrigger(trigger)
|
|
println "Added Gitea trigger: ${name}"
|
|
} else {
|
|
println "Already has trigger: ${name}"
|
|
}
|
|
} catch (Exception e) {
|
|
println "Error ${name}: ${e.message}"
|
|
}
|
|
job.save()
|
|
}
|
|
jenkins.save()
|
|
"""
|
|
with sftp.open('/tmp/trigger_setup.groovy', 'w') as f:
|
|
f.write(groovy)
|
|
|
|
run('Gitea trigger Script Console 실행',
|
|
f'curl -sf -X POST "{J}/scriptText" -u "{A}" '
|
|
f'-H "{CRUMB_H}" '
|
|
'--data-urlencode "script@/tmp/trigger_setup.groovy" 2>/dev/null')
|
|
|
|
# 3. 대안: config.xml 직접 수정으로 trigger 추가
|
|
REPOS = ['guardia-itsm', 'zioinfo-web', 'guardia-manager', 'guardia-messenger', 'guardia-docs']
|
|
for repo in REPOS:
|
|
# 현재 config.xml 가져오기
|
|
_, o, _ = c.exec_command(f'curl -sf -u "{A}" {J}/job/{repo}/config.xml 2>/dev/null', timeout=10)
|
|
config = o.read().decode('utf-8','replace')
|
|
|
|
if '<triggers/>' in config and 'GiteaWebHookTrigger' not in config:
|
|
# Gitea trigger 추가
|
|
config_fixed = config.replace(
|
|
'<triggers/>',
|
|
'<triggers><com.cloudbees.jenkins.gitea.GiteaWebHookTrigger plugin="gitea"><properties/></com.cloudbees.jenkins.gitea.GiteaWebHookTrigger></triggers>'
|
|
)
|
|
with sftp.open(f'/tmp/{repo}_config.xml', 'w') as f:
|
|
f.write(config_fixed)
|
|
run(f'{repo} config.xml 업데이트',
|
|
f'curl -sf -X POST -u "{A}" -H "{CRUMB_H}" '
|
|
f'-H "Content-Type: text/xml" '
|
|
f'--data-binary @/tmp/{repo}_config.xml '
|
|
f'"{J}/job/{repo}/config" 2>/dev/null && echo "OK" || echo "FAIL"')
|
|
else:
|
|
print(f'\n[{repo}]: trigger 이미 설정됨 또는 수정 불필요')
|
|
|
|
# 4. E2E 검증: Gitea에 실제 commit push → 자동 빌드 트리거 확인
|
|
run('E2E 테스트: deploy_server 트리거',
|
|
"curl -sf -X POST http://localhost:9999 "
|
|
"-H 'Content-Type: application/json' "
|
|
"-H 'X-Gitea-Event: push' "
|
|
"-d '{\"repository\":{\"name\":\"guardia-itsm\"},\"ref\":\"refs/heads/main\"}' 2>/dev/null")
|
|
|
|
time.sleep(12)
|
|
run('배포 + Jenkins 빌드 결과',
|
|
f'echo "=배포로그="; tail -5 /var/log/zioinfo/deploy.log; '
|
|
f'echo "=Jenkins="; curl -sf -u "{A}" {J}/job/guardia-itsm/lastBuild/api/json 2>/dev/null | '
|
|
'python3 -c "import sys,json; d=json.load(sys.stdin); '
|
|
'print(\'build #\'+str(d[\'number\']), d[\'result\'], \'building:\', d[\'building\'])" 2>/dev/null')
|
|
|
|
sftp.close()
|
|
c.close()
|
|
print('\n=== 완료 ===')
|