diff --git a/.github/actions/build-local-sdk/action.yml b/.github/actions/build-local-sdk/action.yml index 03b17a4484bc..85e0076ccb0b 100644 --- a/.github/actions/build-local-sdk/action.yml +++ b/.github/actions/build-local-sdk/action.yml @@ -1,24 +1,33 @@ -name: 'Build SDK packages locally' -description: 'Restore workspace dependencies and build the SDK outputs needed by this job.' +name: Build SDK packages locally +description: Restore workspace dependencies and build the SDK outputs needed by this job. inputs: setup: - description: 'Set up the build Node version and restore workspace dependencies.' + description: Set up the build Node version and restore workspace dependencies. default: 'true' package-path: - description: 'Comma-separated package paths whose SDK dependencies to build; use . only for repo-wide checks.' + description: Comma-separated package paths whose SDK dependencies to build; use . only for repo-wide checks. default: '' targets: - description: 'Nx targets to build for the selected packages and their dependencies.' - default: 'build:transpile build:types' + description: Nx targets to build for the selected packages and their dependencies. + default: build:transpile build:types runs: using: composite steps: - name: Set up repository-pinned Node.js for building if: inputs.setup == 'true' uses: ./.github/actions/setup-node + - name: Select dependency cache + id: dependencies + if: inputs.setup == 'true' + env: + PACKAGE_PATH: ${{ inputs.package-path }} + run: node .github/actions/install-dependencies/groups.mjs select "$PACKAGE_PATH" >> "$GITHUB_OUTPUT" + shell: bash - name: Restore workspace dependencies if: inputs.setup == 'true' uses: ./.github/actions/install-dependencies + with: + group: ${{ steps.dependencies.outputs.group }} - name: Select SDK packages id: projects env: diff --git a/.github/actions/install-dependencies/action.yml b/.github/actions/install-dependencies/action.yml index 098f48c191a5..6cd60f2e0dbd 100644 --- a/.github/actions/install-dependencies/action.yml +++ b/.github/actions/install-dependencies/action.yml @@ -1,30 +1,45 @@ -name: 'Install pnpm dependencies' -description: 'Installs pnpm dependencies and caches them.' - +name: Install pnpm dependencies +description: Installs pnpm dependencies and caches them. outputs: cache_key: - description: 'The dependency cache key' + description: The dependency cache key value: ${{ steps.compute_lockfile_hash.outputs.hash }} - runs: - using: 'composite' + using: composite steps: - name: Install pnpm uses: ./.github/actions/install-pnpm - - name: Compute dependency cache key id: compute_lockfile_hash run: node ./scripts/dependency-hash-key.js >> "$GITHUB_OUTPUT" shell: bash - + - name: Compute selected dependency cache key + id: group + if: inputs.group != '' + env: + DEPENDENCY_GROUP: ${{ inputs.group }} + DEPENDENCY_HASH: ${{ steps.compute_lockfile_hash.outputs.hash }} + run: node "$GITHUB_ACTION_PATH/groups.mjs" key "$DEPENDENCY_GROUP" "$DEPENDENCY_HASH" >> "$GITHUB_OUTPUT" + shell: bash + - name: Restore selected workspace dependencies + id: selected + if: inputs.group != '' + uses: actions/cache/restore@v6.1.0 + with: + path: ${{ env.CACHED_DEPENDENCY_PATHS }} + key: ${{ steps.group.outputs.key }} - name: Check dependency cache uses: actions/cache@v6.1.0 id: cache_dependencies with: path: ${{ env.CACHED_DEPENDENCY_PATHS }} key: ${{ steps.compute_lockfile_hash.outputs.hash }}-${{ runner.arch }} - + if: inputs.group == '' || steps.selected.outputs.cache-hit != 'true' - name: Install dependencies - if: steps.cache_dependencies.outputs.cache-hit != 'true' + if: steps.selected.outputs.cache-hit != 'true' && steps.cache_dependencies.outputs.cache-hit != 'true' run: pnpm install --frozen-lockfile shell: bash +inputs: + group: + description: Optional dependency group; fall back to the full workspace cache on a miss. + default: '' diff --git a/.github/actions/install-dependencies/groups.mjs b/.github/actions/install-dependencies/groups.mjs new file mode 100644 index 000000000000..d5c49a066a24 --- /dev/null +++ b/.github/actions/install-dependencies/groups.mjs @@ -0,0 +1,54 @@ +import { createHash } from 'node:crypto'; +import fs from 'node:fs'; + +const workspaces = JSON.parse(fs.readFileSync('package.json', 'utf8')).workspaces; +const sdk = workspaces.filter(directory => directory.startsWith('packages/')); +const serverTests = [ + 'dev-packages/node-integration-tests', + 'dev-packages/bun-integration-tests', + 'dev-packages/deno-integration-tests', + 'dev-packages/bundler-plugin-integration-tests', +]; +const browserTests = ['dev-packages/browser-integration-tests', 'dev-packages/browser-min-version-tests']; +const groups = { + sdk: [...sdk, 'dev-packages/test-utils', 'dev-packages/bundler-tests'], + server: [...sdk, 'dev-packages/test-utils', ...serverTests], + browser: [...browserTests, 'packages/browser'], + cloudflare: ['dev-packages/cloudflare-integration-tests'], +}; + +const [command, value, dependencyHash] = process.argv.slice(2); +if (command === 'select') { + const paths = value.split(','); + let group = ''; + if (paths.every(directory => browserTests.includes(directory) || directory === 'packages/browser')) { + group = 'browser'; + } else if (paths.every(directory => serverTests.includes(directory))) { + group = 'server'; + } else if (paths.every(directory => groups.cloudflare.includes(directory))) { + group = 'cloudflare'; + } else if ( + paths.every( + directory => + directory.startsWith('packages/') || directory.startsWith('dev-packages/e2e-tests/test-applications/'), + ) + ) { + group = 'sdk'; + } + process.stdout.write(`group=${group}\n`); +} else { + const paths = groups[value]; + if (!paths) throw new Error(`Unknown dependency group: ${value}`); + if (command === 'roots') { + process.stdout.write(`paths=${paths.join(',')}\n`); + } else if (command === 'key') { + if (!dependencyHash) throw new Error('Missing dependency hash'); + // Changing a group's membership must invalidate its installation too. + const membership = createHash('sha256').update(JSON.stringify(paths)).digest('hex').slice(0, 12); + process.stdout.write( + `key=dependencies-selected-v1-${value}-${membership}-${dependencyHash}-${process.env.RUNNER_OS}-${process.env.RUNNER_ARCH}\n`, + ); + } else { + throw new Error(`Unknown command: ${command}`); + } +} diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 5e933ae046ec..89dedc9af799 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -413,6 +413,8 @@ jobs: uses: ./.github/actions/setup-node - name: Restore workspace dependencies uses: ./.github/actions/install-dependencies + with: + group: sdk - name: Build selected unit-test dependencies env: UNIT_TEST_ENV: browser @@ -509,6 +511,8 @@ jobs: uses: ./.github/actions/setup-node - name: Restore workspace dependencies uses: ./.github/actions/install-dependencies + with: + group: server - name: Build selected unit-test dependencies env: UNIT_TEST_ENV: node diff --git a/.github/workflows/seed-workspace-dependencies.yml b/.github/workflows/seed-workspace-dependencies.yml new file mode 100644 index 000000000000..7fcdae6cb67b --- /dev/null +++ b/.github/workflows/seed-workspace-dependencies.yml @@ -0,0 +1,81 @@ +name: Seed workspace dependency caches +on: + push: + branches: [develop] + workflow_dispatch: +permissions: + contents: read +env: + CACHED_DEPENDENCY_PATHS: | + ${{ github.workspace }}/node_modules + ${{ github.workspace }}/packages/*/node_modules + ${{ github.workspace }}/dev-packages/*/node_modules + ~/.cache/mongodb-binaries/ + SKIP_PLAYWRIGHT_BROWSER_INSTALL: '1' + pnpm_config_verify_deps_before_run: 'false' +jobs: + seed: + name: Seed ${{ matrix.group }} dependencies (${{ matrix.runner }}) + runs-on: ${{ matrix.runner }} + timeout-minutes: 15 + strategy: + fail-fast: false + matrix: + include: + - { group: sdk, runner: ubuntu-24.04 } + - { group: sdk, runner: ubuntu-24.04-arm } + - { group: server, runner: ubuntu-24.04 } + - { group: server, runner: ubuntu-24.04-arm } + - { group: browser, runner: ubuntu-24.04 } + - { group: cloudflare, runner: ubuntu-24.04-arm } + steps: + - uses: actions/checkout@v7 + - uses: ./.github/actions/setup-node + - uses: ./.github/actions/install-pnpm + - name: Compute cache key + id: key + env: + GROUP: ${{ matrix.group }} + run: | + hash=$(node scripts/dependency-hash-key.js) + node .github/actions/install-dependencies/groups.mjs key "$GROUP" "${hash#hash=}" >> "$GITHUB_OUTPUT" + - name: Restore selected dependencies + id: cache + uses: actions/cache/restore@v6.1.0 + with: + path: ${{ env.CACHED_DEPENDENCY_PATHS }} + key: ${{ steps.key.outputs.key }} + - name: Select workspace roots + if: steps.cache.outputs.cache-hit != 'true' + id: roots + env: + GROUP: ${{ matrix.group }} + run: node .github/actions/install-dependencies/groups.mjs roots "$GROUP" >> "$GITHUB_OUTPUT" + - name: Expand workspace dependencies + if: steps.cache.outputs.cache-hit != 'true' + id: projects + env: + INPUT_PACKAGE_PATH: ${{ steps.roots.outputs.paths }} + run: node .github/actions/build-local-sdk/select-projects.mjs >> "$GITHUB_OUTPUT" + - name: Install selected dependencies + if: steps.cache.outputs.cache-hit != 'true' + env: + PROJECTS: ${{ steps.projects.outputs.projects }} + run: | + filters=(--filter sentry-javascript) + IFS=',' read -ra projects <<< "$PROJECTS" + for project in "${projects[@]}"; do + filters+=(--filter "$project") + done + pnpm "${filters[@]}" install --frozen-lockfile + git diff --exit-code -- pnpm-lock.yaml pnpm-workspace.yaml package.json + - name: Measure installation + run: | + du -sh node_modules + find node_modules/.pnpm -mindepth 1 -maxdepth 1 -type d | wc -l + - name: Save selected dependencies + if: steps.cache.outputs.cache-hit != 'true' + uses: actions/cache/save@v6.1.0 + with: + path: ${{ env.CACHED_DEPENDENCY_PATHS }} + key: ${{ steps.key.outputs.key }}