CI / Lint (JS) (pull_request) Successful in 10s
CI / Lint (Python) (pull_request) Failing after 36s
CI / Test (pull_request) Successful in 56s
CI / Security (pull_request) Successful in 32s
CI / Scan (pull_request) Successful in 28s
CI / Deploy (pull_request) Skipped
CI / Notify (pull_request) Skipped
The runner (act) runs JavaScript actions such as actions/checkout inside the job container, and python:3.13-trixie has no node, so checkout failed with 'exec: node: executable file not found'. Debian's nodejs (20.x) is enough for checkout v4 and keeps the job image matching production. Co-Authored-By: Claude Opus 5.5 <[email protected]>
256 lines
12 KiB
YAML
256 lines
12 KiB
YAML
name: CI
|
|
|
|
# One pipeline (gandalf#55): every check must pass before main deploys, the deploy names the
|
|
# exact commit CI tested, and CI waits until production reports that commit as healthy.
|
|
# Actions are pinned to full commit SHAs and job images to digests (version in the comment).
|
|
|
|
# Pushes build only main (which deploys); branches are built through their pull request,
|
|
# so nothing runs twice. Monday's run re-checks main against new advisories and rules.
|
|
on:
|
|
push:
|
|
branches: [main]
|
|
pull_request:
|
|
branches: ["**"]
|
|
schedule:
|
|
- cron: '0 6 * * 1'
|
|
|
|
permissions:
|
|
contents: read
|
|
|
|
concurrency:
|
|
group: ci-${{ github.ref }}
|
|
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}
|
|
|
|
# Python comes from the job image, not actions/setup-python: the runner shares one tool
|
|
# cache between concurrent jobs, and parallel setup actions overwrote each other in Pulse.
|
|
# Python 3.13 on Debian 13 (trixie) matches production (CT 157).
|
|
|
|
jobs:
|
|
lint-python:
|
|
name: Lint (Python)
|
|
runs-on: ubuntu-latest
|
|
timeout-minutes: 15
|
|
container: python:3.13-trixie@sha256:f25cbd3f7c73c28bdcab476c93c93a2ec90970a8f5b8a7810533654207822390
|
|
steps:
|
|
- name: Node for the checkout action
|
|
# The runner (act) runs JS actions inside the job container, and the Python image has no Node
|
|
run: apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends nodejs >/dev/null
|
|
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
|
|
with:
|
|
persist-credentials: false
|
|
- name: Install pinned tools
|
|
run: python3 -m pip install -q --root-user-action=ignore -r requirements-dev.txt
|
|
- name: flake8
|
|
run: flake8 .
|
|
- name: No em or en dashes (house style)
|
|
run: |
|
|
git config --global --add safe.directory "$GITHUB_WORKSPACE"
|
|
# Includes the vendored design system: it has been dash-free upstream since web_template#3.
|
|
if git grep -InP '[\x{2013}\x{2014}]|&[mn]dash;|\\u201[34]|̵[12];|ȁ[34];' \
|
|
-- . ':!package-lock.json'; then
|
|
echo "::error::Em or en dashes found (listed above). Use a hyphen, colon, comma or 'to'."
|
|
exit 1
|
|
fi
|
|
|
|
lint-js:
|
|
name: Lint (JS)
|
|
runs-on: ubuntu-latest
|
|
timeout-minutes: 15
|
|
container: node:22-bookworm@sha256:e13726ca604184d7a91fcacab1fc09eefd642dc28e79ac34e85540dd93b78cfb
|
|
steps:
|
|
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
|
|
with:
|
|
persist-credentials: false
|
|
- name: Install eslint (lockfile only)
|
|
run: npm ci --no-audit --no-fund
|
|
- name: eslint
|
|
run: npx eslint --ext .js static/
|
|
|
|
test:
|
|
name: Test
|
|
runs-on: ubuntu-latest
|
|
timeout-minutes: 20
|
|
container: python:3.13-trixie@sha256:f25cbd3f7c73c28bdcab476c93c93a2ec90970a8f5b8a7810533654207822390
|
|
steps:
|
|
- name: Node for the checkout action
|
|
# The runner (act) runs JS actions inside the job container, and the Python image has no Node
|
|
run: apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends nodejs >/dev/null
|
|
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
|
|
with:
|
|
persist-credentials: false
|
|
- name: Install dependencies and pinned tools
|
|
run: python3 -m pip install -q --root-user-action=ignore -r requirements.txt -r requirements-dev.txt
|
|
- name: pytest with coverage
|
|
run: python3 -m pytest tests/ -q --cov=. --cov-report=term-missing --cov-config=.coveragerc
|
|
|
|
security:
|
|
name: Security
|
|
runs-on: ubuntu-latest
|
|
timeout-minutes: 15
|
|
container: python:3.13-trixie@sha256:f25cbd3f7c73c28bdcab476c93c93a2ec90970a8f5b8a7810533654207822390
|
|
steps:
|
|
- name: Node for the checkout action
|
|
# The runner (act) runs JS actions inside the job container, and the Python image has no Node
|
|
run: apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends nodejs >/dev/null
|
|
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
|
|
with:
|
|
persist-credentials: false
|
|
- name: Install pinned tools
|
|
run: python3 -m pip install -q --root-user-action=ignore -r requirements-dev.txt
|
|
- name: bandit
|
|
run: bandit -q -r . --exclude ./.git,./node_modules -ll
|
|
|
|
scan:
|
|
name: Scan
|
|
runs-on: ubuntu-latest
|
|
timeout-minutes: 15
|
|
container: python:3.13-trixie@sha256:f25cbd3f7c73c28bdcab476c93c93a2ec90970a8f5b8a7810533654207822390
|
|
steps:
|
|
- name: Node for the checkout action
|
|
# The runner (act) runs JS actions inside the job container, and the Python image has no Node
|
|
run: apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends nodejs >/dev/null
|
|
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
|
|
with:
|
|
persist-credentials: false
|
|
fetch-depth: 0 # gitleaks scans the whole history, not just this commit
|
|
- 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
|
|
DEBIAN_FRONTEND=noninteractive apt-get install -y -qq --no-install-recommends shellcheck >/dev/null
|
|
- name: Secrets in code and history (gitleaks)
|
|
run: |
|
|
git config --global --add safe.directory "$GITHUB_WORKSPACE"
|
|
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 -r shellcheck
|
|
|
|
deploy:
|
|
name: Deploy
|
|
runs-on: ubuntu-latest
|
|
timeout-minutes: 15
|
|
needs: [lint-python, lint-js, test, security, scan]
|
|
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
|
|
container: python:3.13-trixie@sha256:f25cbd3f7c73c28bdcab476c93c93a2ec90970a8f5b8a7810533654207822390
|
|
# Never run two deploys at once; queue instead of cancelling one mid-way.
|
|
concurrency:
|
|
group: deploy-production
|
|
cancel-in-progress: false
|
|
permissions:
|
|
contents: write # deploy tag
|
|
steps:
|
|
- name: Trigger the deploy webhook
|
|
env:
|
|
WEBHOOK_SECRET: ${{ secrets.WEBHOOK_SECRET }}
|
|
GIT_REF: ${{ github.ref }}
|
|
GIT_SHA: ${{ github.sha }}
|
|
run: |
|
|
# The sha and a timestamp make every signature unique. The server checks them once the
|
|
# pinned deploy script is installed (gandalf#55 part 2); until then it deploys origin/main.
|
|
python3 - <<'EOF'
|
|
import hashlib, hmac, json, os, time, urllib.request
|
|
body = json.dumps({'ref': os.environ['GIT_REF'], 'sha': os.environ['GIT_SHA'],
|
|
'ts': int(time.time())}, separators=(',', ':')).encode()
|
|
sig = hmac.new(os.environ['WEBHOOK_SECRET'].encode(), body, hashlib.sha256).hexdigest()
|
|
req = urllib.request.Request('http://10.10.10.61:9000/hooks/gandalf-deploy', data=body, method='POST',
|
|
headers={'Content-Type': 'application/json', 'X-Gitea-Signature': sig})
|
|
with urllib.request.urlopen(req, timeout=15) as resp:
|
|
print('Webhook:', resp.status, resp.read().decode()[:200])
|
|
EOF
|
|
|
|
- name: Verify production runs this commit
|
|
env:
|
|
GIT_SHA: ${{ github.sha }}
|
|
run: |
|
|
# The webhook returns at once; the deploy fetches, installs and restarts both services.
|
|
# Succeed only when /health reports this commit for the web app and the monitor, with a
|
|
# working database and a fresh monitor (up to 5 minutes).
|
|
python3 - <<'EOF'
|
|
import json, os, sys, time, urllib.request
|
|
want, last = os.environ['GIT_SHA'], 'no response'
|
|
for _ in range(60):
|
|
try:
|
|
with urllib.request.urlopen('http://10.10.10.61/health', timeout=5) as resp:
|
|
last = resp.read().decode()
|
|
except Exception as e: # 503 while the monitor catches up, or the restart itself
|
|
last = getattr(e, 'read', lambda: str(e).encode())().decode()[:300]
|
|
try:
|
|
body = json.loads(last)
|
|
except ValueError:
|
|
body = {}
|
|
if body.get('status') == 'ok' and body.get('sha') == want and body.get('monitor_sha') == want:
|
|
print(f'Production is healthy on {want}: {last}')
|
|
sys.exit(0)
|
|
time.sleep(5)
|
|
print(f'::error::Production never reported {want} as healthy (last /health: {last}). '
|
|
'See /var/log/gandalf-deploy.log on CT 157.')
|
|
sys.exit(1)
|
|
EOF
|
|
|
|
- name: Tag the deployed commit
|
|
env:
|
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
RUN_NUMBER: ${{ github.run_number }}
|
|
GIT_SHA: ${{ github.sha }}
|
|
REPO: ${{ github.repository }}
|
|
run: |
|
|
python3 - <<'EOF'
|
|
import json, os, sys, time, urllib.error, urllib.request
|
|
tag = time.strftime('deploy-%Y.%m.%d-', time.gmtime()) + os.environ['RUN_NUMBER']
|
|
api = f"https://code.lotusguild.org/api/v1/repos/{os.environ['REPO']}/tags"
|
|
auth = {'Authorization': f"token {os.environ['GITHUB_TOKEN']}", 'Content-Type': 'application/json'}
|
|
body = json.dumps({'tag_name': tag, 'target': os.environ['GIT_SHA'],
|
|
'message': 'Deployed to production'}).encode()
|
|
try:
|
|
urllib.request.urlopen(urllib.request.Request(api, data=body, headers=auth, method='POST'), timeout=15)
|
|
print(f"Tagged {os.environ['GIT_SHA']} as {tag}")
|
|
sys.exit(0)
|
|
except urllib.error.HTTPError as e:
|
|
code = e.code
|
|
# A rerun keeps its run number, so the tag can already exist: fine if it is this commit's
|
|
try:
|
|
with urllib.request.urlopen(urllib.request.Request(f'{api}/{tag}', headers=auth), timeout=15) as r:
|
|
existing = json.load(r).get('commit', {}).get('sha')
|
|
except Exception:
|
|
existing = None
|
|
if existing == os.environ['GIT_SHA']:
|
|
print(f'{tag} already marks this commit (a rerun)')
|
|
sys.exit(0)
|
|
print(f'::error::Could not tag {tag} (HTTP {code}; existing: {existing})')
|
|
sys.exit(1)
|
|
EOF
|
|
|
|
notify:
|
|
name: Notify
|
|
runs-on: ubuntu-latest
|
|
timeout-minutes: 5
|
|
needs: [lint-python, lint-js, test, security, scan, deploy]
|
|
# Any failure on main or in the weekly run, and every deploy outcome
|
|
if: >-
|
|
always() && (
|
|
(failure() && (github.ref == 'refs/heads/main' || github.event_name == 'schedule')) ||
|
|
needs.deploy.result == 'success'
|
|
)
|
|
steps:
|
|
- name: Send Matrix alert
|
|
env:
|
|
MATRIX_WEBHOOK_URL: ${{ secrets.MATRIX_WEBHOOK_URL }}
|
|
REPO: ${{ github.repository }}
|
|
BRANCH: ${{ github.ref_name }}
|
|
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
|
|
RESULTS: "lint-python=${{ needs.lint-python.result }} lint-js=${{ needs.lint-js.result }} test=${{ needs.test.result }} security=${{ needs.security.result }} scan=${{ needs.scan.result }} deploy=${{ needs.deploy.result }}"
|
|
DEPLOYED: ${{ needs.deploy.result == 'success' }}
|
|
run: |
|
|
if [ -z "$MATRIX_WEBHOOK_URL" ] || [ "$MATRIX_WEBHOOK_URL" = "CONFIGURE_ME" ]; then exit 0; fi
|
|
if [ "$DEPLOYED" = "true" ]; then MSG="DEPLOYED: ${REPO} @ ${BRANCH} (${GITHUB_SHA:0:7})"; else MSG="CI FAILED: ${REPO} @ ${BRANCH} [${RESULTS}]"; fi
|
|
curl -sf -X POST "$MATRIX_WEBHOOK_URL" \
|
|
-H "Content-Type: application/json" \
|
|
-d "{\"text\":\"${MSG}: ${RUN_URL}\"}"
|