"""Jenkins branch 조건 수정 + Gitea→Jenkins webhook 검증""" import paramiko, sys, json, base64, 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) 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[:600]) 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 = f'-H "{cd["crumbRequestField"]}: {cd["crumb"]}"' except: CRUMB = '' # 1. Gitea→Jenkins webhook 테스트 run('Gitea→Jenkins webhook 테스트', "curl -sf -X POST 'http://127.0.0.1:9003/api/v1/repos/zio/guardia-itsm/hooks/17/tests' " "--header 'Authorization: Basic $(echo -n zio:Zio@Admin2026! | base64)' " "2>/dev/null && echo '전송됨' || echo FAIL") time.sleep(3) run('Jenkins 빌드 큐', f'curl -sf -u "{A}" {J}/queue/api/json 2>/dev/null | ' 'python3 -c "import sys,json; items=json.load(sys.stdin).get(\'items\',[]); ' 'print(len(items), \'항목\'); [print(\' \',i[\'task\'][\'name\']) for i in items]" 2>/dev/null') # 2. GIT_BRANCH 환경변수 확인 run('GIT_BRANCH 환경변수', f'curl -sf -u "{A}" {J}/job/guardia-itsm/lastBuild/consoleText 2>/dev/null | ' "grep -i 'GIT_BRANCH\\|BRANCH_NAME\\|Checking out\\|at revision\\|origin' | head -5") # 3. Jenkinsfile 수정: branch 'main' → expression으로 교체 # 파일로 저장 후 Gitea API로 업데이트 JFILE = open('C:/GUARDiA/workspace/guardia-itsm/Jenkinsfile', encoding='utf-8').read() # when { branch 'main' } → when { expression { env.GIT_BRANCH ==~ /.*main/ } } JFILE_FIXED = JFILE.replace( "when { branch 'main' }", "when { expression { env.GIT_BRANCH ==~ /.*main/ || env.BRANCH_NAME == 'main' } }" ) if JFILE_FIXED != JFILE: print('\n[Jenkinsfile branch 조건 수정]') # workspace에 저장 with open('C:/GUARDiA/workspace/guardia-itsm/Jenkinsfile', 'w', encoding='utf-8') as f: f.write(JFILE_FIXED) with open('C:/GUARDiA/repos/guardia-itsm/Jenkinsfile', 'w', encoding='utf-8') as f: f.write(JFILE_FIXED) print(' workspace + repos 수정 완료') # Gitea API 업데이트 encoded = base64.b64encode(JFILE_FIXED.encode('utf-8')).decode() _, o, _ = c.exec_command( "curl -sf 'http://127.0.0.1:9003/api/v1/repos/zio/guardia-itsm/contents/Jenkinsfile' " "--header 'Authorization: Basic $(echo -n zio:Zio@Admin2026! | base64)' " "2>/dev/null | python3 -c \"import sys,json; print(json.load(sys.stdin)['sha'])\" 2>/dev/null", timeout=10) sha = o.read().decode('utf-8','replace').strip() payload = json.dumps({ "message": "ci: fix branch condition to work with regular pipeline jobs", "content": encoded, "sha": sha, "branch": "main" }) sftp = c.open_sftp() with sftp.open('/tmp/jf_itsm_fix.json', 'w') as f: f.write(payload) sftp.close() run('Gitea Jenkinsfile 업데이트', "curl -sf -X PUT 'http://127.0.0.1:9003/api/v1/repos/zio/guardia-itsm/contents/Jenkinsfile' " "--header 'Authorization: Basic $(echo -n zio:Zio@Admin2026! | base64)' " "--header 'Content-Type: application/json' " "--data @/tmp/jf_itsm_fix.json 2>/dev/null | " "python3 -c \"import sys,json; d=json.load(sys.stdin); print('OK:', d.get('content',{}).get('name','?'))\" 2>/dev/null") else: print('\n이미 수정된 상태') # 4. 빌드 트리거 + 결과 확인 run('빌드 트리거', f'curl -sf -X POST -u "{A}" {CRUMB} {J}/job/guardia-itsm/build 2>/dev/null && echo "트리거됨"') print('\n빌드 완료 대기...') for i in range(12): time.sleep(5) _, o, _ = c.exec_command( f'curl -sf -u "{A}" {J}/job/guardia-itsm/lastBuild/api/json 2>/dev/null', timeout=10) try: d = json.loads(o.read().decode('utf-8','replace')) if not d.get('building', True): print(f' build #{d["number"]}: {d["result"]}') break print(f' build #{d["number"]}: 진행중...') except: pass run('Deploy 스테이지 확인', f'curl -sf -u "{A}" {J}/job/guardia-itsm/lastBuild/consoleText 2>/dev/null | ' "grep -A2 'Deploy\\|skipped\\|rsync\\|guardia'") c.close() print('\n=== 완료 ===')