mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-05 21:42:59 -05:00
Merge pull request #10965 from dotCooCoo/fix/sonarcloud-newest-main
CI: analyze main in SonarCloud only at the nightly's commit
This commit is contained in:
4 files changed
+186
-20
No files matched your search
@@ -16,9 +16,11 @@ jobs:
|
||||
github.repository == 'storytold/vectorcraft' &&
|
||||
github.event_name == 'workflow_run'
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 5
|
||||
# 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
|
||||
@@ -27,6 +29,10 @@ jobs:
|
||||
run: |
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
|
||||
def require(value, message):
|
||||
@@ -51,22 +57,58 @@ jobs:
|
||||
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={
|
||||
'Authorization': 'Bearer ' + os.environ['GH_TOKEN'],
|
||||
'Accept': 'application/vnd.github+json',
|
||||
'Content-Type': 'application/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')
|
||||
data=body, method='POST', headers=dict(headers, **{'Content-Type': 'application/json'}))
|
||||
# One bounded request, no retry: duplicate transport is not publication authority.
|
||||
with urllib.request.build_opener(NoRedirect()).open(request, timeout=20) as response:
|
||||
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.')
|
||||
@@ -1,8 +1,12 @@
|
||||
name: SonarCloud
|
||||
|
||||
# Main is analyzed once a day, at the commit the nightly builds: Stage 2 admits a nightly only on
|
||||
# that commit's own analysis, and nightly-dispatch.yml waits for it before dispatching the signer.
|
||||
on:
|
||||
workflow_dispatch:
|
||||
push:
|
||||
workflow_run:
|
||||
workflows: [VectorCraft unsigned nightly]
|
||||
types: [requested]
|
||||
branches: [main]
|
||||
pull_request:
|
||||
types: [opened, synchronize, reopened]
|
||||
@@ -12,11 +16,35 @@ permissions:
|
||||
|
||||
concurrency:
|
||||
group: ${{ github.ref == 'refs/heads/main' && format('sonarcloud-main-{0}', github.sha) || format('sonarcloud-{0}', github.ref) }}
|
||||
# A push to main is the run that publishes coverage; superseded runs there would never finish.
|
||||
# A main run is the analysis a nightly waits for; superseded runs there would never finish.
|
||||
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}
|
||||
|
||||
jobs:
|
||||
nightly-commit:
|
||||
name: Nightly commit is main's head
|
||||
# A workflow_run checks out and analyzes main's head, so main must not have moved since the
|
||||
# nightly started.
|
||||
if: github.event_name == 'workflow_run'
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 2
|
||||
steps:
|
||||
- name: Harden runner
|
||||
uses: step-security/harden-runner@1d88384fd173af6ecb012dfd0264a306b711241a # v2.22.4
|
||||
with:
|
||||
egress-policy: audit
|
||||
- name: Require main's head to be the nightly's commit
|
||||
env:
|
||||
NIGHTLY_SHA: ${{ github.event.workflow_run.head_sha }}
|
||||
RUN_SHA: ${{ github.sha }}
|
||||
run: |
|
||||
if [ "$NIGHTLY_SHA" != "$RUN_SHA" ]; then
|
||||
echo "::error::main moved from $NIGHTLY_SHA to $RUN_SHA after the nightly started; this run would not analyze the nightly's commit"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
sonarcloud:
|
||||
needs: nightly-commit
|
||||
if: ${{ !cancelled() && contains(fromJSON('["success","skipped"]'), needs.nightly-commit.result) }}
|
||||
# Tracked at @main so org-wide CI improvements propagate without PR churn.
|
||||
# The reusable workflow defaults build-jobs to 2 so a cold instrumented
|
||||
# archive stays within hosted-runner memory (craft-common-actions#333).
|
||||
|
||||
Reference in new issue
Block a user