diff --git a/.github/workflows/lambda-tests.yml b/.github/workflows/lambda-tests.yml new file mode 100644 index 0000000..bcc098b --- /dev/null +++ b/.github/workflows/lambda-tests.yml @@ -0,0 +1,70 @@ +name: Lambda Tests + +on: + push: + branches: [main, develop] + pull_request: + branches: [main, develop] + +jobs: + discover: + runs-on: ubuntu-latest + outputs: + lambdas: ${{ steps.find.outputs.lambdas }} + steps: + - uses: actions/checkout@v4 + - name: Find all lambdas + id: find + run: | + lambdas=$(ls -d apps/backend/lambdas/*/ | grep -Ev "(tools)" | jq -R -s -c 'split("\n") | map(select(. != ""))') + echo "lambdas=$lambdas" >> $GITHUB_OUTPUT + + test: + needs: discover + runs-on: ubuntu-latest + + services: + postgres: + image: postgres:16 + env: + POSTGRES_USER: branch_dev + POSTGRES_PASSWORD: password + POSTGRES_DB: branch_db + ports: + - 5432:5432 + options: >- + --health-cmd="pg_isready" + --health-interval=10s + --health-timeout=5s + --health-retries=5 + + strategy: + fail-fast: false + matrix: + lambda: ${{ fromJson(needs.discover.outputs.lambdas) }} + + steps: + - uses: actions/checkout@v4 + + - name: Setup Node.js + uses: actions/setup-node@v4 + with: + node-version: '20' + + - name: Setup database schema + run: psql postgres://branch_dev:password@localhost:5432/branch_db -f apps/backend/db/db_setup.sql + + - name: Install dependencies + working-directory: ${{ matrix.lambda }} + run: npm ci --legacy-peer-deps + + - name: Run tests + working-directory: ${{ matrix.lambda }} + run: | + LAMBDA_NAME=$(basename ${{ matrix.lambda }}) + npm run dev & + sleep 5 + curl --fail --retry 5 --retry-delay 2 http://localhost:3000/${LAMBDA_NAME}/health + npm test + env: + DATABASE_URL: postgres://branch_dev:password@localhost:5432/branch_db?options=-csearch_path%3Dbranch \ No newline at end of file diff --git a/infrastructure/github/main.tf b/infrastructure/github/main.tf index f53d4a7..cad9c21 100644 --- a/infrastructure/github/main.tf +++ b/infrastructure/github/main.tf @@ -37,7 +37,7 @@ resource "github_branch_protection" "main" { required_status_checks { strict = true - contexts = ["terraform-plan-summary"] + contexts = ["terraform-plan-summary", "lambda-tests"] } enforce_admins = false