Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(CI): CI telemetry checking #7623

Merged
merged 10 commits into from
Feb 14, 2023
105 changes: 105 additions & 0 deletions .github/actions/telemetry_check/check.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/* eslint-env node */

import http from 'http'

import { exec } from '@actions/exec'

console.log(
`Telemetry is being redirected to ${process.env.REDWOOD_REDIRECT_TELEMETRY}`
)

// All the fields we expect inside a telemetry packet
const expectedPacketFields = [
'type',
'command',
'duration',
'uid',
'ci',
'redwoodCi',
'NODE_ENV',
'os',
'osVersion',
// "shell", // Not expected on windows
'nodeVersion',
'yarnVersion',
'npmVersion',
'redwoodVersion',
'system',
'complexity',
'sides',
'webBundler',
]

// Setup fake telemetry server
const server = http.createServer((req, res) => {
let data = ''
req.on('data', (chunk) => {
data += chunk
})
req.on('end', () => {
res.writeHead(200)
res.end()

const packet = JSON.parse(data)

let hasAllFields = true
for (const field of expectedPacketFields) {
if (packet[field] === undefined) {
hasAllFields = false
console.error(`Telemetry packet is missing field "${field}"`)
}
}

const isCI = packet.ci ?? false

if (hasAllFields && isCI) {
console.log('Valid telemetry received')
process.exit(0)
} else {
console.error('Invalid telemetry received')
console.error(packet)
process.exit(1)
}
})
})

// Run the fake telemetry server at the redirected location
const host = process.env.REDWOOD_REDIRECT_TELEMETRY.split(':')[1].slice(2)
const port = parseInt(process.env.REDWOOD_REDIRECT_TELEMETRY.split(':')[2])
server.listen(port, host, () => {
console.log(`Telemetry listener is running on http://${host}:${port}`)
})

// Run a command and await output
try {
const mode = process.argv[process.argv.indexOf('--mode') + 1]
let exitCode = 0
switch (mode) {
case 'crwa':
exitCode = await exec(
`yarn node ./packages/create-redwood-app/dist/create-redwood-app.js ../project-for-telemetry --typescript false --git false --yarn-install true`
)
if (exitCode) {
process.exit(1)
}
break
case 'cli':
exitCode = await exec(
`yarn --cwd ../project-for-telemetry node ../redwood/packages/cli/dist/index.js info`
)
if (exitCode) {
process.exit(1)
}
break
default:
console.error(`Unknown mode: ${mode}`)
process.exit(1)
}
} catch (error) {
console.error(error)
}

// If we didn't hear the telemetry after 2 mins then let's fail
await new Promise((r) => setTimeout(r, 120_000))
console.error('No telemetry response within 120 seconds. Failing...')
process.exit(1)
41 changes: 41 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,44 @@ jobs:
runs-on: ${{ matrix.os }}
steps:
- run: echo "Only doc changes"

telemetry-check:
needs: check
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
node-version: [16, 18]
fail-fast: true
name: 🔭 Telemetry check / ${{ matrix.os }} / node ${{ matrix.node-version }} latest
runs-on: ${{ matrix.os }}
env:
REDWOOD_REDIRECT_TELEMETRY: "http://127.0.0.1:48619" # Random port
steps:
- uses: actions/checkout@v3
- name: 🧶 Set up job
uses: ./.github/actions/set-up-job
with:
node-version: ${{ matrix.node-version }}

- name: 🔨 Build
run: yarn build

- name: 📢 Listen for telemetry (CRWA)
run: node ./.github/actions/telemetry_check/check.mjs --mode crwa
env:
YARN_ENABLE_IMMUTABLE_INSTALLS: false

- name: 📢 Listen for telemetry (CLI)
run: node ./.github/actions/telemetry_check/check.mjs --mode cli

telemetry-check-docs:
needs: only-doc-changes
if: needs.only-doc-changes.outputs.only-doc-changes == 'true'
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
node-version: [16, 18]
name: 🔭 Telemetry check / ${{ matrix.os }} / node ${{ matrix.node-version }} latest
runs-on: ${{ matrix.os }}
steps:
- run: echo "Only doc changes"
4 changes: 3 additions & 1 deletion packages/telemetry/src/sendTelemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,9 @@ const uniqueId = (rootDir: string | null) => {

// actually call the API with telemetry data
export const sendTelemetry = async () => {
const telemetryUrl = 'https://telemetry.redwoodjs.com/api/v1/telemetry'
const telemetryUrl =
process.env.REDWOOD_REDIRECT_TELEMETRY ||
'https://telemetry.redwoodjs.com/api/v1/telemetry'

try {
const payload = await buildPayload()
Expand Down