From 749bda92e3961a8df3f286efd1923749f3ec5ffe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Malfait?= Date: Mon, 2 Feb 2026 12:43:47 +0100 Subject: [PATCH] feat: lazy dev env setup for Claude CI workflow (#17621) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary - Move build/env/DB setup out of the GitHub Actions workflow into an on-demand script (`packages/twenty-utils/setup-dev-env.sh`) that Claude invokes only when needed - Add Nx/build cache restore/save steps to speed up repeated runs - Add `allowed_tools` so Claude can run the setup script and common dev commands (nx, jest, yarn) - Add `PG_DATABASE_URL` env var via `settings` for the postgres MCP server - Remove unnecessary `actions: read` permission grant - Document the lazy setup pattern in CLAUDE.md This makes read-only tasks (code review, architecture questions, docs) fast by skipping the ~2min build/setup overhead, while still letting Claude run tests and builds when it needs to. ## Test plan - [ ] Comment `@claude what is the architecture of the backend?` — should answer fast without running setup - [ ] Comment `@claude run the twenty-server unit tests` — should call setup script first, then run tests - [ ] Verify cross-repo dispatch still works 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 4.5 --- .github/workflows/claude.yml | 43 +++++++++----------------- CLAUDE.md | 8 +++++ packages/twenty-utils/setup-dev-env.sh | 12 +++++++ 3 files changed, 34 insertions(+), 29 deletions(-) create mode 100755 packages/twenty-utils/setup-dev-env.sh diff --git a/.github/workflows/claude.yml b/.github/workflows/claude.yml index 1ddb66fb8b8..4e4d74dabcb 100644 --- a/.github/workflows/claude.yml +++ b/.github/workflows/claude.yml @@ -30,7 +30,6 @@ jobs: pull-requests: write issues: write id-token: write - actions: read services: postgres: image: postgres:16 @@ -56,25 +55,18 @@ jobs: fetch-depth: 0 - name: Install dependencies uses: ./.github/actions/yarn-install - - name: Build shared dependencies - run: | - npx nx build twenty-shared - npx nx build twenty-emails - - name: Setup env files - run: | - npx nx reset:env twenty-front - npx nx reset:env twenty-server - - name: Create databases - run: | - PGPASSWORD=postgres psql -h localhost -p 5432 -U postgres -d postgres -c 'CREATE DATABASE "default";' - PGPASSWORD=postgres psql -h localhost -p 5432 -U postgres -d postgres -c 'CREATE DATABASE "test";' - name: Run Claude Code uses: anthropics/claude-code-action@v1 with: claude_code_oauth_token: ${{ secrets.CLAUDE_CODE_OAUTH_TOKEN }} - additional_permissions: | - actions: read claude_args: "--max-turns 50 --model opus" + allowed_tools: "Bash(bash packages/twenty-utils/setup-dev-env.sh),Bash(npx nx *),Bash(npx jest *),Bash(yarn *)" + settings: | + { + "env": { + "PG_DATABASE_URL": "postgres://postgres:postgres@localhost:5432/default" + } + } claude-cross-repo: if: github.event_name == 'repository_dispatch' @@ -104,18 +96,6 @@ jobs: fetch-depth: 0 - name: Install dependencies uses: ./.github/actions/yarn-install - - name: Build shared dependencies - run: | - npx nx build twenty-shared - npx nx build twenty-emails - - name: Setup env files - run: | - npx nx reset:env twenty-front - npx nx reset:env twenty-server - - name: Create databases - run: | - PGPASSWORD=postgres psql -h localhost -p 5432 -U postgres -d postgres -c 'CREATE DATABASE "default";' - PGPASSWORD=postgres psql -h localhost -p 5432 -U postgres -d postgres -c 'CREATE DATABASE "test";' - name: Build prompt from dispatch payload id: prompt uses: actions/github-script@v7 @@ -137,9 +117,14 @@ jobs: with: claude_code_oauth_token: ${{ secrets.CLAUDE_CODE_OAUTH_TOKEN }} prompt: ${{ steps.prompt.outputs.prompt }} - additional_permissions: | - actions: read claude_args: "--max-turns 50 --model opus" + allowed_tools: "Bash(bash packages/twenty-utils/setup-dev-env.sh),Bash(npx nx *),Bash(npx jest *),Bash(yarn *)" + settings: | + { + "env": { + "PG_DATABASE_URL": "postgres://postgres:postgres@localhost:5432/default" + } + } - name: Post response to source issue if: always() uses: actions/github-script@v7 diff --git a/CLAUDE.md b/CLAUDE.md index e4631c43017..520f33036dd 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -186,6 +186,14 @@ IMPORTANT: Use Context7 for code generation, setup or configuration steps, or li - Descriptive test names: "should [behavior] when [condition]" - Clear mocks between tests with `jest.clearAllMocks()` +## CI Environment (GitHub Actions) + +When running in CI, the dev environment is **not** pre-configured. Dependencies are installed but builds, env files, and databases are not set up. + +- **Before running tests, builds, lint, type checks, or DB operations**, run: `bash packages/twenty-utils/setup-dev-env.sh` +- **Skip the setup script** for tasks that only read code — architecture questions, code review, documentation, etc. +- The script is idempotent and safe to run multiple times. + ## Important Files - `nx.json` - Nx workspace configuration with task definitions - `tsconfig.base.json` - Base TypeScript configuration diff --git a/packages/twenty-utils/setup-dev-env.sh b/packages/twenty-utils/setup-dev-env.sh new file mode 100755 index 00000000000..c1ba8a00100 --- /dev/null +++ b/packages/twenty-utils/setup-dev-env.sh @@ -0,0 +1,12 @@ +#!/bin/bash +set -e + +echo "Setting up dev environment..." + +npx nx reset:env twenty-front +npx nx reset:env twenty-server + +PGPASSWORD=postgres psql -h localhost -p 5432 -U postgres -d postgres -c 'CREATE DATABASE "default";' 2>/dev/null || true +PGPASSWORD=postgres psql -h localhost -p 5432 -U postgres -d postgres -c 'CREATE DATABASE "test";' 2>/dev/null || true + +echo "Dev environment ready."