Skip to content

Commit

Permalink
feat(ci): run ci depending on code change (#1697)
Browse files Browse the repository at this point in the history
* feat(ci): run ci depending on code change

* fix: use valid if statement with expression

* feat: add condition to typecheck and test
  • Loading branch information
dotoleeoak authored May 18, 2024
1 parent dc2761e commit bd89fd3
Showing 1 changed file with 79 additions and 14 deletions.
93 changes: 79 additions & 14 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,57 +7,108 @@ on:
types: [opened, edited, synchronize, reopened]

jobs:
build:
name: Build
build-backend:
name: Build Backend
runs-on: ubuntu-latest

strategy:
matrix:
target: [frontend, backend, backend-admin]
target: [client, admin]

steps:
- uses: actions/checkout@v4

- name: Check if source code has changed
uses: dorny/paths-filter@v3
id: filter
with:
filters: |
shared: &shared
- 'apps/backend/libs/**'
- 'apps/backend/prisma/**'
- 'apps/backend/*.json'
- 'pnpm-lock.yaml'
client:
- *shared
- 'apps/backend/apps/client/**'
admin:
- *shared
- 'apps/backend/apps/admin/**'
- uses: ./.github/actions/setup-pnpm
if: ${{ steps.filter.outputs[matrix.target] == 'true' }}

- name: Generate Prisma Client
if: ${{ matrix.target == 'backend' || matrix.target == 'backend-admin' }}
if: ${{ steps.filter.outputs[matrix.target] == 'true' }}
run: pnpm --filter="@codedang/backend" exec prisma generate

- name: Build
if: ${{ steps.filter.outputs[matrix.target] == 'true' }}
run: pnpm --filter="@codedang/backend" build ${{ matrix.target }}

build-frontend:
name: Build Frontend
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Check if source code has changed
uses: dorny/paths-filter@v3
id: filter
with:
filters: |
frontend:
- 'apps/frontend/**'
- 'pnpm-lock.yaml'
- uses: ./.github/actions/setup-pnpm
if: steps.filter.outputs.frontend == 'true'

- name: Setup Next.js build cache
if: ${{ matrix.target == 'frontend' }}
uses: actions/cache@v4
if: steps.filter.outputs.frontend == 'true'
with:
path: apps/frontend/.next/cache
key: ${{ runner.os }}-nextjs-${{ hashFiles('pnpm-lock.yaml') }}-${{ hashFiles('**/*.js', '**/*.jsx', '**/*.ts', '**/*.tsx') }}
restore-keys: |
${{ runner.os }}-nextjs-${{ hashFiles('pnpm-lock.yaml') }}-
- name: Load Next.js environment
if: ${{ matrix.target == 'frontend' }}
if: steps.filter.outputs.frontend == 'true'
run: |
echo "NEXT_PUBLIC_BASEURL=https://stage.codedang.com/api" >> apps/frontend/.env
echo "NEXT_PUBLIC_GQL_BASEURL=https://stage.codedang.com/graphql" >> apps/frontend/.env
echo "NEXT_URL=https://stage.codedang.com" >> apps/frontend/.env
- name: Build (backend admin)
if: ${{ matrix.target == 'backend-admin' }}
run: pnpm --filter="@codedang/backend" build admin
- name: Build
if: ${{ matrix.target != 'backend-admin' }}
run: pnpm --filter="apps/${{ matrix.target }}" build
if: steps.filter.outputs.frontend == 'true'
run: pnpm --filter="@codedang/frontend" build

typecheck:
name: Typecheck
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: Check if source code has changed
uses: dorny/paths-filter@v3
id: filter
with:
filters: |
backend:
- 'apps/backend/**'
- uses: ./.github/actions/setup-pnpm
if: steps.filter.outputs.backend == 'true'

- name: Generate Prisma Client
if: steps.filter.outputs.backend == 'true'
run: pnpm --filter="@codedang/backend" exec prisma generate

- name: Check types (backend) # For spec files
if: steps.filter.outputs.backend == 'true'
run: pnpm --filter="@codedang/backend" exec tsc --noEmit

# Typecheck is not performed for frontend intentionally.
Expand Down Expand Up @@ -98,8 +149,8 @@ jobs:
- name: Lint (Node.js)
run: pnpm lint

test:
name: Test
test-backend:
name: Test Backend
runs-on: ubuntu-latest

env:
Expand All @@ -118,9 +169,21 @@ jobs:

steps:
- uses: actions/checkout@v4

- name: Check if source code has changed
uses: dorny/paths-filter@v3
id: filter
with:
filters: |
backend:
- 'apps/backend/**'
- 'pnpm-lock.yaml'
- uses: ./.github/actions/setup-pnpm
if: steps.filter.outputs.backend == 'true'

- name: Check Prisma Migration
if: steps.filter.outputs.backend == 'true'
run: |
pnpm --filter="@codedang/backend" exec prisma migrate diff \
--from-migrations ./prisma/migrations \
Expand All @@ -131,7 +194,9 @@ jobs:
"Please run 'pnpm prisma migrate dev' locally and commit the changes." && exit 1)
- name: Migrate Prisma
if: steps.filter.outputs.backend == 'true'
run: pnpm --filter="@codedang/backend" exec prisma migrate reset --force

- name: Test
run: pnpm -r test
if: steps.filter.outputs.backend == 'true'
run: pnpm --filter="@codedang/backend" test

0 comments on commit bd89fd3

Please sign in to comment.