ci-release-selfhosted.spec.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /** Release rehearsal routing and persistent-runner isolation, without executing release builds. */
  2. import { readFileSync } from 'node:fs'
  3. import { resolve } from 'node:path'
  4. import { runInNewContext } from 'node:vm'
  5. import { load } from 'js-yaml'
  6. import { describe, expect, it } from 'vitest'
  7. const root = resolve(import.meta.dirname, '../..')
  8. const repository = 'deepseek-harness/deepseek-harness'
  9. const selfhosted = ['self-hosted', 'linux', 'x64', 'vm-backup']
  10. const hosted = 'ubuntu-24.04'
  11. interface Step {
  12. name?: string
  13. uses?: string
  14. run?: string
  15. if?: string
  16. with?: Record<string, unknown>
  17. }
  18. interface Workflow {
  19. on: Record<string, unknown>
  20. permissions: Record<string, string>
  21. concurrency?: Record<string, unknown>
  22. jobs: Record<string, { name: string; 'runs-on': string; steps: Step[] }>
  23. }
  24. function workflow(file: string): Workflow {
  25. return load(readFileSync(resolve(root, '.github/workflows', file), 'utf8')) as Workflow
  26. }
  27. // This canonical-case corpus has matching Actions/JavaScript comparison results.
  28. // This is not an Actions interpreter: string case-folding and general coercion differ.
  29. // Missing context properties use the Actions empty-string value.
  30. function evaluate(expression: string, context: Record<string, string | boolean>): unknown {
  31. const source = expression.trim().replace(/^\$\{\{|\}\}$/g, '')
  32. .replace(/\b(?:github|vars|runner)(?:\.[a-zA-Z_][a-zA-Z_0-9]*)+/g,
  33. key => JSON.stringify(context[key] ?? ''))
  34. return runInNewContext(source, { fromJSON: JSON.parse }, { timeout: 1000 }) as unknown
  35. }
  36. const trustedPr = {
  37. 'vars.DSH_CI_FAILOVER_LINUX': 'selfhosted',
  38. 'github.repository': repository,
  39. 'github.actor': 'maintainer',
  40. 'github.event_name': 'pull_request',
  41. 'github.ref': 'refs/pull/42/merge',
  42. 'github.event.pull_request.head.repo.full_name': repository,
  43. 'github.event.pull_request.head.repo.fork': false,
  44. 'github.event.pull_request.user.login': 'contributor',
  45. }
  46. const trustedPush = {
  47. 'vars.DSH_CI_FAILOVER_LINUX': 'selfhosted',
  48. 'github.repository': repository,
  49. 'github.actor': 'maintainer',
  50. 'github.event_name': 'push',
  51. 'github.ref': 'refs/heads/master',
  52. }
  53. const fallbackCases: Array<[string, Record<string, string | boolean>]> = [
  54. ['unset switch', { ...trustedPr, 'vars.DSH_CI_FAILOVER_LINUX': '' }],
  55. ['hosted switch', { ...trustedPr, 'vars.DSH_CI_FAILOVER_LINUX': 'hosted' }],
  56. ['unknown switch', { ...trustedPr, 'vars.DSH_CI_FAILOVER_LINUX': 'true' }],
  57. ['fork PR', { ...trustedPr, 'github.event.pull_request.head.repo.full_name': 'outsider/fork', 'github.event.pull_request.head.repo.fork': true }],
  58. ['different head repository', { ...trustedPr, 'github.event.pull_request.head.repo.full_name': 'outsider/repo' }],
  59. ['fork flag', { ...trustedPr, 'github.event.pull_request.head.repo.fork': true }],
  60. ['Dependabot author rerun by maintainer', { ...trustedPr, 'github.event.pull_request.user.login': 'dependabot[bot]' }],
  61. ['Dependabot PR actor', { ...trustedPr, 'github.actor': 'dependabot[bot]' }],
  62. ['Dependabot push actor', { ...trustedPush, 'github.actor': 'dependabot[bot]' }],
  63. ['non-master push', { ...trustedPush, 'github.ref': 'refs/heads/topic' }],
  64. ['tag push', { ...trustedPush, 'github.ref': 'refs/tags/dsh-v1.0.0' }],
  65. ['push in another repository', { ...trustedPush, 'github.repository': 'outsider/fork' }],
  66. ['dispatch on master', { ...trustedPush, 'github.event_name': 'workflow_dispatch' }],
  67. ['dispatch on topic', { ...trustedPush, 'github.event_name': 'workflow_dispatch', 'github.ref': 'refs/heads/topic' }],
  68. ['dispatch on tag', { ...trustedPush, 'github.event_name': 'workflow_dispatch', 'github.ref': 'refs/tags/dsh-v1.0.0' }],
  69. ['pull_request_target', { ...trustedPr, 'github.event_name': 'pull_request_target' }],
  70. ['missing PR payload', { ...trustedPush, 'github.event_name': 'pull_request' }],
  71. ]
  72. for (const [file, jobIds] of [['release.yml', ['dependencies', 'pack']], ['release-vendor.yml', ['pack']]] as const) {
  73. describe(file, () => {
  74. const release = workflow(file)
  75. it('preserves the logical jobs, rehearsal events and read-only permission', () => {
  76. expect(Object.keys(release.jobs)).toEqual(jobIds)
  77. expect(release.on).toEqual({ pull_request: null, push: { branches: ['master'] }, workflow_dispatch: null })
  78. expect(release.permissions).toEqual({ contents: 'read' })
  79. expect(release.concurrency).toEqual({ group: '${{ github.workflow }}-${{ github.ref }}', 'cancel-in-progress': false })
  80. })
  81. for (const jobId of jobIds) {
  82. describe(jobId, () => {
  83. const job = release.jobs[jobId]!
  84. it('routes trusted PRs and master pushes onto the existing Linux pool', () => {
  85. expect(evaluate(job['runs-on'], trustedPr)).toEqual(selfhosted)
  86. expect(evaluate(job['runs-on'], trustedPush)).toEqual(selfhosted)
  87. expect(evaluate(job['runs-on'], { ...trustedPush, 'vars.DSH_CI_FAILOVER_LINUX': '' })).toBe(hosted)
  88. })
  89. it.each(fallbackCases)('keeps %s hosted', (_name, context) => {
  90. expect(evaluate(job['runs-on'], context)).toBe(hosted)
  91. })
  92. it('cleans stale checkout output and isolates setup before any pnpm invocation', () => {
  93. expect(job.steps[0]).toMatchObject({ uses: 'actions/checkout@v6', with: { clean: true, 'persist-credentials': false } })
  94. const cacheIndex = job.steps.findIndex(step => step.run?.includes('NODE_COMPILE_CACHE='))
  95. const pnpmIndex = job.steps.findIndex(step => step.uses?.startsWith('pnpm/') || /\bpnpm\b/.test(step.run ?? ''))
  96. expect(cacheIndex).toBeGreaterThan(0)
  97. expect(cacheIndex).toBeLessThan(pnpmIndex)
  98. expect(job.steps[cacheIndex]?.run).toContain('echo "NODE_COMPILE_CACHE=${{ runner.temp }}/node-compile-cache" >> "$GITHUB_ENV"')
  99. expect(job.steps[cacheIndex]?.run).toContain('echo "npm_config_devdir=${{ runner.temp }}/node-gyp" >> "$GITHUB_ENV"')
  100. expect(job.steps[cacheIndex]?.run).toContain('echo "TMPDIR=${{ runner.temp }}" >> "$GITHUB_ENV"')
  101. expect(job.steps.find(step => step.uses === 'pnpm/action-setup@v4')?.with?.dest)
  102. .toBe('${{ runner.temp }}/setup-pnpm-${{ github.run_id }}-${{ github.run_attempt }}-${{ github.job }}')
  103. expect(job.steps.find(step => step.name === 'Install (immutable)')?.run).toBe('pnpm install --frozen-lockfile')
  104. })
  105. it('uses the persistent store without remote cache reads or writes on self-hosted', () => {
  106. expect(job.steps.find(step => step.name === 'Configure pnpm store path')?.run).toContain('store_root="$HOME/.local/share/pnpm/store"')
  107. const caches = job.steps.filter(step => step.uses?.startsWith('actions/cache'))
  108. expect(caches.map(step => step.uses)).toEqual(['actions/cache/restore@v4'])
  109. for (const step of caches) {
  110. expect(evaluate(step.if!, { 'runner.environment': 'self-hosted' })).toBe(false)
  111. expect(evaluate(step.if!, { 'runner.environment': 'github-hosted' })).toBe(true)
  112. }
  113. const nodeSetup = job.steps.find(step => step.uses === 'actions/setup-node@v6')
  114. expect(nodeSetup?.with?.cache).toBeUndefined()
  115. expect(nodeSetup?.with?.['package-manager-cache']).toBe(false)
  116. })
  117. it('retains the dependency and pack verification commands', () => {
  118. const commands = job.steps.flatMap(step => step.run === undefined ? [] : [step.run])
  119. if (jobId === 'dependencies') {
  120. expect(commands).toContain('pnpm run verify-package-dependencies')
  121. expect(commands).toContain('pnpm run verify-npm-install-layout')
  122. } else {
  123. const family = file === 'release.yml' ? 'dsh' : 'vendor'
  124. const output = family === 'dsh' ? 'dist/npm' : 'dist/npm-vendor'
  125. expect(job.steps[0]?.with?.['fetch-depth']).toBe(0)
  126. expect(commands).toContain('pnpm run release:verify --family ' + family)
  127. expect(commands).toContain('pnpm run ' + (family === 'dsh' ? 'build:official' : 'build:lib:host'))
  128. expect(commands).toContain('pnpm run release:pack --family ' + family + ' --out ' + output + ' --concurrency 8')
  129. expect(commands).toContain('pnpm run release:verify-packed-install --family ' + family + ' --from ' + output
  130. + (family === 'dsh' ? ' --from dist/npm-vendor --from dist/npm-landlock' : ''))
  131. expect(job.steps.at(-1)).toMatchObject({ uses: 'actions/upload-artifact@v4', with: { path: output + '/*', 'retention-days': 7 } })
  132. }
  133. expect(JSON.stringify(job)).not.toMatch(/secrets\.|release:publish|npm-publish/)
  134. })
  135. })
  136. }
  137. })
  138. }
  139. it.each(['release-publish.yml', 'release-vendor-publish.yml'])('keeps %s manual and entirely hosted', (file) => {
  140. const publish = workflow(file)
  141. expect(publish.on).toEqual({ workflow_dispatch: null })
  142. for (const job of Object.values(publish.jobs)) expect(job['runs-on']).toBe(hosted)
  143. })