Skip to content

Commit

Permalink
Fix docker compose script and ci/cd (#46)
Browse files Browse the repository at this point in the history
* Fix docker compose script and ci/cd
  • Loading branch information
daniele-20tab authored Feb 12, 2023
1 parent 5016c20 commit e2f5863
Show file tree
Hide file tree
Showing 28 changed files with 185 additions and 165 deletions.
4 changes: 3 additions & 1 deletion {{cookiecutter.project_dirname}}/.env_template
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
COMPOSE_FILE=docker-compose/consumer.yaml:docker-compose/volumes.yaml:docker-compose/services.yaml:docker-compose/provider.yaml
COMPOSE_FILE=docker-compose.yaml:docker-compose/local.yaml
COMPOSE_PROFILES=pact
# CYPRESS_BASE_URL=https://proxy:8443
# INTERNAL_BACKEND_URL=http://provider:8000
# NEXT_PUBLIC_PROJECT_URL=https://localhost:8443
# REACT_ENVIRONMENT=development
Expand Down
24 changes: 6 additions & 18 deletions {{cookiecutter.project_dirname}}/.gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ variables:
cache:
paths:
- node_modules/
- .yarn
{% with env=cookiecutter.resources.envs[0] %}
.development:
rules: &development-rules
Expand Down Expand Up @@ -130,11 +129,12 @@ sentry_release_production:

test:
stage: Test
image: docker/compose:debian-1.29.2
image: docker:20
services:
- docker:20-dind
needs: []
variables:
COMPOSE_FILE: docker-compose.yaml:docker-compose/local.yaml
COMPOSE_PROJECT_NAME: "${CI_PROJECT_PATH_SLUG}-${CI_JOB_NAME}-${CI_JOB_ID}"
SERVICE_CONTAINER_NAME: "${CI_PROJECT_PATH_SLUG}-${CI_JOB_NAME}-${CI_JOB_ID}_frontend"
SERVICE_DOCKER_FILE: "docker/test.Dockerfile"
Expand Down Expand Up @@ -300,20 +300,15 @@ build_production:

.e2e:
stage: E2E
image: docker/compose:debian-1.29.2
image: docker:20
services:
- docker:20-dind
variables:
COMPOSE_PROJECT_NAME: "${CI_PROJECT_PATH_SLUG}-${CI_JOB_NAME}-${CI_JOB_ID}"
SERVICE_CONTAINER_NAME: "${CI_PROJECT_PATH_SLUG}-${CI_JOB_NAME}-${CI_JOB_ID}_e2e"
before_script:
- >
if [ "${PACT_ENABLED}" == "true" ];
then
export COMPOSE_FILE=docker-compose/consumer-image.yaml:docker-compose/services.yaml:docker-compose/provider.yaml;
else
export COMPOSE_FILE=docker-compose/consumer-image.yaml:docker-compose/services.yaml;
fi
- if [ "${PACT_ENABLED}" == "true" ]; then COMPOSE_PROFILES=pact; else COMPOSE_PROFILES=no-pact; fi
- export COMPOSE_PROFILES
- export CONSUMER_IMAGE=${CI_REGISTRY}/${CI_PROJECT_PATH}:${VERSION_REF}
- docker login -u gitlab-ci-token -p ${CI_BUILD_TOKEN} ${CI_REGISTRY}
script:
Expand Down Expand Up @@ -374,15 +369,8 @@ e2e_manual:
NEXT_PUBLIC_PROJECT_URL: 'https://proxy:8443'
REACT_ENVIRONMENT: 'production'
SERVICE_DOCKER_FILE: "docker/remote.Dockerfile"
COMPOSE_FILE: docker-compose.yaml:docker-compose/e2e.yaml:docker-compose/e2e-branch.yaml
needs: ["test"]
before_script:
- >
if [ "${PACT_ENABLED}" == "true" ];
then
export COMPOSE_FILE=docker-compose/consumer.yaml:docker-compose/services.yaml:docker-compose/provider.yaml;
else
export COMPOSE_FILE=docker-compose/consumer.yaml:docker-compose/services.yaml;
fi

.deploy:
stage: Deploy
Expand Down
13 changes: 3 additions & 10 deletions {{cookiecutter.project_dirname}}/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@ This service is generated from [20tab standard project](https://github.com/20tab
## The Kubernetes resource limits

The Kubernetes deployment service limits should be adapted to the expected load of the other services and to the size of the available nodes.
By default, the `s-1vcpu-1gb-amd` DigitalOcean droplet is used (https://slugs.do-api.dev/), which allocates 900.00m of CPU capacity and 1.54Gi of memory capacity.
The following default values are calculated assuming 2 deployments and 2 stacks on a single node.

| tfvars name | default value |
|--|--|
| service_limits_cpu | 225m |
| service_limits_memory | 256Mi |
| service_requests_cpu | 25m |
| service_requests_memory | 115Mi |

## Git

Expand Down Expand Up @@ -112,7 +103,9 @@ docker-compose up
:warning: **env variable** in custom mode you must be sure to have env, set in the system or in `.env` file.

```bash
COMPOSE_FILE=docker-compose.yaml:docker-compose/volumes.yaml:docker-compose/services.yaml:docker-compose/provider.yaml
COMPOSE_FILE=docker-compose.yaml:docker-compose/local.yaml
COMPOSE_PROFILES=pact
CYPRESS_BASE_URL=https://proxy:8443
INTERNAL_BACKEND_URL=http://provider:8000
NEXT_PUBLIC_PROJECT_URL=https://localhost:8443
REACT_ENVIRONMENT=development
Expand Down
46 changes: 46 additions & 0 deletions {{cookiecutter.project_dirname}}/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
version: "3.9"

services:

consumer:
environment:
- INTERNAL_BACKEND_URL=${INTERNAL_BACKEND_URL:-http://provider:8000}
- NEXT_PUBLIC_PROJECT_URL=${NEXT_PUBLIC_PROJECT_URL:-https://localhost:8443}
- REACT_ENVIRONMENT=${REACT_ENVIRONMENT:-development}
healthcheck:
test: wget -O- -q http://localhost:3000/api/health/ || exit 1

provider:
command: -p 8000 -d pacts -o --insecure-tls
entrypoint: ./pact_stub_server.sh
healthcheck:
test: wget -O- -q http://localhost:8000/api/health/ || exit 1
image: pactfoundation/pact-stub-server
profiles:
- pact
volumes:
- ./scripts/pact_stub_server.sh:/app/pact_stub_server.sh
- ./pacts:/app/pacts:ro
working_dir: /app

proxy:
depends_on:
consumer:
condition: service_healthy
provider:
condition: service_healthy
extends:
file: ./docker-compose/common.yaml
service: proxy
profiles:
- pact

proxy-no-pact:
depends_on:
consumer:
condition: service_healthy
extends:
file: ./docker-compose/common.yaml
service: proxy
profiles:
- no-pact
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
version: "3.4"
version: "3.9"

services:

proxy:
image: traefik:v2.6
depends_on:
consumer:
condition: service_healthy
volumes:
- ../proxy:/traefik:ro
command:
- "--configFile=/traefik/conf/static.yaml"
image: traefik:v2.6
networks:
default:
aliases:
- proxy
ports:
- "${LOCAL_HTTPS_PORT:-8443}:8443"
volumes:
- ../proxy:/traefik:ro

cypress:
build:
Expand All @@ -21,10 +22,12 @@ services:
args:
USER: ${USER:-appuser}
container_name: cypress
depends_on:
- proxy
environment:
- CYPRESS_BASE_URL=https://proxy:8443
- CYPRESS_BASE_URL=${CYPRESS_BASE_URL:-https://proxy:8443}
networks:
default:
aliases:
- cypress
user: ${USER:-appuser}
volumes:
- ../cypress:/app/cypress
Expand Down

This file was deleted.

14 changes: 0 additions & 14 deletions {{cookiecutter.project_dirname}}/docker-compose/consumer.yaml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: "3.9"

services:

consumer:
build:
context: ./
dockerfile: ${SERVICE_DOCKER_FILE:-docker/remote.Dockerfile}
24 changes: 24 additions & 0 deletions {{cookiecutter.project_dirname}}/docker-compose/e2e.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
version: "3.9"

services:

consumer:
image: ${CONSUMER_IMAGE}

cypress:
depends_on:
- proxy
extends:
file: ./docker-compose/common.yaml
service: cypress
profiles:
- pact

cypress-no-pact:
depends_on:
- proxy-no-pact
extends:
file: ./docker-compose/common.yaml
service: cypress
profiles:
- no-pact
10 changes: 10 additions & 0 deletions {{cookiecutter.project_dirname}}/docker-compose/local.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: "3.9"

services:

consumer:
build:
context: ./
dockerfile: ${SERVICE_DOCKER_FILE:-docker/local.Dockerfile}
volumes:
- ./:/app
19 changes: 0 additions & 19 deletions {{cookiecutter.project_dirname}}/docker-compose/provider.yaml

This file was deleted.

8 changes: 0 additions & 8 deletions {{cookiecutter.project_dirname}}/docker-compose/volumes.yaml

This file was deleted.

3 changes: 3 additions & 0 deletions {{cookiecutter.project_dirname}}/docker/e2e.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# syntax=docker/dockerfile:1

FROM cypress/base:16.18.1
ARG USER=appuser
ENV APPUSER=$USER PATH="$PATH:./node_modules/.bin"
Expand All @@ -10,3 +12,4 @@ RUN yarn add cypress typescript
RUN cypress install
RUN mkdir cypress-outputs
CMD [ "cypress", "run" ]
LABEL company="20tab" project="{{ cookiecutter.project_slug }}" service="frontend" stage="e2e"
3 changes: 1 addition & 2 deletions {{cookiecutter.project_dirname}}/docker/local.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# syntax=docker/dockerfile:1

FROM node:16-bullseye-slim
LABEL company="20tab" project="{{ cookiecutter.project_slug }}" service="frontend" stage="local"

ARG DEBIAN_FRONTEND=noninteractive
ENV LANG=C.UTF-8 LC_ALL=C.UTF-8 NEXT_TELEMETRY_DISABLED=1 NODE_ENV="development" WORKDIR=/app
RUN apt-get update \
Expand All @@ -24,3 +22,4 @@ WORKDIR ${WORKDIR}
RUN chown node ${WORKDIR}
ENTRYPOINT ["./scripts/entrypoint.sh"]
CMD yarn start
LABEL company="20tab" project="{{ cookiecutter.project_slug }}" service="frontend" stage="local"
11 changes: 2 additions & 9 deletions {{cookiecutter.project_dirname}}/docker/remote.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# syntax=docker/dockerfile:1

FROM node:16-alpine AS build
LABEL company="20tab" project="{{ cookiecutter.project_slug }}" service="frontend" stage="build"

ENV PATH="$PATH:./node_modules/.bin"
WORKDIR /app
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
Expand All @@ -21,8 +19,6 @@ COPY store ./store
COPY styles ./styles
COPY utils ./utils
COPY tsconfig.json next.config.js sentry.client.config.js sentry.server.config.js middleware.ts ./
# Environment variables must be present at build time
# https://github.com/vercel/next.js/discussions/14030
ARG SENTRY_AUTH_TOKEN \
SENTRY_ORG \
SENTRY_PROJECT_NAME \
Expand All @@ -35,21 +31,17 @@ ENV NEXT_TELEMETRY_DISABLED=1 \
SENTRY_PROJECT_NAME=$SENTRY_PROJECT_NAME \
SENTRY_URL=$SENTRY_URL
RUN yarn build
LABEL company="20tab" project="{{ cookiecutter.project_slug }}" service="frontend" stage="build"

FROM node:16-alpine AS remote
LABEL company="20tab" project="{{ cookiecutter.project_slug }}" service="frontend" stage="remote"

WORKDIR /app
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
USER nextjs
COPY ["next.config.js", "package.json", "sentry.client.config.js", "sentry.server.config.js", "server.js", "yarn.lock", "middleware.ts", "./"]
COPY ["public/", "public/"]
# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=build --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=build --chown=nextjs:nodejs /app/.next/static ./.next/static
# Environment variables must be redefined at run time
ARG SENTRY_AUTH_TOKEN \
SENTRY_ORG \
SENTRY_PROJECT_NAME \
Expand All @@ -62,3 +54,4 @@ ENV NEXT_TELEMETRY_DISABLED=1 \
SENTRY_PROJECT_NAME=$SENTRY_PROJECT_NAME \
SENTRY_URL=$SENTRY_URL
CMD yarn start
LABEL company="20tab" project="{{ cookiecutter.project_slug }}" service="frontend" stage="remote"
11 changes: 3 additions & 8 deletions {{cookiecutter.project_dirname}}/docker/test.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
# syntax=docker/dockerfile:1

FROM node:16-bullseye-slim
LABEL company="20tab" project="{{ cookiecutter.project_slug }}" service="frontend" stage="test"

ARG DEBIAN_FRONTEND=noninteractive
ARG USER=appuser
ENV APPUSER=$USER LANG=C.UTF-8 LC_ALL=C.UTF-8 NEXT_TELEMETRY_DISABLED=1 NODE_ENV="development" TZ='Europe/Rome' WORKDIR=/app
ENV LANG=C.UTF-8 LC_ALL=C.UTF-8 NEXT_TELEMETRY_DISABLED=1 NODE_ENV="development" TZ='Europe/Rome' WORKDIR=/app
RUN apt-get update \
&& apt-get install --assume-yes --no-install-recommends \
g++ \
make \
python3 \
&& rm -rf /var/lib/apt/lists/*
WORKDIR $WORKDIR
RUN addgroup --system --gid 997 $APPUSER
RUN adduser --system --uid 997 $APPUSER
USER $APPUSER
ENV PATH="$PATH:./node_modules/.bin"
COPY --chown=$APPUSER package.json yarn.lock ./
COPY package.json yarn.lock ./
ENTRYPOINT ["./scripts/test.sh"]
CMD yarn ci:unit-test && yarn ci:contract-test
LABEL company="20tab" project="{{ cookiecutter.project_slug }}" service="frontend" stage="test"
1 change: 1 addition & 0 deletions {{cookiecutter.project_dirname}}/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"build": "next build",
"ci:contract-test": "npx jest pact/contracts --testEnvironment=node --color --detectOpenHandles --passWithNoTests",
"ci:unit-test": "npx jest --color --silent --detectOpenHandles --testPathIgnorePatterns=pact/contracts --coverage --coverageDirectory ./coverage/tests --ci --reporters=default --reporters=jest-junit", "e2e": "CYPRESS_BASE_URL=http://localhost:3000 cypress open",
"e2e": "CYPRESS_BASE_URL=https://localhost:8443 cypress open",
"lint": "next lint",
"pact": "npx jest pact/contracts --testEnvironment=node --detectOpenHandles",
"start": "node server.js",
Expand Down
Loading

0 comments on commit e2f5863

Please sign in to comment.