zioinfo-mail/scripts/push/push_jenkinsfiles_api.py
DESKTOP-TKLFCPR\ython 5561d0d050 feat(cicd): fix webhook server, git URLs, push Jenkinsfiles to all 5 Gitea repos
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-01 19:55:19 +09:00

89 lines
3.7 KiB
Python

"""Gitea Contents API로 Jenkinsfile 직접 업로드"""
import paramiko, sys, base64, json
sys.stdout.reconfigure(encoding='utf-8', errors='replace')
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('101.79.17.164', username='root', password='1q2w3e!Q', timeout=15)
def run(label, cmd, timeout=30):
print(f'[{label}]')
_, o, e = client.exec_command(cmd, timeout=timeout)
out = o.read().decode('utf-8', 'replace').strip()
if out: print(f' {out[:400]}')
return out
# Gitea API: bearer token 방식으로 처리 (@ 특수문자 문제 우회)
# 먼저 API token 확인 또는 생성
run('Gitea API 토큰 확인', """
curl -sf 'http://127.0.0.1:9003/api/v1/users/zio/tokens' \
--header 'Authorization: Basic '"$(echo -n 'zio:Zio@Admin2026!' | base64)" \
2>/dev/null | python3 -c "import sys,json; [print(t['name'], t['id']) for t in json.load(sys.stdin)]" 2>/dev/null || echo FAIL
""")
# API 토큰 생성
run('Gitea API 토큰 생성', """
curl -sf -X POST 'http://127.0.0.1:9003/api/v1/users/zio/tokens' \
--header 'Authorization: Basic '"$(echo -n 'zio:Zio@Admin2026!' | base64)" \
--header 'Content-Type: application/json' \
-d '{"name":"ci-deploy-token-2026","scopes":["write:repository","read:repository"]}' \
2>/dev/null | python3 -c "import sys,json; d=json.load(sys.stdin); print('token:', d.get('sha1','ERROR'))" 2>/dev/null || echo FAIL
""")
def rf(p): return open(p, encoding='utf-8').read()
JENKINSFILES = {
"guardia-manager": rf('C:/GUARDiA/workspace/guardia-manager/Jenkinsfile'),
"guardia-messenger":rf('C:/GUARDiA/workspace/guardia-messenger/Jenkinsfile'),
"guardia-docs": rf('C:/GUARDiA/workspace/guardia-docs/Jenkinsfile'),
"zioinfo-web": rf('C:/GUARDiA/workspace/zioinfo-web/Jenkinsfile'),
"guardia-itsm": rf('C:/GUARDiA/workspace/guardia-itsm/Jenkinsfile'),
}
print('\n[Gitea Contents API로 Jenkinsfile 업로드]')
for repo, content in JENKINSFILES.items():
b64 = base64.b64encode(content.encode('utf-8')).decode()
# 기존 파일 SHA 확인
sha_cmd = f"""curl -sf 'http://127.0.0.1:9003/api/v1/repos/zio/{repo}/contents/Jenkinsfile' \
--header 'Authorization: Basic '"$(echo -n 'zio:Zio@Admin2026!' | base64)" \
2>/dev/null | python3 -c "import sys,json; print(json.load(sys.stdin).get('sha',''))" 2>/dev/null"""
_, o, _ = client.exec_command(sha_cmd, timeout=10)
sha = o.read().decode('utf-8', 'replace').strip()
if sha:
# 파일 업데이트
payload = json.dumps({
"message": "ci: update Jenkinsfile for CI/CD pipeline",
"content": b64,
"sha": sha,
"branch": "main"
})
method = "PUT"
print(f' {repo}: 기존 파일 업데이트 (sha={sha[:8]}...)')
else:
# 파일 신규 생성
payload = json.dumps({
"message": "ci: add Jenkinsfile for CI/CD pipeline",
"content": b64,
"branch": "main"
})
method = "POST"
print(f' {repo}: 신규 파일 생성')
# payload를 임시 파일로 저장
sftp = client.open_sftp()
with sftp.open(f'/tmp/jf_{repo}.json', 'w') as f:
f.write(payload)
sftp.close()
run(f'upload {repo}', f"""
curl -sf -X {method} 'http://127.0.0.1:9003/api/v1/repos/zio/{repo}/contents/Jenkinsfile' \
--header 'Authorization: Basic '"$(echo -n 'zio:Zio@Admin2026!' | base64)" \
--header 'Content-Type: application/json' \
--data @/tmp/jf_{repo}.json \
2>/dev/null | python3 -c "import sys,json; d=json.load(sys.stdin); print('OK:', d.get('content',{{}}).get('name','?'))" 2>/dev/null || echo FAIL
rm -f /tmp/jf_{repo}.json
""")
client.close()
print('\n=== Jenkinsfile 업로드 완료 ===')