|
| 1 | +name: Internal Tests |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + push: |
| 6 | + branches: |
| 7 | + - master |
| 8 | + merge_group: |
| 9 | + workflow_dispatch: |
| 10 | + |
| 11 | +run-name: Internal Tests [ref=${{ github.ref }}] |
| 12 | + |
| 13 | +concurrency: |
| 14 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 15 | + cancel-in-progress: true |
| 16 | + |
| 17 | +permissions: |
| 18 | + contents: read |
| 19 | + pull-requests: read |
| 20 | + |
| 21 | +jobs: |
| 22 | + internal-tests: |
| 23 | + name: Internal Tests |
| 24 | + # Skip if not a PR or a push to master |
| 25 | + # Skip if this is an external contribution. GitHub secrets will be empty, so the step would fail anyway. |
| 26 | + if: ${{ (github.event_name == 'pull_request' || (github.event_name == 'push' && github.ref == 'refs/heads/master')) |
| 27 | + && (github.event_name != 'pull_request' || !github.event.pull_request.head.repo.fork) }} |
| 28 | + runs-on: ubuntu-latest |
| 29 | + env: |
| 30 | + TARGET_OWNER: clockworklabs |
| 31 | + TARGET_REPO: SpacetimeDBPrivate |
| 32 | + steps: |
| 33 | + # Skip the private dispatch entirely when only `docs/` is touched. The job |
| 34 | + # itself still completes successfully so required-status-check gating is |
| 35 | + # satisfied without spending private-runner time on a docs-only change. |
| 36 | + - name: Detect non-docs changes |
| 37 | + id: filter |
| 38 | + uses: dorny/paths-filter@v3 |
| 39 | + with: |
| 40 | + filters: | |
| 41 | + non_docs: |
| 42 | + - '!docs/**' |
| 43 | +
|
| 44 | + - id: dispatch |
| 45 | + name: Trigger tests |
| 46 | + if: steps.filter.outputs.non_docs == 'true' |
| 47 | + uses: actions/github-script@v7 |
| 48 | + with: |
| 49 | + github-token: ${{ secrets.SPACETIMEDB_PRIVATE_TOKEN }} |
| 50 | + script: | |
| 51 | + const workflowId = 'ci.yml'; |
| 52 | + const targetRef = 'master'; |
| 53 | + const targetOwner = process.env.TARGET_OWNER; |
| 54 | + const targetRepo = process.env.TARGET_REPO; |
| 55 | + // Use the ref for pull requests because the head sha is brittle (github does some extra dance where it merges in master). |
| 56 | + const publicRef = (context.eventName === 'pull_request') ? context.payload.pull_request.head.ref : context.sha; |
| 57 | + const publicPrNumber = context.payload.pull_request?.number; |
| 58 | + const preDispatch = new Date().toISOString(); |
| 59 | + const inputs = { public_ref: publicRef }; |
| 60 | + if (publicPrNumber) { |
| 61 | + inputs.public_pr_number = String(publicPrNumber); |
| 62 | + } |
| 63 | +
|
| 64 | + // Dispatch the workflow in the target repository |
| 65 | + await github.rest.actions.createWorkflowDispatch({ |
| 66 | + owner: targetOwner, |
| 67 | + repo: targetRepo, |
| 68 | + workflow_id: workflowId, |
| 69 | + ref: targetRef, |
| 70 | + inputs, |
| 71 | + }); |
| 72 | +
|
| 73 | + const sleep = (ms) => new Promise(r => setTimeout(r, ms)); |
| 74 | +
|
| 75 | + // Find the dispatched run by name |
| 76 | + let runId = null; |
| 77 | + for (let attempt = 0; attempt < 20 && !runId; attempt++) { // up to ~10 minutes to locate the run |
| 78 | + await sleep(5000); |
| 79 | + const runsResp = await github.rest.actions.listWorkflowRuns({ |
| 80 | + owner: targetOwner, |
| 81 | + repo: targetRepo, |
| 82 | + workflow_id: workflowId, |
| 83 | + event: 'workflow_dispatch', |
| 84 | + branch: targetRef, |
| 85 | + per_page: 50, |
| 86 | + }); |
| 87 | +
|
| 88 | + const expectedName = `CI [public_ref=${publicRef}]`; |
| 89 | + const candidates = runsResp.data.workflow_runs |
| 90 | + .filter(r => r.name === expectedName && new Date(r.created_at) >= new Date(preDispatch)) |
| 91 | + .sort((a, b) => new Date(b.created_at) - new Date(a.created_at)); |
| 92 | +
|
| 93 | + if (candidates.length > 0) { |
| 94 | + runId = candidates[0].id; |
| 95 | + break; |
| 96 | + } |
| 97 | + } |
| 98 | +
|
| 99 | + if (!runId) { |
| 100 | + core.setFailed('Failed to locate dispatched run in the private repository.'); |
| 101 | + return; |
| 102 | + } |
| 103 | +
|
| 104 | + const runUrl = `https://github.com/${targetOwner}/${targetRepo}/actions/runs/${runId}`; |
| 105 | + core.info(`View run: ${runUrl}`); |
| 106 | + core.setOutput('run_id', String(runId)); |
| 107 | + core.setOutput('run_url', runUrl); |
| 108 | +
|
| 109 | + - name: Wait for Internal Tests to complete |
| 110 | + if: steps.filter.outputs.non_docs == 'true' |
| 111 | + uses: actions/github-script@v7 |
| 112 | + with: |
| 113 | + github-token: ${{ secrets.SPACETIMEDB_PRIVATE_TOKEN }} |
| 114 | + script: | |
| 115 | + const targetOwner = process.env.TARGET_OWNER; |
| 116 | + const targetRepo = process.env.TARGET_REPO; |
| 117 | + const runId = Number(`${{ steps.dispatch.outputs.run_id }}`); |
| 118 | + const runUrl = `${{ steps.dispatch.outputs.run_url }}`; |
| 119 | + const sleep = (ms) => new Promise(r => setTimeout(r, ms)); |
| 120 | +
|
| 121 | + core.info(`Waiting for workflow result... ${runUrl}`); |
| 122 | +
|
| 123 | + let conclusion = null; |
| 124 | + for (let attempt = 0; attempt < 240; attempt++) { // up to ~2 hours |
| 125 | + const runResp = await github.rest.actions.getWorkflowRun({ |
| 126 | + owner: targetOwner, |
| 127 | + repo: targetRepo, |
| 128 | + run_id: runId |
| 129 | + }); |
| 130 | + const { status, conclusion: c } = runResp.data; |
| 131 | + if (status === 'completed') { |
| 132 | + conclusion = c || 'success'; |
| 133 | + break; |
| 134 | + } |
| 135 | + await sleep(30000); |
| 136 | + } |
| 137 | +
|
| 138 | + if (!conclusion) { |
| 139 | + core.setFailed('Timed out waiting for private workflow to complete.'); |
| 140 | + return; |
| 141 | + } |
| 142 | +
|
| 143 | + if (conclusion !== 'success') { |
| 144 | + core.setFailed(`Private workflow failed with conclusion: ${conclusion}`); |
| 145 | + } |
| 146 | +
|
| 147 | + - name: Cancel invoked run if workflow cancelled |
| 148 | + if: ${{ cancelled() && steps.dispatch.outputs.run_id }} |
| 149 | + uses: actions/github-script@v7 |
| 150 | + with: |
| 151 | + github-token: ${{ secrets.SPACETIMEDB_PRIVATE_TOKEN }} |
| 152 | + script: | |
| 153 | + const targetOwner = process.env.TARGET_OWNER; |
| 154 | + const targetRepo = process.env.TARGET_REPO; |
| 155 | + const runId = Number(`${{ steps.dispatch.outputs.run_id }}`); |
| 156 | + if (!runId) return; |
| 157 | + await github.rest.actions.cancelWorkflowRun({ |
| 158 | + owner: targetOwner, |
| 159 | + repo: targetRepo, |
| 160 | + run_id: runId, |
| 161 | + }); |
0 commit comments