import { createWorkflow, NormalJob } from "github-actions-wac"; import { createJob } from "./jobs/index.js"; import { ACTION, AWS_REGION, DEFAULT_RUNNER, NODE_VERSION, OPENSEARCH_SERVICE, addToOutputs, createWaitForOpenSearchStep, runNodeScript } from "./utils/index.js"; import { VALKEY_SERVICE } from "./utils/index.js"; import { VALKEY_TEST_URL } from "./utils/index.js"; import { createGlobalBuildCacheSteps, createInstallBuildSteps, createRunBuildCacheSteps, createYarnCacheSteps, withCommonParams } from "./steps/index.js"; import { AbstractStorageOps, DdbOsStorageOps, DdbStorageOps, PgStorageOps, SqlStorageOps } from "./storageOps/index.js"; // Will print "next" or "dev". Important for caching (via actions/cache). const DIR_WEBINY_JS = "${{ github.base_ref }}"; const installBuildSteps = createInstallBuildSteps({ workingDirectory: DIR_WEBINY_JS }); const yarnCacheSteps = createYarnCacheSteps({ workingDirectory: DIR_WEBINY_JS }); const globalBuildCacheSteps = createGlobalBuildCacheSteps({ workingDirectory: DIR_WEBINY_JS }); const runBuildCacheSteps = createRunBuildCacheSteps({ workingDirectory: DIR_WEBINY_JS }); const ddbStorageOps = new DdbStorageOps(); const ddbOsStorageOps = new DdbOsStorageOps(); const sqlStorageOps = new SqlStorageOps(); const pgStorageOps = new PgStorageOps(); const createVitestTestsJobs = (storageOps?: AbstractStorageOps) => { const jobNames = { constants: ["vitest", storageOps?.shortId, "constants"].filter(Boolean).join("-"), tests: ["vitest", storageOps?.shortId, "run"].filter(Boolean).join("-") }; const constantsJob: NormalJob = createJob({ needs: ["constants", "build"], checkout: { path: DIR_WEBINY_JS }, name: `Vitest (${storageOps ? storageOps.displayName : "No storage"}) - Constants`, outputs: { "vitest-test-commands": "${{ steps.list-vitest-test-commands.outputs.vitest-test-commands }}" }, steps: [ { id: "list-vitest-test-commands", name: "List Vitest Test Commands", "working-directory": DIR_WEBINY_JS, run: runNodeScript( "listVitestTestCommands", `["${storageOps?.id || ""}", \${{ needs.constants.outputs.changed-packages }}]`, { outputAs: "vitest-test-commands" } ) }, { name: "Packages to test with Vitest", id: "list-packages", run: "echo '${{ steps.list-vitest-test-commands.outputs.vitest-test-commands }}'" } ] }); const env: Record = { AWS_REGION }; // The container needs no configuration at all - see `utils/openSearch.ts` for why there is no // endpoint, no credentials and no index prefix here. const needsOpenSearch = storageOps?.id === "ddb-os,ddb"; // The "No storage" job runs the Redis message bus tests; see `utils/valkey.ts`. const needsValkey = !storageOps; if (needsValkey) { env["WEBINY_TEST_REDIS_URL"] = VALKEY_TEST_URL; } // One object for every service container the job needs, so adding one never replaces another. const services: Record = {}; if (needsOpenSearch) { Object.assign(services, OPENSEARCH_SERVICE); } if (needsValkey) { Object.assign(services, VALKEY_SERVICE); } const hasServices = Object.keys(services).length > 0; if (storageOps) { env["WEBINY_STORAGE"] = storageOps.id; // An in-process PGlite: no Postgres service. if (storageOps instanceof PgStorageOps) { env["WEBINY_SQL_CLIENT"] = "pglite"; } } const runJob: NormalJob = createJob({ needs: ["constants", "build", jobNames.constants], name: "${{ matrix.testCommand.title }}", strategy: { "fail-fast": false, matrix: { os: [DEFAULT_RUNNER], node: [NODE_VERSION], testCommand: `$\{{ fromJSON(needs.${jobNames.constants}.outputs.vitest-test-commands) }}` } }, "runs-on": "${{ matrix.os }}", env, if: `needs.${jobNames.constants}.outputs.vitest-test-commands != '[]'`, awsAuth: !!storageOps, services: hasServices ? services : undefined, checkout: { path: DIR_WEBINY_JS }, steps: [ ...yarnCacheSteps, ...runBuildCacheSteps, ...installBuildSteps, ...(needsOpenSearch ? [createWaitForOpenSearchStep()] : []), { name: "Run tests", run: "${{ matrix.testCommand.cmd }}", "working-directory": DIR_WEBINY_JS } ] }); // We prevent running of Vitest tests if a PR was created from a fork. // This is because we don't want to expose our AWS credentials to forks. if (storageOps && storageOps.id === "ddb-os,ddb") { runJob.if += " && needs.constants.outputs.is-fork-pr != 'true'"; } return { [jobNames.constants]: constantsJob, [jobNames.tests]: runJob }; }; export const pullRequests = createWorkflow({ name: "Pull Requests", on: "pull_request", concurrency: { group: "pr-${{ github.event.pull_request.number }}", "cancel-in-progress": true }, jobs: { constants: createJob({ name: "Create constants", outputs: { "global-cache-key": "${{ steps.global-cache-key.outputs.global-cache-key }}", "run-cache-key": "${{ steps.run-cache-key.outputs.run-cache-key }}", "is-fork-pr": "${{ steps.is-fork-pr.outputs.is-fork-pr }}", "changed-packages": "${{ steps.detect-changed-packages.outputs.changed-packages }}", "latest-webiny-version": "${{ steps.latest-webiny-version.outputs.latest-webiny-version }}" }, steps: [ { name: "Create global cache key", id: "global-cache-key", run: addToOutputs( "global-cache-key", '${{ github.base_ref }}-${{ runner.os }}-$(/bin/date -u "+%m%d")-${{ vars.RANDOM_CACHE_KEY_SUFFIX }}' ) }, { name: "Create workflow run cache key", id: "run-cache-key", run: addToOutputs( "run-cache-key", "${{ github.run_id }}-${{ github.run_attempt }}-${{ vars.RANDOM_CACHE_KEY_SUFFIX }}" ) }, { name: "Is a PR from a fork", id: "is-fork-pr", run: addToOutputs( "is-fork-pr", "${{ github.event.pull_request.head.repo.fork }}" ) }, { name: "Detect changed files", id: "detect-changed-files", uses: ACTION.pathsFilter, with: { filters: "changed:\n - 'packages/**/*'\n", "list-files": "json" } }, { name: "Detect changed packages", id: "detect-changed-packages", run: runNodeScript( "listChangedPackages", "${{ steps.detect-changed-files.outputs.changed_files }}", { outputAs: "changed-packages" } ) }, { name: "Get latest Webiny version on NPM", id: "latest-webiny-version", run: addToOutputs("latest-webiny-version", "$(npm view @webiny/cli version)") } ] }), build: createJob({ name: "Build", needs: "constants", "runs-on": DEFAULT_RUNNER, checkout: { path: DIR_WEBINY_JS }, steps: [ ...yarnCacheSteps, ...globalBuildCacheSteps, ...installBuildSteps, // Once we've built packages with the help of the global cache, we can now cache // the result for this run. All of the following jobs will use this cache. ...runBuildCacheSteps ] }), buildProject: createJob({ needs: ["constants", "build"], name: "Build project (core, api, admin)", env: { WEBINY_INFRA_API_MAX_BUNDLE_SIZE: "${{ vars.WEBINY_INFRA_API_MAX_BUNDLE_SIZE }}" }, checkout: { path: DIR_WEBINY_JS }, steps: [ ...yarnCacheSteps, ...runBuildCacheSteps, ...installBuildSteps, { name: "Build core", run: "yarn webiny build core", "working-directory": DIR_WEBINY_JS }, { name: "API bundle size limit", run: 'echo "API bundle size limit: ${WEBINY_INFRA_API_MAX_BUNDLE_SIZE:-6291456} bytes"' }, { name: "Build api", run: "yarn webiny build api --no-deployment-checks", "working-directory": DIR_WEBINY_JS }, { name: "Build admin", run: "yarn webiny build admin --no-deployment-checks", "working-directory": DIR_WEBINY_JS } ] }), staticCodeAnalysis: createJob({ needs: ["constants"], name: "Static code analysis", checkout: { path: DIR_WEBINY_JS }, steps: [ ...yarnCacheSteps, ...runBuildCacheSteps, ...withCommonParams( [ { name: "Install dependencies", run: "yarn --immutable" }, { name: "Check code formatting", run: "yarn format:check" }, { name: "Check dependencies", run: "yarn adio" }, { name: "Check TS configs", run: "yarn check-ts-configs" }, { name: "Lint", run: "yarn lint" }, { name: "Check Package Node Modules", run: "yarn check:node-modules:ci" }, { name: "Validate webiny package", run: "yarn webiny-scripts validate-webiny-package" } ], { "working-directory": DIR_WEBINY_JS } ) ] }), // We couldn't add the `verify-dependencies` script to the `staticCodeAnalysis` job // because it requires the `build` job to run first. To not slow down the `staticCodeAnalysis` // and not to run the `build` job twice, we've created a separate job for this. staticCodeAnalysisVerifyDependencies: createJob({ needs: ["constants", "build"], name: "Static code analysis (verify dependencies)", checkout: { path: DIR_WEBINY_JS }, steps: [ ...yarnCacheSteps, ...runBuildCacheSteps, ...installBuildSteps, { name: "Sync Dependencies Verification", run: "yarn verify-dependencies", "working-directory": DIR_WEBINY_JS } ] }), staticCodeAnalysisTs: createJob({ name: "Static code analysis (TypeScript)", "runs-on": DEFAULT_RUNNER, checkout: { path: DIR_WEBINY_JS }, steps: [ ...yarnCacheSteps, // We're not using run cache here. We want to build all packages // with TypeScript, to ensure there are no TypeScript errors. // ...runBuildCacheSteps, ...withCommonParams( [ { name: "Install dependencies", run: "yarn --immutable" }, { name: "Check types for E2E tests", run: "yarn e2e:ts" } ], { "working-directory": DIR_WEBINY_JS } ) ] }), ...createVitestTestsJobs(), ...createVitestTestsJobs(ddbStorageOps), ...createVitestTestsJobs(ddbOsStorageOps), ...createVitestTestsJobs(sqlStorageOps), ...createVitestTestsJobs(pgStorageOps) } });