Skip to content

Commit

Permalink
Fix up CI for existing app
Browse files Browse the repository at this point in the history
  • Loading branch information
cfranklin11 committed Jul 16, 2024
1 parent fb45244 commit 5a4e6ac
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 125 deletions.
49 changes: 17 additions & 32 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,30 @@ name: build
on: push

jobs:
lint:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: true
matrix:
service: [server, client]
env:
SERVICE_NAME: ${{ matrix.service }}
steps:
- uses: actions/checkout@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Build and export to Docker
uses: docker/build-push-action@v3
- uses: actions/checkout@v4
- uses: hoverkraft-tech/compose-action@v2.0.1
with:
context: ./${{ env.SERVICE_NAME }}
load: true
tags: ${{ env.SERVICE_NAME }}
- name: Check types
run: docker run --rm $SERVICE_NAME:latest yarn run typecheck
- name: Lint Typescript files
run: docker run --rm $SERVICE_NAME:latest yarn run lint
compose-file: "docker-compose.ci.yml"

- name: Check client types
run: docker-compose -f docker-compose.ci.yml exec -T client yarn run typecheck
- name: Lint client Typescript files
run: docker-compose -f docker-compose.ci.yml exec -T client yarn run lint

- name: Check server types
run: docker-compose -f docker-compose.ci.yml exec -T server yarn run typecheck
- name: Lint server Typescript files
run: docker-compose -f docker-compose.ci.yml exec -T server yarn run lint

test:
runs-on: ubuntu-latest
env:
DB_NAME: ${{ secrets.DB_NAME }}
steps:
- uses: actions/checkout@v2
- name: Build the docker image
run: docker-compose -f docker-compose.ci.yml build
- name: Run server unit tests
run: docker-compose -f docker-compose.ci.yml run server yarn run test:unit
run: docker-compose -f docker-compose.ci.yml exec -T server yarn run test:unit
- name: Run client unit tests
run: docker-compose -f docker-compose.ci.yml run client yarn run test --watchAll=false
run: docker-compose -f docker-compose.ci.yml exec -T client yarn run test --watchAll=false
- name: Run server integration tests
run: ./scripts/integration_tests.sh docker-compose.ci.yml
- name: Reset containers between test scripts
run: docker-compose -f docker-compose.ci.yml down
run: ./scripts/integration_tests.sh
- name: Run browser end to end tests for client app
id: e2e
run: ./scripts/browser_tests.sh
Expand Down
50 changes: 13 additions & 37 deletions docker-compose.ci.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
version: "3"
services:
db:
image: cockroachdb/cockroach:latest
ports:
# Port for connecting to the DB
- "26257:26257"
# Port for dashboard
- "8080:8080"
environment:
COCKROACH_DATABASE: test
command: start-single-node --insecure
server:
build: ./server
depends_on:
db:
condition: service_healthy
- db
ports:
- "3333:3333"
environment:
PORT: 3333
DATABASE_URL: postgresql://root@db:26257/${DB_NAME}?sslmode=disable
DATABASE_URL: postgresql://root@db:26257/test?sslmode=disable
NODE_ENV: test
HOSTNAME: localhost
ADMIN_EMAIL: test@test.com
Expand All @@ -20,29 +29,7 @@ services:
SPACES_REGION: spaces_region
SPACES_BUCKET: spaces_bucket
SPACES_ENDPOINT: spaces_endpoint
healthcheck:
test: ["CMD", "curl", "http://server:3333"]
interval: 2s
timeout: 10s
retries: 10
start_period: 30s
command: yarn run dev
db:
image: cockroachdb/cockroach:latest
ports:
# Port for connecting to the DB
- "26257:26257"
# Port for dashboard
- "8080:8080"
environment:
COCKROACH_DATABASE: ${DB_NAME}
healthcheck:
test: ["CMD", "curl", "http://db:8080/health?ready=1"]
interval: 2s
timeout: 10s
retries: 5
start_period: 10s
command: start-single-node --insecure
client:
build: ./client
ports:
Expand All @@ -52,22 +39,11 @@ services:
environment:
API_TOKEN: supersecretspicysauce
NODE_ENV: test
healthcheck:
test: ["CMD", "curl", "http://client:3000"]
interval: 2s
timeout: 10s
retries: 10
start_period: 30s
command: yarn run start
browser_test:
build: ./browser_test
depends_on:
client:
condition: service_healthy
server:
condition: service_healthy
db:
condition: service_healthy
- client
volumes:
- ./browser_test/cypress/screenshots:/app/cypress/screenshots
# Need to use 'host' network mode to avoid cross-host errors
Expand Down
37 changes: 9 additions & 28 deletions scripts/browser_tests.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
#!/bin/bash

set -euo pipefail
# set -euo pipefail

#### SETUP ####
DOCKER_COMPOSE_FILE="${1:-docker-compose.yml}"
DEFAULT_NODE_ENV=${NODE_ENV:-""}
export NODE_ENV=test
DEFAULT_DB_NAME=${DB_NAME:-""}
export DB_NAME=test_${DB_NAME}
DOCKER_COMPOSE_FILE="${1:-docker-compose.ci.yml}"
EXIT_CODE=0

trap log_errors err
Expand All @@ -16,43 +12,28 @@ trap clean_up exit
function log_errors() {
docker-compose -f ${DOCKER_COMPOSE_FILE} ps
docker-compose -f ${DOCKER_COMPOSE_FILE} logs
EXIT_CODE=1
}

