Files
jaredandClaude Opus 5.5 379570bbdc CI modelled on Pulse, deploy script with rollback, bootstrap, README
- .gitea/workflows/ci.yml: flake8 + dash check, pytest (90% coverage floor), MariaDB
  integration tests, bandit, pip-audit, gitleaks, actionlint, shellcheck, zizmor, semgrep;
  main deploys through a signed webhook and waits for /health to report the commit.
- deploy/thor_deploy.sh: deploys an exact sha, installs units and nginx site when changed,
  rolls back unless /health reports the sha with a working database. Sandbox-tested.
- deploy/bootstrap.sh: idempotent first install on CT 173.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 22:35:28 -05:00

192 lines
8.2 KiB
YAML

name: CI
# One pipeline, modelled on Pulse's: lint, unit tests, integration tests against MariaDB,
# security checks and scans must all pass before main deploys. Actions are pinned to full
# commit SHAs and the job image to a digest (version in the trailing comment).
# Pushes build only main (which deploys); branches are built through their pull request.
on:
push:
branches: [main]
pull_request:
branches: ["**"]
schedule:
- cron: '0 6 * * 1' # weekly dependency audit against new advisories
permissions:
contents: read
concurrency:
group: ci-${{ github.ref }}
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}
# node:22-trixie: Node for the checkout action, Debian trixie for Python 3.13 (production).
jobs:
lint:
name: Lint (flake8, house style)
runs-on: ubuntu-latest
timeout-minutes: 15
container: node:22-trixie@sha256:f92a4668ece057eff6b83ed531b5f6277892b5c8fa8c1df4ea4ef470bf92b91e
steps:
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
with:
persist-credentials: false
- name: Python environment
run: scripts/ci/python-env.sh
- name: flake8
run: /tmp/venv/bin/flake8 .
- name: No em or en dashes (house style)
run: |
if git grep -nP '[\x{2013}\x{2014}]|&[mn]dash;|\\u201[34]|&#821[12];|&#x201[34];' -- .; then
echo "::error::Em or en dashes found (listed above). Use a hyphen, colon, comma or 'to'."
exit 1
fi
test:
name: Test (pytest)
runs-on: ubuntu-latest
timeout-minutes: 15
container: node:22-trixie@sha256:f92a4668ece057eff6b83ed531b5f6277892b5c8fa8c1df4ea4ef470bf92b91e
steps:
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
with:
persist-credentials: false
- name: Python environment
run: scripts/ci/python-env.sh
- name: pytest with coverage
run: /tmp/venv/bin/python -m pytest -W error::DeprecationWarning --cov=thor --cov-report=term-missing --cov-fail-under=90
integration:
name: Integration (MariaDB)
runs-on: ubuntu-latest
timeout-minutes: 20
container: node:22-trixie@sha256:f92a4668ece057eff6b83ed531b5f6277892b5c8fa8c1df4ea4ef470bf92b91e
env:
DB_NAME: thor
DB_USER: thor
DB_PASSWORD: ci-only # throwaway database inside the job container
THOR_TEST_DB_HOST: 127.0.0.1
THOR_TEST_DB_PASSWORD: ci-only
steps:
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
with:
persist-credentials: false
- name: Python environment
run: scripts/ci/python-env.sh
- name: Start MariaDB
run: scripts/ci/mariadb.sh
- name: Database tests
run: /tmp/venv/bin/python -m pytest -W error::DeprecationWarning tests/test_db_mariadb.py -v
security:
name: Security (bandit, pip-audit)
runs-on: ubuntu-latest
timeout-minutes: 15
container: node:22-trixie@sha256:f92a4668ece057eff6b83ed531b5f6277892b5c8fa8c1df4ea4ef470bf92b91e
steps:
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
with:
persist-credentials: false
- name: Python environment
run: scripts/ci/python-env.sh
- name: bandit
run: /tmp/venv/bin/bandit -q -r thor -ll
- name: pip-audit (runtime dependencies)
run: /tmp/venv/bin/pip-audit -r requirements.txt --progress-spinner off
scan:
name: Scan (secrets, workflows, shell, patterns)
runs-on: ubuntu-latest
timeout-minutes: 20
container: node:22-trixie@sha256:f92a4668ece057eff6b83ed531b5f6277892b5c8fa8c1df4ea4ef470bf92b91e
steps:
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
with:
persist-credentials: false
fetch-depth: 0 # gitleaks scans the whole history
- name: Install scanners (pinned versions, verified checksums)
run: |
cd /tmp
curl -fsSLo gitleaks.tgz https://github.com/gitleaks/gitleaks/releases/download/v8.30.1/gitleaks_8.30.1_linux_x64.tar.gz
echo "551f6fc83ea457d62a0d98237cbad105af8d557003051f41f3e7ca7b3f2470eb gitleaks.tgz" | sha256sum -c
curl -fsSLo actionlint.tgz https://github.com/rhysd/actionlint/releases/download/v1.7.12/actionlint_1.7.12_linux_amd64.tar.gz
echo "8aca8db96f1b94770f1b0d72b6dddcb1ebb8123cb3712530b08cc387b349a3d8 actionlint.tgz" | sha256sum -c
tar -xzf gitleaks.tgz -C /usr/local/bin gitleaks
tar -xzf actionlint.tgz -C /usr/local/bin actionlint
apt-get update -qq
DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends shellcheck python3-venv >/dev/null
- name: Secrets in code and history (gitleaks)
run: gitleaks git --no-banner --redact --exit-code 1 .
- name: Workflow files (actionlint, with shellcheck on run scripts)
run: actionlint .gitea/workflows/*.yml
- name: Shell scripts (shellcheck)
run: git ls-files -z '*.sh' | xargs -0 shellcheck
- name: Workflow security (zizmor)
run: |
python3 -m venv /tmp/zizmor && /tmp/zizmor/bin/pip install -q zizmor==1.32.0
/tmp/zizmor/bin/zizmor --offline --format plain .gitea/workflows/*.yml
- name: Code patterns (semgrep)
# Rule packs come from the semgrep registry. Fix new findings, or suppress one line
# with `nosemgrep: <rule id>` and a reason.
run: |
python3 -m venv /tmp/semgrep && /tmp/semgrep/bin/pip install -q semgrep==1.184.0
/tmp/semgrep/bin/semgrep scan --metrics=off --error \
--config p/python --config p/flask --config p/secrets --config p/security-audit \
--exclude tests
deploy:
name: Deploy
runs-on: ubuntu-latest
timeout-minutes: 15
needs: [lint, test, integration, security, scan]
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
concurrency:
group: deploy-production
cancel-in-progress: false
permissions:
contents: write # deploy tag
steps:
- name: Trigger webhook
env:
WEBHOOK_SECRET: ${{ secrets.WEBHOOK_SECRET }}
GIT_REF: ${{ github.ref }}
GIT_SHA: ${{ github.sha }}
run: |
PAYLOAD="{\"ref\":\"${GIT_REF}\",\"sha\":\"${GIT_SHA}\",\"ts\":$(date +%s)}"
SIG=$(echo -n "$PAYLOAD" | openssl dgst -sha256 -hmac "$WEBHOOK_SECRET" | awk '{print $2}')
curl -sf --connect-timeout 10 -X POST \
-H "Content-Type: application/json" \
-H "X-Gitea-Signature: ${SIG}" \
-d "$PAYLOAD" \
"http://10.10.10.86:9000/hooks/thor-deploy"
- name: Verify production runs this commit
env:
GIT_SHA: ${{ github.sha }}
run: |
# thor_deploy.sh checks out this sha, installs dependencies, restarts, and rolls
# back if /health never reports it with a working database.
for _ in $(seq 1 48); do
body=$(curl -sS --connect-timeout 5 --max-time 5 http://10.10.10.86/health || true)
if echo "$body" | grep -Eq "\"sha\": ?\"${GIT_SHA}\"" && echo "$body" | grep -Eq '"db": ?true'; then
echo "Production runs ${GIT_SHA}: ${body}"
exit 0
fi
sleep 5
done
echo "::error::Production never reported ${GIT_SHA} (last /health: ${body:-no response}). See /var/log/thor-deploy.log on CT 173."
exit 1
- name: Tag deployed commit
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
RUN_NUMBER: ${{ github.run_number }}
GIT_SHA: ${{ github.sha }}
REPO: ${{ github.repository }}
run: |
TAG="deploy-$(date -u +%Y.%m.%d)-${RUN_NUMBER}"
code=$(curl -s -o /tmp/tag.json -w '%{http_code}' -X POST \
-H "Authorization: token $GITHUB_TOKEN" -H "Content-Type: application/json" \
-d "{\"tag_name\":\"${TAG}\",\"target\":\"${GIT_SHA}\",\"message\":\"Deployed to production\"}" \
"https://code.lotusguild.org/api/v1/repos/${REPO}/tags")
if [ "$code" = 201 ] || [ "$code" = 409 ]; then echo "Tag ${TAG}: HTTP ${code}"; exit 0; fi
cat /tmp/tag.json; exit 1