From d488d0d4885746fd79303c8d329d9f8817c130bc Mon Sep 17 00:00:00 2001 From: Xiaoxia AI Date: Mon, 15 Jun 2026 20:02:04 +0800 Subject: [PATCH] fix: use manual checkout for gitea actions --- .gitea/workflows/deploy.yml | 50 ++++++++++++++++++++++++++++-------- .gitea/workflows/tests.yml | 51 ++++++++++++++++++++----------------- docs/CI-CD.md | 2 +- 3 files changed, 69 insertions(+), 34 deletions(-) diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index 160496bc1..10d36edb0 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -14,14 +14,29 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v3 - with: - github-server-url: https://api.xiaoxiajianji.com/git + shell: bash + run: | + set -euo pipefail + repo_url="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git" + auth_header="AUTHORIZATION: basic $(printf 'x-access-token:%s' "$GITHUB_TOKEN" | base64 -w0)" + git init . + git remote add origin "$repo_url" + git -c http.https://api.xiaoxiajianji.com/.extraheader="$auth_header" fetch --depth=1 origin "$GITHUB_SHA" + git checkout --force FETCH_HEAD - name: Sync code to staging workspace + shell: bash run: | - mkdir -p /var/lib/xiaoxia-saas-staging/repo - rsync -a --delete --exclude '.git' ./ /var/lib/xiaoxia-saas-staging/repo/ + set -euo pipefail + python3 - <<'PY' +from pathlib import Path +import shutil +root = Path('/var/lib/xiaoxia-saas-staging/repo') +if root.exists(): + shutil.rmtree(root) +root.mkdir(parents=True, exist_ok=True) +PY + tar --exclude=.git -cf - . | tar -xf - -C /var/lib/xiaoxia-saas-staging/repo - name: Verify staging env file run: | @@ -69,14 +84,29 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v3 - with: - github-server-url: https://api.xiaoxiajianji.com/git + shell: bash + run: | + set -euo pipefail + repo_url="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git" + auth_header="AUTHORIZATION: basic $(printf 'x-access-token:%s' "$GITHUB_TOKEN" | base64 -w0)" + git init . + git remote add origin "$repo_url" + git -c http.https://api.xiaoxiajianji.com/.extraheader="$auth_header" fetch --depth=1 origin "$GITHUB_SHA" + git checkout --force FETCH_HEAD - name: Sync code to production workspace + shell: bash run: | - mkdir -p /var/lib/xiaoxia-saas-production/repo - rsync -a --delete --exclude '.git' ./ /var/lib/xiaoxia-saas-production/repo/ + set -euo pipefail + python3 - <<'PY' +from pathlib import Path +import shutil +root = Path('/var/lib/xiaoxia-saas-production/repo') +if root.exists(): + shutil.rmtree(root) +root.mkdir(parents=True, exist_ok=True) +PY + tar --exclude=.git -cf - . | tar -xf - -C /var/lib/xiaoxia-saas-production/repo - name: Verify production env file run: | diff --git a/.gitea/workflows/tests.yml b/.gitea/workflows/tests.yml index fc52e0657..077faf728 100644 --- a/.gitea/workflows/tests.yml +++ b/.gitea/workflows/tests.yml @@ -9,61 +9,66 @@ on: jobs: test: runs-on: ubuntu-latest - + steps: - name: Checkout code - uses: actions/checkout@v3 - with: - github-server-url: https://api.xiaoxiajianji.com/git - + shell: bash + run: | + set -euo pipefail + repo_url="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git" + auth_header="AUTHORIZATION: basic $(printf 'x-access-token:%s' "$GITHUB_TOKEN" | base64 -w0)" + git init . + git remote add origin "$repo_url" + git -c http.https://api.xiaoxiajianji.com/.extraheader="$auth_header" fetch --depth=1 origin "$GITHUB_SHA" + git checkout --force FETCH_HEAD + - name: Set up Python uses: actions/setup-python@v4 with: python-version: '3.12' - + - name: Install dependencies run: | python -m pip install --upgrade pip pip install -r requirements.txt - + - name: Run tests run: | pytest tests/integration/ -v --cov=packages --cov=apps --cov-report=xml --cov-report=term - - - name: Upload coverage reports - uses: codecov/codecov-action@v3 - if: always() - with: - file: ./coverage.xml - fail_ci_if_error: false lint: runs-on: ubuntu-latest - + steps: - name: Checkout code - uses: actions/checkout@v3 - with: - github-server-url: https://api.xiaoxiajianji.com/git - + shell: bash + run: | + set -euo pipefail + repo_url="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}.git" + auth_header="AUTHORIZATION: basic $(printf 'x-access-token:%s' "$GITHUB_TOKEN" | base64 -w0)" + git init . + git remote add origin "$repo_url" + git -c http.https://api.xiaoxiajianji.com/.extraheader="$auth_header" fetch --depth=1 origin "$GITHUB_SHA" + git checkout --force FETCH_HEAD + - name: Set up Python uses: actions/setup-python@v4 with: python-version: '3.12' - + - name: Install linting tools run: | python -m pip install --upgrade pip pip install black flake8 mypy - + - name: Run Black (check only) run: | black --check packages/ apps/ tests/ - + - name: Run Flake8 run: | flake8 packages/ apps/ tests/ --max-line-length=120 --extend-ignore=E203,W503 - + - name: Run MyPy run: | mypy packages/ apps/ --ignore-missing-imports diff --git a/docs/CI-CD.md b/docs/CI-CD.md index 0e2deeb34..77b316669 100644 --- a/docs/CI-CD.md +++ b/docs/CI-CD.md @@ -80,7 +80,7 @@ mypy packages/ apps/ --ignore-missing-imports ## 故障排查 ### Actions 触发了但 checkout 失败 -优先检查 runner 能否从 job 容器访问 Gitea 实例地址;如果 Gitea 挂在 `/git` 这类子路径下,需要给 `actions/checkout` 显式传 `github-server-url`。 +优先检查 runner 能否从 job 容器访问 Gitea 实例地址;如果 Gitea 挂在 `/git` 这类子路径下,优先使用手写 `git fetch`,不要依赖 `actions/checkout` 自动拼接仓库地址。 ### Deploy 成功但业务链不通 优先检查: