Files
vectorcraft/.github/workflows/nightly-dispatch.yml
T
dotCooCoo 1bc0c5ba78 CI: analyze main in SonarCloud only at the nightly's commit
SonarCloud ran a full instrumented analysis for every merge commit on main
(about 90 a day, 97 to 191 minutes each). Stage 2 only reads the analysis
of the commit the nightly builds, so main is now analyzed once a day:

- sonarcloud.yml drops the push trigger and starts when the unsigned
  nightly starts (workflow_run, requested). A first job fails the run when
  main has moved past the nightly's commit, so the analysis is never of
  another commit. Pull request analysis is unchanged.
- nightly-dispatch.yml waits, for up to 280 minutes, for the SonarCloud
  check on the producer's commit to complete before it dispatches the
  signer. A SonarCloud run that ended without a check stops the wait after
  20 minutes. The job gains checks: read and a 300-minute timeout.
- packaging/nightly-release-tests.py runs the dispatch script against a
  fake API: it dispatches only after the check completes, and refuses when
  the SonarCloud run ended without one or when the deadline passes.
- docs/nightly-release-preparation.md describes both workflows.
2026-12-05 08:06:12 -08:00

115 lines
6.2 KiB
YAML

name: Dispatch release-ref nightly
# Transport only, never authorization. No checkout, payload parsing or release environment.
on:
workflow_run:
workflows: [VectorCraft unsigned nightly]
types: [completed]
branches: [main]
permissions: {}
jobs:
dispatch:
# Enabled for automated scheduled producer completions on main.
if: >-
github.repository == 'storytold/vectorcraft' &&
github.event_name == 'workflow_run'
runs-on: ubuntu-latest
# Most of the time is the wait for the producer commit's SonarCloud analysis (up to 280 minutes).
timeout-minutes: 300
permissions:
actions: write
checks: read
steps:
- name: Dispatch only the exact completed producer run and attempt
shell: python
env:
GH_TOKEN: ${{ github.token }}
run: |
import json
import os
import re
import time
import urllib.error
import urllib.parse
import urllib.request
def require(value, message):
if not value:
raise RuntimeError(message)
# These filters reduce noise; the protected signer-branch validator repeats admission
# against authenticated API metadata and required attestation/quality evidence.
require(os.environ.get('GITHUB_REPOSITORY') == 'storytold/vectorcraft', 'wrong repository')
require(os.environ.get('GITHUB_EVENT_NAME') == 'workflow_run', 'wrong event')
with open(os.environ['GITHUB_EVENT_PATH'], encoding='utf-8') as stream:
event = json.load(stream)
require(event.get('action') == 'completed', 'producer is not complete')
require(event.get('repository', {}).get('full_name') == 'storytold/vectorcraft', 'wrong event repository')
producer = event.get('workflow_run', {})
require(producer.get('head_repository', {}).get('full_name') == 'storytold/vectorcraft', 'foreign producer')
require(producer.get('path') == '.github/workflows/nightly.yml', 'wrong producer path')
require(producer.get('head_branch') == 'main', 'wrong producer branch')
require(producer.get('status') == 'completed' and producer.get('conclusion') == 'success', 'producer did not succeed')
# First scheduled attempt only; reviewed manual/re-run authority belongs to release policy.
require(producer.get('event') == 'schedule', 'not an automatic scheduled producer')
run_id, attempt = producer.get('id'), producer.get('run_attempt')
require(type(run_id) is int and run_id > 0, 'invalid producer run id')
require(type(attempt) is int and attempt == 1, 'invalid automatic producer attempt')
source = producer.get('head_sha')
require(isinstance(source, str) and re.fullmatch(r'[0-9a-f]{40}', source), 'invalid producer commit')
headers = {
'Authorization': 'Bearer ' + os.environ['GH_TOKEN'],
'Accept': 'application/vnd.github+json',
'X-GitHub-Api-Version': '2022-11-28',
'User-Agent': 'vectorcraft-nightly-dispatch'
}
class NoRedirect(urllib.request.HTTPRedirectHandler):
def redirect_request(self, req, fp, code, msg, headers, newurl):
raise RuntimeError('dispatch redirects are forbidden')
opener = urllib.request.build_opener(NoRedirect())
def read(path):
request = urllib.request.Request('https://api.github.com/repos/storytold/vectorcraft' + path, headers=headers)
with opener.open(request, timeout=20) as response:
require(response.status == 200, 'read was not accepted')
return json.load(response)
# Stage 2 admits the producer only on its commit's own SonarCloud analysis, which
# sonarcloud.yml starts when the producer starts. Wait for the check that analysis posts;
# a SonarCloud run that ended without one gets 20 minutes before the wait gives up.
checks_path = '/commits/' + source + '/check-runs?' + urllib.parse.urlencode(
{'app_id': 12526, 'check_name': 'SonarCloud Code Analysis', 'filter': 'latest'})
runs_path = '/actions/workflows/sonarcloud.yml/runs?' + urllib.parse.urlencode(
{'event': 'workflow_run', 'head_sha': source, 'per_page': 1})
deadline = time.monotonic() + 280 * 60
ended = None
while True:
try:
checks = read(checks_path).get('check_runs', [])
if any(c.get('head_sha') == source and c.get('status') == 'completed' for c in checks):
break
runs = read(runs_path).get('workflow_runs', [])
if runs and runs[0].get('status') == 'completed' and ended is None:
ended = time.monotonic()
print('SonarCloud run', runs[0].get('id'), 'ended:', runs[0].get('conclusion'))
except (urllib.error.URLError, OSError, ValueError) as error:
print('Read failed, polling again:', error)
require(ended is None or time.monotonic() < ended + 20 * 60,
'the SonarCloud run for the producer commit ended without an analysis check')
require(time.monotonic() < deadline, 'no SonarCloud analysis of the producer commit in time')
time.sleep(60)
print('SonarCloud analysis check of', source, 'is complete.')
body = json.dumps({'ref': 'refs/heads/nightly-signer', 'inputs': {
'producer_run_id': str(run_id), 'producer_run_attempt': str(attempt)
}}).encode('utf-8')
request = urllib.request.Request(
'https://api.github.com/repos/storytold/vectorcraft/actions/workflows/nightly-sign.yml/dispatches',
data=body, method='POST', headers=dict(headers, **{'Content-Type': 'application/json'}))
# One bounded request, no retry: duplicate transport is not publication authority.
with opener.open(request, timeout=20) as response:
require(response.status == 204, 'dispatch was not accepted')
print('Dispatched producer run/attempt to the release-ref validator; not a signing result.')