function clean_up() {
docker-compose -f ${DOCKER_COMPOSE_FILE} exec -T db cockroach sql --execute "DROP DATABASE IF EXISTS ${DB_NAME};" --insecure

docker-compose -f ${DOCKER_COMPOSE_FILE} stop
export NODE_ENV=${DEFAULT_NODE_ENV}
export DB_NAME=${DEFAULT_DB_NAME}
docker-compose -f ${DOCKER_COMPOSE_FILE} up -d
docker-compose -f ${DOCKER_COMPOSE_FILE} exec -T db cockroach sql --execute "DROP DATABASE IF EXISTS test;" --insecure

exit ${EXIT_CODE}
}

docker-compose -f ${DOCKER_COMPOSE_FILE} stop
docker-compose -f ${DOCKER_COMPOSE_FILE} pull db
docker-compose -f ${DOCKER_COMPOSE_FILE} up --no-start
docker-compose -f ${DOCKER_COMPOSE_FILE} start db

# --retry-all-errors is available in a version of curl that isn't available
# in the latest version of Ubuntu yet, so we sleep till we can use it in CI.
sleep 10

# Need to create test DB separately because TypeORM won't do it for us
docker-compose -f ${DOCKER_COMPOSE_FILE} exec -T db cockroach sql --execute "CREATE DATABASE IF NOT EXISTS ${DB_NAME};" --insecure
docker-compose -f ${DOCKER_COMPOSE_FILE} exec -T db cockroach sql --execute "CREATE DATABASE IF NOT EXISTS test;" --insecure

docker-compose -f ${DOCKER_COMPOSE_FILE} up -d

docker-compose -f ${DOCKER_COMPOSE_FILE} run --rm server \
docker-compose -f ${DOCKER_COMPOSE_FILE} exec -T server \
yarn run migration:run -d src/dataSource.ts

#### SEED TEST DB ####
echo "Seeding database..."
docker-compose -f ${DOCKER_COMPOSE_FILE} run --rm \
docker-compose -f ${DOCKER_COMPOSE_FILE} exec -T \
server yarn run ts-node test/fixtures/seed_db.ts

#### RUN TESTS ####
docker-compose -f ${DOCKER_COMPOSE_FILE} run --rm \
browser_test npm run run:catalog || EXIT_CODE=1
browser_test npm run run:catalog
docker-compose -f ${DOCKER_COMPOSE_FILE} run --rm \
browser_test npm run run:admin || EXIT_CODE=1
browser_test npm run run:admin
35 changes: 7 additions & 28 deletions scripts/integration_tests.sh
Original file line number Diff line number Diff line change
@@ -1,46 +1,25 @@
#!/bin/bash

DOCKER_COMPOSE_FILE="${1:-docker-compose.yml}"
DEFAULT_NODE_ENV=${NODE_ENV:-""}
export NODE_ENV=test
DEFAULT_DB_NAME=${DB_NAME:-""}
export DB_NAME=test_${DB_NAME}

docker-compose -f ${DOCKER_COMPOSE_FILE} stop
docker-compose -f ${DOCKER_COMPOSE_FILE} pull db
echo "Creating Docker containers..."
docker-compose -f ${DOCKER_COMPOSE_FILE} up --no-start
echo "Starting DB..."
docker-compose -f ${DOCKER_COMPOSE_FILE} start db

# Tests have been flaky in CI, probably due to the DB not being ready
# even if the server is running. In other projects, a 4-second sleep
# safely give the DB time to get ready for input.
sleep 4
DOCKER_COMPOSE_FILE="${1:-docker-compose.ci.yml}"

# Need to create test DB separately because TypeORM won't do it for us
docker exec -t productcatalog_db_1 \
cockroach sql --execute "CREATE DATABASE ${DB_NAME};" --insecure
docker-compose -f ${DOCKER_COMPOSE_FILE} exec -T db \
cockroach sql --execute "CREATE DATABASE IF NOT EXISTS test;" --insecure

docker-compose -f ${DOCKER_COMPOSE_FILE} run --rm server \
docker-compose -f ${DOCKER_COMPOSE_FILE} exec -T server \
yarn run migration:run -d src/dataSource.ts

EXIT_CODE=$?

if [ ${EXIT_CODE} == 0 ]
then
docker-compose -f ${DOCKER_COMPOSE_FILE} run --rm server yarn test:integration
docker-compose -f ${DOCKER_COMPOSE_FILE} exec -T server yarn test:integration

EXIT_CODE=$?
fi

# TEST CLEANUP
docker exec -t productcatalog_db_1 \
cockroach sql --execute "DROP DATABASE IF EXISTS ${DB_NAME};" --insecure

docker-compose -f ${DOCKER_COMPOSE_FILE} stop
export NODE_ENV=${DEFAULT_NODE_ENV}
export DB_NAME=${DEFAULT_DB_NAME}
docker-compose -f ${DOCKER_COMPOSE_FILE} up -d
docker-compose -f ${DOCKER_COMPOSE_FILE} exec -T db \
cockroach sql --execute "DROP DATABASE IF EXISTS test;" --insecure

exit ${EXIT_CODE}

0 comments on commit 5a4e6ac

Please sign in to comment.