|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Re-key nx GitHub Actions cache for cross-workflow restore. |
| 3 | +
|
| 4 | +SAVE key: move ${{ github.sha }} to the front (right after 'nx-v2-') so sha |
| 5 | +becomes a restore prefix; keep workflow/job/matrix + run_attempt so each job |
| 6 | +still writes a UNIQUE entry (no save collisions, nothing overwritten). |
| 7 | +
|
| 8 | +RESTORE keys: prefix every existing scoped key with the sha, and add a new |
| 9 | +'...-nx-v2-<sha>-' tier (any job/workflow at this sha) before the broad |
| 10 | +'...-nx-v2-' fallback. Result per step: |
| 11 | + ...-nx-v2-<sha>-<workflow>-<job>[-<matrix>]- own job, same sha (reruns) |
| 12 | + ...-nx-v2-<sha>- any job/workflow, same sha |
| 13 | + ...-nx-v2- any nx cache (cross-commit) |
| 14 | +Idempotent. |
| 15 | +""" |
| 16 | +import glob |
| 17 | + |
| 18 | +FILES = sorted(set(glob.glob('.github/workflows/*.yml'))) + \ |
| 19 | + ['.github/actions/run-qunit-tests/action.yml'] |
| 20 | + |
| 21 | +RUNNER = '${{ runner.os }}' |
| 22 | +SHA = '${{ github.sha }}' |
| 23 | +ATTEMPT = '${{ github.run_attempt }}' |
| 24 | +PREFIX = RUNNER + '-nx-v2-' |
| 25 | +KEY_TAIL = '-' + SHA + '-' + ATTEMPT |
| 26 | + |
| 27 | + |
| 28 | +def transform(path): |
| 29 | + with open(path) as f: |
| 30 | + lines = f.readlines() |
| 31 | + out = [] |
| 32 | + changed = False |
| 33 | + for line in lines: |
| 34 | + nl = '\n' if line.endswith('\n') else '' |
| 35 | + bare = line[:-1] if nl else line |
| 36 | + indent = bare[:len(bare) - len(bare.lstrip(' '))] |
| 37 | + stripped = bare.strip() |
| 38 | + |
| 39 | + # KEY line: key: ${{ runner.os }}-nx-v2-<MIDDLE>-${{ github.sha }}-${{ github.run_attempt }} |
| 40 | + if stripped.startswith('key:'): |
| 41 | + val = stripped[len('key:'):].strip() |
| 42 | + if val.startswith(PREFIX) and val.endswith(KEY_TAIL): |
| 43 | + middle = val[len(PREFIX):-len(KEY_TAIL)] |
| 44 | + out.append(indent + 'key: ' + PREFIX + SHA + '-' + middle + '-' + ATTEMPT + nl) |
| 45 | + changed = True |
| 46 | + continue |
| 47 | + out.append(line) |
| 48 | + continue |
| 49 | + |
| 50 | + # broad fallback restore-key: exactly ${{ runner.os }}-nx-v2- |
| 51 | + if stripped == PREFIX: |
| 52 | + same_sha = indent + PREFIX + SHA + '-' |
| 53 | + if not (out and out[-1].rstrip('\n') == same_sha): |
| 54 | + out.append(same_sha + nl) |
| 55 | + changed = True |
| 56 | + out.append(line) |
| 57 | + continue |
| 58 | + |
| 59 | + # scoped restore-key: ${{ runner.os }}-nx-v2-<non-empty>- |
| 60 | + if stripped.startswith(PREFIX) and stripped.endswith('-'): |
| 61 | + rest = stripped[len(PREFIX):] |
| 62 | + if rest.startswith(SHA + '-'): # already has sha (idempotent) |
| 63 | + out.append(line) |
| 64 | + continue |
| 65 | + out.append(indent + PREFIX + SHA + '-' + rest + nl) |
| 66 | + changed = True |
| 67 | + continue |
| 68 | + |
| 69 | + out.append(line) |
| 70 | + |
| 71 | + if changed: |
| 72 | + with open(path, 'w') as f: |
| 73 | + f.writelines(out) |
| 74 | + return changed |
| 75 | + |
| 76 | + |
| 77 | +for path in FILES: |
| 78 | + if transform(path): |
| 79 | + print('rekeyed:', path) |
0 commit comments