@@ -80,3 +80,39 @@ jobs:
|
|||||||
KG_CD_SSH_HOST: ${{ vars.KG_CD_SSH_HOST }}
|
KG_CD_SSH_HOST: ${{ vars.KG_CD_SSH_HOST }}
|
||||||
KG_CD_SSH_PORT: ${{ vars.KG_CD_SSH_PORT }}
|
KG_CD_SSH_PORT: ${{ vars.KG_CD_SSH_PORT }}
|
||||||
run: python3 scripts/production_cd.py send --event "$GITHUB_EVENT_PATH"
|
run: python3 scripts/production_cd.py send --event "$GITHUB_EVENT_PATH"
|
||||||
|
notify:
|
||||||
|
needs: [guard, publish]
|
||||||
|
if: always()
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
timeout-minutes: 5
|
||||||
|
steps:
|
||||||
|
- name: 取同一提交的上报脚本
|
||||||
|
run: |
|
||||||
|
set -eu
|
||||||
|
test "$GITHUB_EVENT_NAME" = workflow_dispatch
|
||||||
|
test "$GITHUB_REPOSITORY" = Weknora/knowledge-governance
|
||||||
|
test "$GITHUB_REF" = refs/heads/main
|
||||||
|
git init -q .
|
||||||
|
git fetch -q --depth 200 "$GITHUB_SERVER_URL/$GITHUB_REPOSITORY.git" refs/heads/main
|
||||||
|
git checkout -q --detach "$GITHUB_SHA"
|
||||||
|
test "$(git rev-parse HEAD)" = "$GITHUB_SHA"
|
||||||
|
- name: 上报 ci-bridge(发布结果,通知失败不改变发布结论)
|
||||||
|
if: always()
|
||||||
|
env:
|
||||||
|
BRIDGE_URL: ${{ vars.CI_BRIDGE_URL }}
|
||||||
|
BRIDGE_TOKEN: ${{ secrets.CI_BRIDGE_TOKEN }}
|
||||||
|
CI_PUBLIC_BASE_URL: https://gitea.zheromiku.top
|
||||||
|
GUARD_RESULT: ${{ needs.guard.result }}
|
||||||
|
PUBLISH_RESULT: ${{ needs.publish.result }}
|
||||||
|
DEPLOY_SHA: ${{ inputs.sha }}
|
||||||
|
DEPLOY_BUNDLE: ${{ inputs.bundle }}
|
||||||
|
DEPLOY_TASK: ${{ inputs.task }}
|
||||||
|
run: |
|
||||||
|
set +e; set +o pipefail
|
||||||
|
B="${BRIDGE_URL:-}"
|
||||||
|
for cand in "$B" "http://172.17.0.1:8899" "http://192.168.50.134:8899" "http://100.92.49.78:8899"; do
|
||||||
|
if [ -n "$cand" ]; then curl -s -m 5 -o /dev/null "$cand/healthz" && { B="$cand"; break; }; fi
|
||||||
|
done
|
||||||
|
echo "使用 ci-bridge: ${B:-(不可达)}"
|
||||||
|
CI_BRIDGE_RESOLVED="$B" python3 scripts/cd_notify.py
|
||||||
|
echo "上报结束(通知失败不改变发布结论)"
|
||||||
|
|||||||
@@ -0,0 +1,93 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""把一次发布运行的结果上报 ci-bridge,与 build 的通知同一出口。
|
||||||
|
|
||||||
|
发布失败也必须上报:群里要知道是「发布失败」而不是没有消息。
|
||||||
|
上报失败只打印,不改变发布的结论。
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import subprocess
|
||||||
|
import sys
|
||||||
|
import urllib.error
|
||||||
|
import urllib.request
|
||||||
|
|
||||||
|
|
||||||
|
def env(name, default=''):
|
||||||
|
return (os.environ.get(name) or default).strip()
|
||||||
|
|
||||||
|
|
||||||
|
def git(*args):
|
||||||
|
try:
|
||||||
|
return subprocess.check_output(['git', *args], text=True, stderr=subprocess.DEVNULL).strip()
|
||||||
|
except (OSError, subprocess.CalledProcessError):
|
||||||
|
return ''
|
||||||
|
|
||||||
|
|
||||||
|
def result_status(value):
|
||||||
|
return {'success': 'success', 'failure': 'failure', 'cancelled': 'cancelled'}.get(value, 'failure')
|
||||||
|
|
||||||
|
|
||||||
|
def payload():
|
||||||
|
deploy_sha = env('DEPLOY_SHA') or env('GITHUB_SHA')
|
||||||
|
publish = result_status(env('PUBLISH_RESULT'))
|
||||||
|
guard = result_status(env('GUARD_RESULT'))
|
||||||
|
status = publish if publish != 'success' else guard
|
||||||
|
subject = git('log', '-1', '--format=%s', deploy_sha) if deploy_sha else ''
|
||||||
|
steps = []
|
||||||
|
for name, value in (('guard', env('GUARD_RESULT')), ('publish', env('PUBLISH_RESULT'))):
|
||||||
|
steps.append({'name': name, 'status': result_status(value), 'duration_s': ''})
|
||||||
|
return {
|
||||||
|
'repo': env('GITHUB_REPOSITORY', 'unknown/unknown'),
|
||||||
|
'sha': deploy_sha,
|
||||||
|
'branch': 'main',
|
||||||
|
'actor': env('GITHUB_ACTOR'),
|
||||||
|
'workflow': 'deploy-production',
|
||||||
|
'status': status,
|
||||||
|
'commit_subject': subject,
|
||||||
|
'commit_count': '1',
|
||||||
|
'duration_s': '',
|
||||||
|
'preview_url': '',
|
||||||
|
'steps': steps,
|
||||||
|
'bundle': env('DEPLOY_BUNDLE'),
|
||||||
|
'task': env('DEPLOY_TASK'),
|
||||||
|
'run_id': env('GITHUB_RUN_ID'),
|
||||||
|
'run_number': env('GITHUB_RUN_NUMBER'),
|
||||||
|
'run_attempt': env('GITHUB_RUN_ATTEMPT', '1'),
|
||||||
|
'commit_url': f"{env('CI_PUBLIC_BASE_URL', env('GITHUB_SERVER_URL'))}/{env('GITHUB_REPOSITORY')}/commit/{deploy_sha}",
|
||||||
|
'run_url': f"{env('CI_PUBLIC_BASE_URL', env('GITHUB_SERVER_URL'))}/{env('GITHUB_REPOSITORY')}/actions/runs/{env('GITHUB_RUN_ID')}",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def post(data, base, token):
|
||||||
|
body = json.dumps(data, ensure_ascii=False).encode()
|
||||||
|
req = urllib.request.Request(base.rstrip('/') + '/api/ci/report', data=body, method='POST',
|
||||||
|
headers={'Content-Type': 'application/json', 'X-CI-Token': token})
|
||||||
|
with urllib.request.urlopen(req, timeout=30) as response:
|
||||||
|
return response.read().decode()[:300]
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
parser = argparse.ArgumentParser(description=__doc__)
|
||||||
|
parser.add_argument('--no-post', action='store_true')
|
||||||
|
args = parser.parse_args()
|
||||||
|
data = payload()
|
||||||
|
if args.no_post:
|
||||||
|
print(json.dumps(data, ensure_ascii=False, indent=2))
|
||||||
|
return 0
|
||||||
|
base = env('CI_BRIDGE_RESOLVED') or env('BRIDGE_URL') or env('CI_BRIDGE_URL')
|
||||||
|
if not base:
|
||||||
|
print('跳过上报:未找到 ci-bridge 地址', file=sys.stderr)
|
||||||
|
return 0
|
||||||
|
try:
|
||||||
|
print('✅ 已上报 ci-bridge:' + post(data, base, env('BRIDGE_TOKEN')))
|
||||||
|
except (urllib.error.URLError, OSError) as error:
|
||||||
|
# 通知失败不改变发布结论,只记一行。
|
||||||
|
print(f'❌ 上报 ci-bridge 失败(发布结论不变):{error}', file=sys.stderr)
|
||||||
|
return 0
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
sys.exit(main())
|
||||||
Reference in New Issue
Block a user