From b87cd6c004aa486e5e2499ce1f908ffe306c7de4 Mon Sep 17 00:00:00 2001 From: Nuno Caseiro Date: Thu, 17 Nov 2022 12:04:31 +0000 Subject: [PATCH 1/8] chore(workflows): git flows --- .github/workflows/deploy_backend-dev.yml | 28 ++++++++++ .github/workflows/deploy_backend.yml | 2 +- .github/workflows/deploy_frontend-dev.yml | 28 ++++++++++ .../workflows/tests_backend_frontend-dev.yml | 51 +++++++++++++++++++ 4 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/deploy_backend-dev.yml create mode 100644 .github/workflows/deploy_frontend-dev.yml create mode 100644 .github/workflows/tests_backend_frontend-dev.yml diff --git a/.github/workflows/deploy_backend-dev.yml b/.github/workflows/deploy_backend-dev.yml new file mode 100644 index 000000000..e21f35430 --- /dev/null +++ b/.github/workflows/deploy_backend-dev.yml @@ -0,0 +1,28 @@ +name: Deploy backend + +on: + push: + branches: [ main ] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + + - uses: azure/docker-login@v1 + with: + login-server: split.azurecr.io + username: ${{ secrets.REGISTRY_USERNAME }} + password: ${{ secrets.REGISTRY_PASSWORD }} + + - run: | + docker build ./backend -f ./backend/docker/prod/Dockerfile -t split.azurecr.io/backend-dev:latest + docker push split.azurecr.io/backend-dev:latest + + - uses: azure/webapps-deploy@v2 + with: + app-name: 'split-dev-be' + publish-profile: ${{ secrets.AZURE_BACKEND_DEV_PUBLISH_PROFILE }} + images: 'split.azurecr.io/backend-dev:latest' diff --git a/.github/workflows/deploy_backend.yml b/.github/workflows/deploy_backend.yml index 2f8172858..a2e38e919 100644 --- a/.github/workflows/deploy_backend.yml +++ b/.github/workflows/deploy_backend.yml @@ -2,7 +2,7 @@ name: Deploy backend on: push: - branches: [ main ] + branches: [ dev ] jobs: build: diff --git a/.github/workflows/deploy_frontend-dev.yml b/.github/workflows/deploy_frontend-dev.yml new file mode 100644 index 000000000..d51abcb35 --- /dev/null +++ b/.github/workflows/deploy_frontend-dev.yml @@ -0,0 +1,28 @@ +name: Deploy frontend + +on: + push: + branches: [dev] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + + - uses: azure/docker-login@v1 + with: + login-server: split.azurecr.io + username: ${{ secrets.REGISTRY_USERNAME }} + password: ${{ secrets.REGISTRY_PASSWORD }} + + - run: | + docker build ./frontend -f ./frontend/docker/prod/Dockerfile --build-arg NEXT_PUBLIC_BACKEND_URL=${{secrets.NEXT_PUBLIC_BACKEND_DEV_URL}} --build-arg NEXT_PUBLIC_NEXTAUTH_URL=${{secrets.NEXT_PUBLIC_NEXTAUTH_DEV_URL}} --build-arg NEXT_PUBLIC_EXPIRATION_TIME=${{secrets.NEXT_PUBLIC_EXPIRATION_TIME}} --build-arg NEXT_PUBLIC_ENABLE_AZURE=${{secrets.NEXT_PUBLIC_ENABLE_AZURE}} -t split.azurecr.io/frontend-dev:latest + docker push split.azurecr.io/frontend-dev:latest + + - uses: azure/webapps-deploy@v2 + with: + app-name: 'split-fe-dev' + publish-profile: ${{ secrets.AZURE_FRONTEND_DEV_PUBLISH_PROFILE }} + images: 'split.azurecr.io/frontend-dev:latest' diff --git a/.github/workflows/tests_backend_frontend-dev.yml b/.github/workflows/tests_backend_frontend-dev.yml new file mode 100644 index 000000000..6f41b2aba --- /dev/null +++ b/.github/workflows/tests_backend_frontend-dev.yml @@ -0,0 +1,51 @@ +# This is a basic workflow to help you get started with Actions + +name: CI + +# Controls when the workflow will run +on: + # Triggers the workflow on push or pull request events but only for the main branch + push: + branches: [dev] + pull_request: + branches: [dev] + + workflow_dispatch: + +# A workflow run is made up of one or more jobs that can run sequentially or in parallel +jobs: + # This workflow contains a single job called "build" + build_test: + # The type of runner that the job will run on + runs-on: ubuntu-latest + + strategy: + matrix: + node-version: [16.x] + + # Steps represent a sequence of tasks that will be executed as part of the job + steps: + # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it + - uses: actions/checkout@v2 + - name: Use Node.js ${{ matrix.node-version }} + uses: actions/setup-node@v2 + with: + node-version: ${{ matrix.node-version }} + cache: 'npm' + - name: Cache multiple paths + uses: actions/cache@v2 + with: + path: ${{ github.workspace }}/.next/cache + key: ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-${{ hashFiles('**.[jt]s', '**.[jt]sx') }} + restore-keys: | + ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}- + - name: npm ci and test + run: | + cd backend + npm ci + npm test + - name: npm ci and test frontend + run: | + cd frontend + npm ci + npm run jest From 549180df2fbd5aacd89636f131bb2965e048e0b3 Mon Sep 17 00:00:00 2001 From: Nuno Caseiro Date: Thu, 17 Nov 2022 18:01:44 +0000 Subject: [PATCH 2/8] chore(dev): add development and local environment --- .github/workflows/deploy_backend-dev.yml | 8 +-- .github/workflows/deploy_backend.yml | 2 +- .github/workflows/deploy_frontend-dev.yml | 6 +- .github/workflows/deploy_frontend.yml | 2 +- .../workflows/tests_backend_frontend-dev.yml | 51 -------------- .github/workflows/tests_backend_frontend.yml | 4 +- backend/docker/dev/Dockerfile | 41 ++++++++---- backend/docker/local/Dockerfile | 20 ++++++ docker-compose.dev.yaml | 20 +++--- docker-compose.yaml | 24 +++++-- frontend/docker/dev/Dockerfile | 66 +++++++++++++++---- frontend/docker/local/Dockerfile | 20 ++++++ 12 files changed, 159 insertions(+), 105 deletions(-) delete mode 100644 .github/workflows/tests_backend_frontend-dev.yml create mode 100644 backend/docker/local/Dockerfile create mode 100644 frontend/docker/local/Dockerfile diff --git a/.github/workflows/deploy_backend-dev.yml b/.github/workflows/deploy_backend-dev.yml index e21f35430..6c84690ad 100644 --- a/.github/workflows/deploy_backend-dev.yml +++ b/.github/workflows/deploy_backend-dev.yml @@ -1,8 +1,8 @@ -name: Deploy backend +name: Deploy backend dev on: push: - branches: [ main ] + branches: [ dev ] jobs: build: @@ -18,11 +18,11 @@ jobs: password: ${{ secrets.REGISTRY_PASSWORD }} - run: | - docker build ./backend -f ./backend/docker/prod/Dockerfile -t split.azurecr.io/backend-dev:latest + docker build ./backend -f ./backend/docker/dev/Dockerfile -t split.azurecr.io/backend-dev:latest docker push split.azurecr.io/backend-dev:latest - uses: azure/webapps-deploy@v2 with: - app-name: 'split-dev-be' + app-name: 'split-be-dev' publish-profile: ${{ secrets.AZURE_BACKEND_DEV_PUBLISH_PROFILE }} images: 'split.azurecr.io/backend-dev:latest' diff --git a/.github/workflows/deploy_backend.yml b/.github/workflows/deploy_backend.yml index a2e38e919..2f8172858 100644 --- a/.github/workflows/deploy_backend.yml +++ b/.github/workflows/deploy_backend.yml @@ -2,7 +2,7 @@ name: Deploy backend on: push: - branches: [ dev ] + branches: [ main ] jobs: build: diff --git a/.github/workflows/deploy_frontend-dev.yml b/.github/workflows/deploy_frontend-dev.yml index d51abcb35..9c6ef2b15 100644 --- a/.github/workflows/deploy_frontend-dev.yml +++ b/.github/workflows/deploy_frontend-dev.yml @@ -1,8 +1,8 @@ -name: Deploy frontend +name: Deploy frontend dev on: push: - branches: [dev] + branches: [ dev ] jobs: build: @@ -18,7 +18,7 @@ jobs: password: ${{ secrets.REGISTRY_PASSWORD }} - run: | - docker build ./frontend -f ./frontend/docker/prod/Dockerfile --build-arg NEXT_PUBLIC_BACKEND_URL=${{secrets.NEXT_PUBLIC_BACKEND_DEV_URL}} --build-arg NEXT_PUBLIC_NEXTAUTH_URL=${{secrets.NEXT_PUBLIC_NEXTAUTH_DEV_URL}} --build-arg NEXT_PUBLIC_EXPIRATION_TIME=${{secrets.NEXT_PUBLIC_EXPIRATION_TIME}} --build-arg NEXT_PUBLIC_ENABLE_AZURE=${{secrets.NEXT_PUBLIC_ENABLE_AZURE}} -t split.azurecr.io/frontend-dev:latest + docker build ./frontend -f ./frontend/docker/dev/Dockerfile --build-arg NEXT_PUBLIC_BACKEND_URL=${{secrets.NEXT_PUBLIC_DEV_BACKEND_URL}} --build-arg NEXT_PUBLIC_NEXTAUTH_URL=${{secrets.NEXT_PUBLIC_DEV_NEXTAUTH_URL}} --build-arg NEXT_PUBLIC_EXPIRATION_TIME=${{secrets.NEXT_PUBLIC_EXPIRATION_TIME}} --build-arg NEXT_PUBLIC_ENABLE_AZURE=${{secrets.NEXT_PUBLIC_DEV_ENABLE_AZURE}} -t split.azurecr.io/frontend-dev:latest docker push split.azurecr.io/frontend-dev:latest - uses: azure/webapps-deploy@v2 diff --git a/.github/workflows/deploy_frontend.yml b/.github/workflows/deploy_frontend.yml index 22c640ad9..a78721bba 100644 --- a/.github/workflows/deploy_frontend.yml +++ b/.github/workflows/deploy_frontend.yml @@ -2,7 +2,7 @@ name: Deploy frontend on: push: - branches: [main] + branches: [ main ] jobs: build: diff --git a/.github/workflows/tests_backend_frontend-dev.yml b/.github/workflows/tests_backend_frontend-dev.yml deleted file mode 100644 index 6f41b2aba..000000000 --- a/.github/workflows/tests_backend_frontend-dev.yml +++ /dev/null @@ -1,51 +0,0 @@ -# This is a basic workflow to help you get started with Actions - -name: CI - -# Controls when the workflow will run -on: - # Triggers the workflow on push or pull request events but only for the main branch - push: - branches: [dev] - pull_request: - branches: [dev] - - workflow_dispatch: - -# A workflow run is made up of one or more jobs that can run sequentially or in parallel -jobs: - # This workflow contains a single job called "build" - build_test: - # The type of runner that the job will run on - runs-on: ubuntu-latest - - strategy: - matrix: - node-version: [16.x] - - # Steps represent a sequence of tasks that will be executed as part of the job - steps: - # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it - - uses: actions/checkout@v2 - - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v2 - with: - node-version: ${{ matrix.node-version }} - cache: 'npm' - - name: Cache multiple paths - uses: actions/cache@v2 - with: - path: ${{ github.workspace }}/.next/cache - key: ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-${{ hashFiles('**.[jt]s', '**.[jt]sx') }} - restore-keys: | - ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}- - - name: npm ci and test - run: | - cd backend - npm ci - npm test - - name: npm ci and test frontend - run: | - cd frontend - npm ci - npm run jest diff --git a/.github/workflows/tests_backend_frontend.yml b/.github/workflows/tests_backend_frontend.yml index d2b2aa39f..11da78c45 100644 --- a/.github/workflows/tests_backend_frontend.yml +++ b/.github/workflows/tests_backend_frontend.yml @@ -6,9 +6,9 @@ name: CI on: # Triggers the workflow on push or pull request events but only for the main branch push: - branches: [main] + branches: [ main, dev ] pull_request: - branches: [main] + branches: [ main, dev ] workflow_dispatch: diff --git a/backend/docker/dev/Dockerfile b/backend/docker/dev/Dockerfile index 1d53b74e2..58c5ef249 100644 --- a/backend/docker/dev/Dockerfile +++ b/backend/docker/dev/Dockerfile @@ -1,20 +1,35 @@ -# Dev Environment -FROM node:16-alpine AS development +FROM node:16-alpine -# Container Dir -WORKDIR /app/backend +ENV NODE_ENV build -# Copy package.json (to install all packages) -COPY package.json ./ +WORKDIR /backend -# Install the packages (on the package.json) -RUN yarn +COPY package*.json ./ -# Copy all files -COPY ./ . +RUN npm ci + +COPY ./ ./ + +RUN npm run build + +ARG NODE_ENV +ENV NODE_ENV=${NODE_ENV} -# Expose port EXPOSE 3200 +ENV PORT 3200 + +CMD ["node", "dist/src/main.js"] + +# --- + +# FROM node:16-alpine + + +# USER node +# WORKDIR /backend + +# COPY --from=builder ./package*.json /backend/ +# COPY --from=builder ./node_modules /backend/node_modules/ +# COPY --from=builder ./dist /backend/dist -# Run in dev mode -CMD ["yarn", "start:dev"] \ No newline at end of file +# CMD ["node", "dist/src/main.js"] \ No newline at end of file diff --git a/backend/docker/local/Dockerfile b/backend/docker/local/Dockerfile new file mode 100644 index 000000000..2f5847651 --- /dev/null +++ b/backend/docker/local/Dockerfile @@ -0,0 +1,20 @@ +# Dev Environment +FROM node:16-alpine AS local + +# Container Dir +WORKDIR /app/backend + +# Copy package.json (to install all packages) +COPY package.json ./ + +# Install the packages (on the package.json) +RUN yarn + +# Copy all files +COPY ./ . + +# Expose port +EXPOSE 3200 + +# Run in dev mode +CMD ["yarn", "start:dev"] \ No newline at end of file diff --git a/docker-compose.dev.yaml b/docker-compose.dev.yaml index 4936340e2..e14ff061f 100644 --- a/docker-compose.dev.yaml +++ b/docker-compose.dev.yaml @@ -4,17 +4,17 @@ services: backend: build: context: backend # starts on backend folder instead on root - target: development # choose development part from Dockerfile - dockerfile: docker/dev/Dockerfile + target: local # choose development part from Dockerfile + dockerfile: docker/local/Dockerfile # image name - image: dc-dev-backend + image: split-dev-backend # container name container_name: dev-backend networks: - - dc-network + - split-network depends_on: - mongo @@ -36,16 +36,16 @@ services: frontend: build: context: frontend # to start on frontend folder - dockerfile: docker/dev/Dockerfile # path to dev Dockerfile + dockerfile: docker/local/Dockerfile # path to dev Dockerfile # set image name - image: dc-dev-frontend + image: split-dev-frontend # set container name container_name: dev-frontend networks: - - dc-network + - split-network depends_on: - backend @@ -68,7 +68,7 @@ services: restart: always container_name: mongo2 networks: - - dc-network + - split-network ports: - "27018:27017" entrypoint: "bash -c '/usr/bin/mongod --replSet $${MONGO_REPLICA_NAME} --journal --bind_ip_all'" @@ -83,7 +83,7 @@ services: volumes: - ./database/rs-init.sh:/scripts/rs-init.sh networks: - - dc-network + - split-network links: - mongo2 command: "bash -c '/usr/bin/mongod --replSet $${MONGO_REPLICA_NAME} --journal --bind_ip_all'" @@ -106,5 +106,5 @@ volumes: # Networks networks: - dc-network: + split-network: driver: bridge diff --git a/docker-compose.yaml b/docker-compose.yaml index d9e6b850f..5cf1cd7c9 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -7,7 +7,7 @@ services: dockerfile: docker/prod/Dockerfile # set image name - image: dc-backend + image: split-backend # set container name container_name: backend @@ -20,7 +20,7 @@ services: restart: unless-stopped networks: - - dc-network + - split-network depends_on: - mongo frontend: @@ -33,7 +33,7 @@ services: - NEXT_PUBLIC_ENABLE_AZURE=${NEXT_PUBLIC_ENABLE_AZURE} # set image name - image: dc-frontend + image: split-frontend # set container name container_name: frontend @@ -46,7 +46,7 @@ services: restart: unless-stopped networks: - - dc-network + - split-network depends_on: - backend mongo2: @@ -54,7 +54,7 @@ services: restart: always container_name: mongo2 networks: - - dc-network + - split-network ports: - "27018:27017" entrypoint: "bash -c '/usr/bin/mongod --replSet $${MONGO_REPLICA_NAME} --journal --bind_ip_all'" @@ -71,13 +71,23 @@ services: volumes: - ./database/rs-init.sh:/scripts/rs-init.sh networks: - - dc-network + - split-network links: - mongo2 command: "bash -c '/usr/bin/mongod --replSet $${MONGO_REPLICA_NAME} --journal --bind_ip_all'" env_file: - ./database/.env + + redis: + image: redis + command: /bin/sh -c "redis-server --requirepass $$REDIS_PASSWORD" + environment: + - REDIS_DISABLE_COMMANDS=FLUSHDB,FLUSHALL + ports: + - '6379:6379' + env_file: + - ./database/redis/.env networks: - dc-network: + split-network: driver: bridge diff --git a/frontend/docker/dev/Dockerfile b/frontend/docker/dev/Dockerfile index 20bddd9b0..bd8c4c4db 100644 --- a/frontend/docker/dev/Dockerfile +++ b/frontend/docker/dev/Dockerfile @@ -1,20 +1,60 @@ -# Use Node image with version 16 -FROM node:16-alpine +# Install dependencies only when needed +FROM node:16-alpine AS deps +# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed. +RUN apk add --no-cache libc6-compat +WORKDIR /app +COPY ./package*.json ./ -# Container Dir -WORKDIR /app/frontend +RUN npm ci -# Copy package.json (to install all packages) -COPY ./package.json . +# If using npm with a `package-lock.json` comment out above and use below instead +# COPY package.json package-lock.json / +# RUN npm install -# Install the packages (on the package.json) -RUN yarn - -# Copy all files +# Rebuild the source code only when needed +FROM node:16-alpine AS builder +WORKDIR /app +COPY --from=deps /app/node_modules ./node_modules COPY ./ . -# Export the port used by Next.js +ARG NEXT_PUBLIC_ENABLE_AZURE +ENV NEXT_PUBLIC_ENABLE_AZURE=${NEXT_PUBLIC_DEV_ENABLE_AZURE} +ARG NEXT_PUBLIC_BACKEND_URL +ENV NEXT_PUBLIC_BACKEND_URL=${NEXT_PUBLIC_DEV_BACKEND_URL} +ARG NEXT_PUBLIC_NEXTAUTH_URL +ENV NEXT_PUBLIC_NEXTAUTH_URL=${NEXT_PUBLIC_DEV_NEXTAUTH_URL} + +RUN npm run build + +# Production image, copy all the files and run next +FROM node:16-alpine AS runner +WORKDIR /app + +ARG NODE_ENV +ENV NODE_ENV=${NODE_ENV} + +RUN addgroup -g 1001 -S nodejs +RUN adduser -S nextjs -u 1001 + +# You only need to copy next.config.js if you are NOT using the default configuration +# COPY --from=builder /app/next.config.js ./ +COPY --from=builder /app/public ./public +COPY --from=builder /app/package.json ./package.json + +# Automatically leverage output traces to reduce image size +# https://nextjs.org/docs/advanced-features/output-file-tracing +COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ +COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static + +USER nextjs + EXPOSE 3000 -# Run uarn dev (as you do via terminal) -CMD ["yarn", "dev"] \ No newline at end of file +ENV PORT 3000 + +# Next.js collects completely anonymous telemetry data about general usage. +# Learn more here: https://nextjs.org/telemetry +# Uncomment the following line in case you want to disable telemetry. +# ENV NEXT_TELEMETRY_DISABLED 1 + +CMD ["node", "server.js"] \ No newline at end of file diff --git a/frontend/docker/local/Dockerfile b/frontend/docker/local/Dockerfile new file mode 100644 index 000000000..20bddd9b0 --- /dev/null +++ b/frontend/docker/local/Dockerfile @@ -0,0 +1,20 @@ +# Use Node image with version 16 +FROM node:16-alpine + +# Container Dir +WORKDIR /app/frontend + +# Copy package.json (to install all packages) +COPY ./package.json . + +# Install the packages (on the package.json) +RUN yarn + +# Copy all files +COPY ./ . + +# Export the port used by Next.js +EXPOSE 3000 + +# Run uarn dev (as you do via terminal) +CMD ["yarn", "dev"] \ No newline at end of file From bb8b49f61815a2e6e8779ff232db17e298f0e70a Mon Sep 17 00:00:00 2001 From: Nuno Caseiro Date: Fri, 18 Nov 2022 20:25:19 +0000 Subject: [PATCH 3/8] chore(dev): remove unnecessary files --- .github/workflows/deploy_backend-dev.yml | 28 --------- .github/workflows/deploy_backend.yml | 28 --------- .github/workflows/deploy_backend_frontend.yml | 51 ++++++++++++++++ .github/workflows/deploy_frontend-dev.yml | 28 --------- .github/workflows/deploy_frontend.yml | 28 --------- .github/workflows/tests_backend_frontend.yml | 19 ++---- backend/.env.example | 2 +- backend/docker/dev/Dockerfile | 35 ----------- backend/docker/local/Dockerfile | 4 +- backend/package.json | 2 +- .../infrastructure/config/config.module.ts | 6 +- .../infrastructure/config/configuration.ts | 2 +- ...pose.dev.yaml => docker-compose.local.yaml | 0 docker-compose.yaml | 1 + frontend/docker/dev/Dockerfile | 60 ------------------- 15 files changed, 65 insertions(+), 229 deletions(-) delete mode 100644 .github/workflows/deploy_backend-dev.yml delete mode 100644 .github/workflows/deploy_backend.yml create mode 100644 .github/workflows/deploy_backend_frontend.yml delete mode 100644 .github/workflows/deploy_frontend-dev.yml delete mode 100644 .github/workflows/deploy_frontend.yml delete mode 100644 backend/docker/dev/Dockerfile rename docker-compose.dev.yaml => docker-compose.local.yaml (100%) delete mode 100644 frontend/docker/dev/Dockerfile diff --git a/.github/workflows/deploy_backend-dev.yml b/.github/workflows/deploy_backend-dev.yml deleted file mode 100644 index 6c84690ad..000000000 --- a/.github/workflows/deploy_backend-dev.yml +++ /dev/null @@ -1,28 +0,0 @@ -name: Deploy backend dev - -on: - push: - branches: [ dev ] - -jobs: - build: - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v2 - - - uses: azure/docker-login@v1 - with: - login-server: split.azurecr.io - username: ${{ secrets.REGISTRY_USERNAME }} - password: ${{ secrets.REGISTRY_PASSWORD }} - - - run: | - docker build ./backend -f ./backend/docker/dev/Dockerfile -t split.azurecr.io/backend-dev:latest - docker push split.azurecr.io/backend-dev:latest - - - uses: azure/webapps-deploy@v2 - with: - app-name: 'split-be-dev' - publish-profile: ${{ secrets.AZURE_BACKEND_DEV_PUBLISH_PROFILE }} - images: 'split.azurecr.io/backend-dev:latest' diff --git a/.github/workflows/deploy_backend.yml b/.github/workflows/deploy_backend.yml deleted file mode 100644 index 2f8172858..000000000 --- a/.github/workflows/deploy_backend.yml +++ /dev/null @@ -1,28 +0,0 @@ -name: Deploy backend - -on: - push: - branches: [ main ] - -jobs: - build: - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v2 - - - uses: azure/docker-login@v1 - with: - login-server: split.azurecr.io - username: ${{ secrets.REGISTRY_USERNAME }} - password: ${{ secrets.REGISTRY_PASSWORD }} - - - run: | - docker build ./backend -f ./backend/docker/prod/Dockerfile -t split.azurecr.io/backend:latest - docker push split.azurecr.io/backend:latest - - - uses: azure/webapps-deploy@v2 - with: - app-name: 'split-be' - publish-profile: ${{ secrets.AZURE_BACKEND_PUBLISH_PROFILE }} - images: 'split.azurecr.io/backend:latest' diff --git a/.github/workflows/deploy_backend_frontend.yml b/.github/workflows/deploy_backend_frontend.yml new file mode 100644 index 000000000..3695186e8 --- /dev/null +++ b/.github/workflows/deploy_backend_frontend.yml @@ -0,0 +1,51 @@ +name: Deploy + +on: + push: + branches: + - main + - dev + +jobs: + build_backend: + runs-on: ubuntu-latest + environment: ${{ github.ref_name == 'dev' && 'dev' || 'prod' }} + + steps: + - uses: actions/checkout@v2 + - uses: azure/docker-login@v1 + with: + login-server: split.azurecr.io + username: ${{ secrets.REGISTRY_USERNAME }} + password: ${{ secrets.REGISTRY_PASSWORD }} + + - run: | + docker build ./backend -f ./backend/docker/prod/Dockerfile -t split.azurecr.io/backend${{ github.ref_name == 'dev' && '-dev' || '' }}:latest + docker push split.azurecr.io/backend${{ github.ref_name == 'dev' && '-dev' || '' }}:latest + + - uses: azure/webapps-deploy@v2 + with: + app-name: ${{ github.ref_name == 'dev' && 'split-be-dev' || 'split-be' }} + publish-profile: ${{ secrets.AZURE_BACKEND_PUBLISH_PROFILE }} + images: ${{ github.ref_name == 'dev' && 'split.azurecr.io/backend-dev:latest' || 'split.azurecr.io/backend:latest' }} + + build_frontend: + runs-on: ubuntu-latest + environment: ${{ github.ref_name == 'dev' && 'dev' || 'prod' }} + + steps: + - uses: actions/checkout@v2 + - uses: azure/docker-login@v1 + with: + login-server: split.azurecr.io + username: ${{ secrets.REGISTRY_USERNAME }} + password: ${{ secrets.REGISTRY_PASSWORD }} + - run: | + docker build ./frontend -f ./frontend/docker/prod/Dockerfile --build-arg NEXT_PUBLIC_BACKEND_URL=${{secrets.NEXT_PUBLIC_BACKEND_URL}} --build-arg NEXT_PUBLIC_NEXTAUTH_URL=${{secrets.NEXT_PUBLIC_NEXTAUTH_URL}} --build-arg NEXT_PUBLIC_EXPIRATION_TIME=${{secrets.NEXT_PUBLIC_EXPIRATION_TIME}} --build-arg NEXT_PUBLIC_ENABLE_AZURE=${{secrets.NEXT_PUBLIC_ENABLE_AZURE}} -t split.azurecr.io/frontend${{ github.ref_name == 'dev' && '-dev' || '' }}:latest + docker push split.azurecr.io/frontend${{ github.ref_name == 'dev' && '-dev' || '' }}:latest + + - uses: azure/webapps-deploy@v2 + with: + app-name: ${{ github.ref_name == 'dev' && 'split-fe-dev' || 'split-fe' }} + publish-profile: ${{ secrets.AZURE_FRONTEND_PUBLISH_PROFILE }} + images: ${{ github.ref_name == 'dev' && 'split.azurecr.io/frontend-dev:latest' || 'split.azurecr.io/frontend:latest' }} diff --git a/.github/workflows/deploy_frontend-dev.yml b/.github/workflows/deploy_frontend-dev.yml deleted file mode 100644 index 9c6ef2b15..000000000 --- a/.github/workflows/deploy_frontend-dev.yml +++ /dev/null @@ -1,28 +0,0 @@ -name: Deploy frontend dev - -on: - push: - branches: [ dev ] - -jobs: - build: - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v2 - - - uses: azure/docker-login@v1 - with: - login-server: split.azurecr.io - username: ${{ secrets.REGISTRY_USERNAME }} - password: ${{ secrets.REGISTRY_PASSWORD }} - - - run: | - docker build ./frontend -f ./frontend/docker/dev/Dockerfile --build-arg NEXT_PUBLIC_BACKEND_URL=${{secrets.NEXT_PUBLIC_DEV_BACKEND_URL}} --build-arg NEXT_PUBLIC_NEXTAUTH_URL=${{secrets.NEXT_PUBLIC_DEV_NEXTAUTH_URL}} --build-arg NEXT_PUBLIC_EXPIRATION_TIME=${{secrets.NEXT_PUBLIC_EXPIRATION_TIME}} --build-arg NEXT_PUBLIC_ENABLE_AZURE=${{secrets.NEXT_PUBLIC_DEV_ENABLE_AZURE}} -t split.azurecr.io/frontend-dev:latest - docker push split.azurecr.io/frontend-dev:latest - - - uses: azure/webapps-deploy@v2 - with: - app-name: 'split-fe-dev' - publish-profile: ${{ secrets.AZURE_FRONTEND_DEV_PUBLISH_PROFILE }} - images: 'split.azurecr.io/frontend-dev:latest' diff --git a/.github/workflows/deploy_frontend.yml b/.github/workflows/deploy_frontend.yml deleted file mode 100644 index a78721bba..000000000 --- a/.github/workflows/deploy_frontend.yml +++ /dev/null @@ -1,28 +0,0 @@ -name: Deploy frontend - -on: - push: - branches: [ main ] - -jobs: - build: - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v2 - - - uses: azure/docker-login@v1 - with: - login-server: split.azurecr.io - username: ${{ secrets.REGISTRY_USERNAME }} - password: ${{ secrets.REGISTRY_PASSWORD }} - - - run: | - docker build ./frontend -f ./frontend/docker/prod/Dockerfile --build-arg NEXT_PUBLIC_BACKEND_URL=${{secrets.NEXT_PUBLIC_BACKEND_URL}} --build-arg NEXT_PUBLIC_NEXTAUTH_URL=${{secrets.NEXT_PUBLIC_NEXTAUTH_URL}} --build-arg NEXT_PUBLIC_EXPIRATION_TIME=${{secrets.NEXT_PUBLIC_EXPIRATION_TIME}} --build-arg NEXT_PUBLIC_ENABLE_AZURE=${{secrets.NEXT_PUBLIC_ENABLE_AZURE}} -t split.azurecr.io/frontend:latest - docker push split.azurecr.io/frontend:latest - - - uses: azure/webapps-deploy@v2 - with: - app-name: 'split-fe' - publish-profile: ${{ secrets.AZURE_FRONTEND_PUBLISH_PROFILE }} - images: 'split.azurecr.io/frontend:latest' diff --git a/.github/workflows/tests_backend_frontend.yml b/.github/workflows/tests_backend_frontend.yml index 11da78c45..77eabba0d 100644 --- a/.github/workflows/tests_backend_frontend.yml +++ b/.github/workflows/tests_backend_frontend.yml @@ -1,37 +1,28 @@ -# This is a basic workflow to help you get started with Actions +name: Test -name: CI - -# Controls when the workflow will run on: - # Triggers the workflow on push or pull request events but only for the main branch - push: - branches: [ main, dev ] pull_request: - branches: [ main, dev ] + branches: + - main + - dev workflow_dispatch: -# A workflow run is made up of one or more jobs that can run sequentially or in parallel jobs: - # This workflow contains a single job called "build" build_test: - # The type of runner that the job will run on runs-on: ubuntu-latest strategy: matrix: node-version: [16.x] - # Steps represent a sequence of tasks that will be executed as part of the job steps: - # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it - uses: actions/checkout@v2 - name: Use Node.js ${{ matrix.node-version }} uses: actions/setup-node@v2 with: node-version: ${{ matrix.node-version }} - cache: 'npm' + cache: "npm" - name: Cache multiple paths uses: actions/cache@v2 with: diff --git a/backend/.env.example b/backend/.env.example index c1d30488e..3fe606f11 100644 --- a/backend/.env.example +++ b/backend/.env.example @@ -3,7 +3,7 @@ BACKEND_PORT=3200 # NODE ENV -NODE_ENV=dev +NODE_ENV=local # -> Daatabase <- # # db user diff --git a/backend/docker/dev/Dockerfile b/backend/docker/dev/Dockerfile deleted file mode 100644 index 58c5ef249..000000000 --- a/backend/docker/dev/Dockerfile +++ /dev/null @@ -1,35 +0,0 @@ -FROM node:16-alpine - -ENV NODE_ENV build - -WORKDIR /backend - -COPY package*.json ./ - -RUN npm ci - -COPY ./ ./ - -RUN npm run build - -ARG NODE_ENV -ENV NODE_ENV=${NODE_ENV} - -EXPOSE 3200 -ENV PORT 3200 - -CMD ["node", "dist/src/main.js"] - -# --- - -# FROM node:16-alpine - - -# USER node -# WORKDIR /backend - -# COPY --from=builder ./package*.json /backend/ -# COPY --from=builder ./node_modules /backend/node_modules/ -# COPY --from=builder ./dist /backend/dist - -# CMD ["node", "dist/src/main.js"] \ No newline at end of file diff --git a/backend/docker/local/Dockerfile b/backend/docker/local/Dockerfile index 2f5847651..d28e2000b 100644 --- a/backend/docker/local/Dockerfile +++ b/backend/docker/local/Dockerfile @@ -8,7 +8,7 @@ WORKDIR /app/backend COPY package.json ./ # Install the packages (on the package.json) -RUN yarn +RUN npm i # Copy all files COPY ./ . @@ -17,4 +17,4 @@ COPY ./ . EXPOSE 3200 # Run in dev mode -CMD ["yarn", "start:dev"] \ No newline at end of file +CMD ["npm", "start:dev"] \ No newline at end of file diff --git a/backend/package.json b/backend/package.json index 311ce50fc..1bf2c9bbb 100644 --- a/backend/package.json +++ b/backend/package.json @@ -10,7 +10,7 @@ "build": "nest build", "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", "start": "nest start", - "start:dev": "NODE_ENV=dev nest start --watch", + "start:dev": "NODE_ENV=local nest start --watch", "start:staging": "NODE_ENV=staging nest start --watch", "start:debug": "nest start --debug --watch", "start:prod": "NODE_ENV=prod nest start --watch", diff --git a/backend/src/infrastructure/config/config.module.ts b/backend/src/infrastructure/config/config.module.ts index ab1aada0a..ba9ed3a35 100644 --- a/backend/src/infrastructure/config/config.module.ts +++ b/backend/src/infrastructure/config/config.module.ts @@ -12,18 +12,18 @@ const NODE_ENV = process.env.NODE_ENV; envFilePath: !NODE_ENV || NODE_ENV === 'dev' ? '.env' : `.env.${NODE_ENV}`, load: [configuration], validationSchema: Joi.object({ - NODE_ENV: Joi.string().valid('dev', 'prod', 'test', 'staging').default('dev'), + NODE_ENV: Joi.string().valid('dev', 'prod', 'test', 'staging', 'local').default('local'), DB_HOST: Joi.string().required(), DB_USER: Joi.string().required(), DB_PASSWORD: Joi.string().required(), DB_NAME: Joi.string().required(), DB_PORT: Joi.any().when('NODE_ENV', { - is: 'dev', + is: 'local', then: Joi.required(), otherwise: Joi.optional() }), DB_REPLICA_SET: Joi.any().when('NODE_ENV', { - is: 'dev', + is: 'local', then: Joi.required(), otherwise: Joi.optional() }), diff --git a/backend/src/infrastructure/config/configuration.ts b/backend/src/infrastructure/config/configuration.ts index eb907d4ec..e5ec98ec1 100644 --- a/backend/src/infrastructure/config/configuration.ts +++ b/backend/src/infrastructure/config/configuration.ts @@ -13,7 +13,7 @@ export const configuration = (): Configuration => { }, database: { uri: - NODE_ENV === 'dev' + NODE_ENV === 'local' ? `mongodb://${process.env.DB_USER}:${process.env.DB_PASSWORD}@${process.env.DB_HOST}:${process.env.DB_PORT}/${process.env.DB_NAME}?authSource=admin&replicaSet=${process.env.DB_REPLICA_SET}&readPreference=primary&directConnection=true` : `mongodb://${process.env.DB_USER}:${process.env.DB_PASSWORD}@${process.env.DB_HOST}:${process.env.DB_PORT}/${process.env.DB_NAME}?ssl=true&replicaSet=globaldb&retrywrites=false&maxIdleTimeMS=120000&appName=@${process.env.DB_USER}@` }, diff --git a/docker-compose.dev.yaml b/docker-compose.local.yaml similarity index 100% rename from docker-compose.dev.yaml rename to docker-compose.local.yaml diff --git a/docker-compose.yaml b/docker-compose.yaml index 5cf1cd7c9..6bbdef6f9 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -80,6 +80,7 @@ services: - ./database/.env redis: + container_name: redis image: redis command: /bin/sh -c "redis-server --requirepass $$REDIS_PASSWORD" environment: diff --git a/frontend/docker/dev/Dockerfile b/frontend/docker/dev/Dockerfile deleted file mode 100644 index bd8c4c4db..000000000 --- a/frontend/docker/dev/Dockerfile +++ /dev/null @@ -1,60 +0,0 @@ -# Install dependencies only when needed -FROM node:16-alpine AS deps -# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed. -RUN apk add --no-cache libc6-compat -WORKDIR /app -COPY ./package*.json ./ - -RUN npm ci - -# If using npm with a `package-lock.json` comment out above and use below instead -# COPY package.json package-lock.json / -# RUN npm install - -# Rebuild the source code only when needed -FROM node:16-alpine AS builder -WORKDIR /app -COPY --from=deps /app/node_modules ./node_modules -COPY ./ . - -ARG NEXT_PUBLIC_ENABLE_AZURE -ENV NEXT_PUBLIC_ENABLE_AZURE=${NEXT_PUBLIC_DEV_ENABLE_AZURE} -ARG NEXT_PUBLIC_BACKEND_URL -ENV NEXT_PUBLIC_BACKEND_URL=${NEXT_PUBLIC_DEV_BACKEND_URL} -ARG NEXT_PUBLIC_NEXTAUTH_URL -ENV NEXT_PUBLIC_NEXTAUTH_URL=${NEXT_PUBLIC_DEV_NEXTAUTH_URL} - -RUN npm run build - -# Production image, copy all the files and run next -FROM node:16-alpine AS runner -WORKDIR /app - -ARG NODE_ENV -ENV NODE_ENV=${NODE_ENV} - -RUN addgroup -g 1001 -S nodejs -RUN adduser -S nextjs -u 1001 - -# You only need to copy next.config.js if you are NOT using the default configuration -# COPY --from=builder /app/next.config.js ./ -COPY --from=builder /app/public ./public -COPY --from=builder /app/package.json ./package.json - -# Automatically leverage output traces to reduce image size -# https://nextjs.org/docs/advanced-features/output-file-tracing -COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ -COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static - -USER nextjs - -EXPOSE 3000 - -ENV PORT 3000 - -# Next.js collects completely anonymous telemetry data about general usage. -# Learn more here: https://nextjs.org/telemetry -# Uncomment the following line in case you want to disable telemetry. -# ENV NEXT_TELEMETRY_DISABLED 1 - -CMD ["node", "server.js"] \ No newline at end of file From 286bdeb23ee5f8f14ca8eeef568b81e1ff43eca9 Mon Sep 17 00:00:00 2001 From: Rui Silva Date: Tue, 22 Nov 2022 10:43:03 +0000 Subject: [PATCH 4/8] chore: remove package vulnerabilities --- backend/package-lock.json | 12 ++++++------ frontend/package-lock.json | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/backend/package-lock.json b/backend/package-lock.json index e4da9e621..54a63e74d 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -6077,9 +6077,9 @@ } }, "node_modules/engine.io": { - "version": "6.2.0", - "resolved": "https://registry.npmjs.org/engine.io/-/engine.io-6.2.0.tgz", - "integrity": "sha512-4KzwW3F3bk+KlzSOY57fj/Jx6LyRQ1nbcyIadehl+AnXjKT7gDO0ORdRi/84ixvMKTym6ZKuxvbzN62HDDU1Lg==", + "version": "6.2.1", + "resolved": "https://registry.npmjs.org/engine.io/-/engine.io-6.2.1.tgz", + "integrity": "sha512-ECceEFcAaNRybd3lsGQKas3ZlMVjN3cyWwMP25D2i0zWfyiytVbTpRPa34qrr+FHddtpBVOmq4H/DCv1O0lZRA==", "dependencies": { "@types/cookie": "^0.4.1", "@types/cors": "^2.8.12", @@ -20806,9 +20806,9 @@ } }, "engine.io": { - "version": "6.2.0", - "resolved": "https://registry.npmjs.org/engine.io/-/engine.io-6.2.0.tgz", - "integrity": "sha512-4KzwW3F3bk+KlzSOY57fj/Jx6LyRQ1nbcyIadehl+AnXjKT7gDO0ORdRi/84ixvMKTym6ZKuxvbzN62HDDU1Lg==", + "version": "6.2.1", + "resolved": "https://registry.npmjs.org/engine.io/-/engine.io-6.2.1.tgz", + "integrity": "sha512-ECceEFcAaNRybd3lsGQKas3ZlMVjN3cyWwMP25D2i0zWfyiytVbTpRPa34qrr+FHddtpBVOmq4H/DCv1O0lZRA==", "requires": { "@types/cookie": "^0.4.1", "@types/cors": "^2.8.12", diff --git a/frontend/package-lock.json b/frontend/package-lock.json index 53d8e76fd..fa3a653a8 100644 --- a/frontend/package-lock.json +++ b/frontend/package-lock.json @@ -10253,9 +10253,9 @@ } }, "node_modules/loader-utils": { - "version": "2.0.2", - "resolved": "https://registry.npmjs.org/loader-utils/-/loader-utils-2.0.2.tgz", - "integrity": "sha512-TM57VeHptv569d/GKh6TAYdzKblwDNiumOdkFnejjD0XwTH87K90w3O7AiJRqdQoXygvi1VQTJTLGhJl7WqA7A==", + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/loader-utils/-/loader-utils-2.0.4.tgz", + "integrity": "sha512-xXqpXoINfFhgua9xiqD8fPFHgkoq1mmmpE92WlDbm9rNRd/EbRb+Gqf908T2DMfuHjjJlksiK2RbHVOdD/MqSw==", "dev": true, "dependencies": { "big.js": "^5.2.2", @@ -21101,9 +21101,9 @@ "integrity": "sha512-3R/1M+yS3j5ou80Me59j7F9IMs4PXs3VqRrm0TU3AbKPxlmpoY1TNscJV/oGJXo8qCatFGTfDbY6W6ipGOYXfg==" }, "loader-utils": { - "version": "2.0.2", - "resolved": "https://registry.npmjs.org/loader-utils/-/loader-utils-2.0.2.tgz", - "integrity": "sha512-TM57VeHptv569d/GKh6TAYdzKblwDNiumOdkFnejjD0XwTH87K90w3O7AiJRqdQoXygvi1VQTJTLGhJl7WqA7A==", + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/loader-utils/-/loader-utils-2.0.4.tgz", + "integrity": "sha512-xXqpXoINfFhgua9xiqD8fPFHgkoq1mmmpE92WlDbm9rNRd/EbRb+Gqf908T2DMfuHjjJlksiK2RbHVOdD/MqSw==", "dev": true, "requires": { "big.js": "^5.2.2", From 2dae82554b82ac88820dbd4c037d1948ed04734f Mon Sep 17 00:00:00 2001 From: Rui Silva Date: Tue, 22 Nov 2022 10:59:57 +0000 Subject: [PATCH 5/8] refactor(ci): made some changes on pr workflow --- .github/workflows/tests_backend_frontend.yml | 204 +++++++++++++++---- 1 file changed, 167 insertions(+), 37 deletions(-) diff --git a/.github/workflows/tests_backend_frontend.yml b/.github/workflows/tests_backend_frontend.yml index 77eabba0d..a935dfa32 100644 --- a/.github/workflows/tests_backend_frontend.yml +++ b/.github/workflows/tests_backend_frontend.yml @@ -1,42 +1,172 @@ -name: Test +name: Test & Build on: - pull_request: - branches: - - main - - dev + pull_request: + branches: + - main - workflow_dispatch: + workflow_dispatch: + +env: + NODE_VERSION: 18.x jobs: - build_test: - runs-on: ubuntu-latest - - strategy: - matrix: - node-version: [16.x] - - steps: - - uses: actions/checkout@v2 - - name: Use Node.js ${{ matrix.node-version }} - uses: actions/setup-node@v2 - with: - node-version: ${{ matrix.node-version }} - cache: "npm" - - name: Cache multiple paths - uses: actions/cache@v2 - with: - path: ${{ github.workspace }}/.next/cache - key: ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-${{ hashFiles('**.[jt]s', '**.[jt]sx') }} - restore-keys: | - ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}- - - name: npm ci and test - run: | - cd backend - npm ci - npm test - - name: npm ci and test frontend - run: | - cd frontend - npm ci - npm run jest + install: + name: Install Dependencies + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Use Node.js ${{ env.NODE_VERSION}} + uses: actions/setup-node@v2 + with: + node-version: ${{ env.NODE_VERSION}} + cache: "npm" + + - name: Cache multiple paths + uses: actions/cache@v3 + id: cache + with: + path: | + **/node_modules + ${{ steps.cache.outputs.dir }} + key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }} + restore-keys: | + ${{ runner.os }}-npm- + + - name: Install + if: steps.cache.outputs.cache-hit != 'true' + run: npm ci + + build_backend: + name: Build Backend + runs-on: ubuntu-latest + needs: install + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Use Node.js ${{ env.NODE_VERSION}} + uses: actions/setup-node@v2 + with: + node-version: ${{ env.NODE_VERSION}} + cache: "npm" + + - name: Cache multiple paths + uses: actions/cache@v3 + id: cache + with: + path: | + **/node_modules + ${{ steps.cache.outputs.dir }} + key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }} + restore-keys: | + ${{ runner.os }}-npm- + + - name: Install + if: steps.cache.outputs.cache-hit != 'true' + run: npm ci + + - name: Build + run: npm run build + working-directory: backend + + test_backend: + name: Test Backend + runs-on: ubuntu-latest + needs: install + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Use Node.js ${{ env.NODE_VERSION}} + uses: actions/setup-node@v2 + with: + node-version: ${{ env.NODE_VERSION}} + cache: "npm" + + - name: Cache multiple paths + uses: actions/cache@v3 + id: cache + with: + path: | + **/node_modules + ${{ steps.cache.outputs.dir }} + key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }} + restore-keys: | + ${{ runner.os }}-npm- + + - name: Install + if: steps.cache.outputs.cache-hit != 'true' + run: npm ci + + - name: Build + run: npm run test:cov + working-directory: backend + + build_frontend: + name: Build Frontend + runs-on: ubuntu-latest + needs: install + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Use Node.js ${{ env.NODE_VERSION}} + uses: actions/setup-node@v2 + with: + node-version: ${{ env.NODE_VERSION}} + cache: "npm" + + - name: Cache multiple paths + uses: actions/cache@v3 + id: cache + with: + path: | + **/node_modules + ${{ steps.cache.outputs.dir }} + key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }} + restore-keys: | + ${{ runner.os }}-npm- + + - name: Install + if: steps.cache.outputs.cache-hit != 'true' + run: npm ci + + - name: Build + run: npm run build + working-directory: frontend + + test_frontend: + name: Test Frontend + runs-on: ubuntu-latest + needs: install + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Use Node.js ${{ env.NODE_VERSION}} + uses: actions/setup-node@v2 + with: + node-version: ${{ env.NODE_VERSION}} + cache: "npm" + + - name: Cache multiple paths + uses: actions/cache@v3 + id: cache + with: + path: | + **/node_modules + ${{ steps.cache.outputs.dir }} + key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }} + restore-keys: | + ${{ runner.os }}-npm- + + - name: Install + if: steps.cache.outputs.cache-hit != 'true' + run: npm ci + + - name: Build + run: npm run jest -- --coverage + working-directory: frontend \ No newline at end of file From 39bde683a8d46a6e868fd0479d53ada715975427 Mon Sep 17 00:00:00 2001 From: Rui Silva Date: Tue, 22 Nov 2022 11:23:58 +0000 Subject: [PATCH 6/8] chore: fix typo --- .github/workflows/tests_backend_frontend.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests_backend_frontend.yml b/.github/workflows/tests_backend_frontend.yml index a935dfa32..419913fc0 100644 --- a/.github/workflows/tests_backend_frontend.yml +++ b/.github/workflows/tests_backend_frontend.yml @@ -101,7 +101,7 @@ jobs: if: steps.cache.outputs.cache-hit != 'true' run: npm ci - - name: Build + - name: Test run: npm run test:cov working-directory: backend @@ -167,6 +167,6 @@ jobs: if: steps.cache.outputs.cache-hit != 'true' run: npm ci - - name: Build + - name: Test run: npm run jest -- --coverage working-directory: frontend \ No newline at end of file From e987675a5e7a677d2d8825b90714e8781a8d5bfe Mon Sep 17 00:00:00 2001 From: Rui Silva Date: Tue, 22 Nov 2022 12:33:24 +0000 Subject: [PATCH 7/8] feat(ci): added workflows to build and deploy --- .github/release-drafter.yml | 59 +++++++ .github/workflows/build_docker.yaml | 68 ++++++++ .github/workflows/deploy_backend_frontend.yml | 159 ++++++++++++------ .github/workflows/on_call_deploy.yaml | 31 ++++ .github/workflows/tests_backend_frontend.yml | 4 + backend/docker/prod/Dockerfile | 35 ++-- 6 files changed, 284 insertions(+), 72 deletions(-) create mode 100644 .github/release-drafter.yml create mode 100644 .github/workflows/build_docker.yaml create mode 100644 .github/workflows/on_call_deploy.yaml diff --git a/.github/release-drafter.yml b/.github/release-drafter.yml new file mode 100644 index 000000000..baf2907c5 --- /dev/null +++ b/.github/release-drafter.yml @@ -0,0 +1,59 @@ +# This release drafter follows the conventions +# from https://keepachangelog.com + +name-template: 'v$RESOLVED_VERSION' +tag-template: 'v$RESOLVED_VERSION' +template: | + ## What Changed 👀 + + $CHANGES + + **Full Changelog**: https://github.com/$OWNER/$REPOSITORY/compare/$PREVIOUS_TAG...v$RESOLVED_VERSION + +categories: + - title: 🚀 Features + labels: + - feature + - enhancement + - title: 🐛 Bug Fixes + labels: + - fix + - bug + - title: ⚠️ Changes + labels: + - changed + - title: ⛔️ Deprecated + labels: + - deprecated + - title: 🗑 Removed + labels: + - removed + - title: 🔐 Security + labels: + - security + - title: 📄 Documentation + labels: + - docs + - documentation + - title: 🧩 Dependency Updates + labels: + - deps + - dependencies + collapse-after: 5 + +change-template: '- $TITLE @$AUTHOR (#$NUMBER)' +change-title-escapes: '\<*_&' # You can add # and @ to disable mentions, and add ` to disable code blocks. +version-resolver: + major: + labels: + - major + minor: + labels: + - minor + patch: + labels: + - patch + default: patch + +exclude-labels: + - skip-changelog diff --git a/.github/workflows/build_docker.yaml b/.github/workflows/build_docker.yaml new file mode 100644 index 000000000..1cccbad97 --- /dev/null +++ b/.github/workflows/build_docker.yaml @@ -0,0 +1,68 @@ +name: Build Docker Image + +on: + workflow_call: + inputs: + DOCKERFILE: + required: true + type: string + REPOSITORY: + required: true + type: string + BUILD_ARGS: + required: false + type: string + default: '' + +env: + REGISTRY: split.azurecr.io + TAG: ${{ github.sha }} + +jobs: + build_docker: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v2 + + - name: Set up Docker Context for Buildx + run: | + docker context create builders + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + with: + endpoint: builders + + - uses: azure/docker-login@v1 + with: + login-server: ${{ env.REGISTRY }} + username: ${{ secrets.REGISTRY_USERNAME }} + password: ${{ secrets.REGISTRY_PASSWORD }} + + - name: Setup Docker metadata + id: meta + uses: docker/metadata-action@v4 + with: + images: ${{ env.REGISTRY }}/${{ inputs.REPOSITORY }} + tags: | + type=raw,value=${{ env.TAG }} + type=raw,value=cache + + - name: Build and Push + uses: docker/build-push-action@v3 + with: + context: . + file: ${{ inputs.DOCKERFILE }} + push: true + target: production + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + cache-from: | + type=registry,ref=${{ env.REGISTRY }}/${{ inputs.REPOSITORY }}:${{ env.TAG }} + type=registry,ref=${{ env.REGISTRY }}/${{ inputs.REPOSITORY }}:cache + cache-to: type=inline + build-args: ${{ inputs.BUILD_ARGS }} \ No newline at end of file diff --git a/.github/workflows/deploy_backend_frontend.yml b/.github/workflows/deploy_backend_frontend.yml index 3695186e8..e32a59c14 100644 --- a/.github/workflows/deploy_backend_frontend.yml +++ b/.github/workflows/deploy_backend_frontend.yml @@ -1,51 +1,114 @@ -name: Deploy +name: Build and Deploy -on: - push: - branches: - - main - - dev +on: + push: + branches: + - main + +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number }} + cancel-in-progress: true jobs: - build_backend: - runs-on: ubuntu-latest - environment: ${{ github.ref_name == 'dev' && 'dev' || 'prod' }} - - steps: - - uses: actions/checkout@v2 - - uses: azure/docker-login@v1 - with: - login-server: split.azurecr.io - username: ${{ secrets.REGISTRY_USERNAME }} - password: ${{ secrets.REGISTRY_PASSWORD }} - - - run: | - docker build ./backend -f ./backend/docker/prod/Dockerfile -t split.azurecr.io/backend${{ github.ref_name == 'dev' && '-dev' || '' }}:latest - docker push split.azurecr.io/backend${{ github.ref_name == 'dev' && '-dev' || '' }}:latest - - - uses: azure/webapps-deploy@v2 - with: - app-name: ${{ github.ref_name == 'dev' && 'split-be-dev' || 'split-be' }} - publish-profile: ${{ secrets.AZURE_BACKEND_PUBLISH_PROFILE }} - images: ${{ github.ref_name == 'dev' && 'split.azurecr.io/backend-dev:latest' || 'split.azurecr.io/backend:latest' }} - - build_frontend: - runs-on: ubuntu-latest - environment: ${{ github.ref_name == 'dev' && 'dev' || 'prod' }} - - steps: - - uses: actions/checkout@v2 - - uses: azure/docker-login@v1 - with: - login-server: split.azurecr.io - username: ${{ secrets.REGISTRY_USERNAME }} - password: ${{ secrets.REGISTRY_PASSWORD }} - - run: | - docker build ./frontend -f ./frontend/docker/prod/Dockerfile --build-arg NEXT_PUBLIC_BACKEND_URL=${{secrets.NEXT_PUBLIC_BACKEND_URL}} --build-arg NEXT_PUBLIC_NEXTAUTH_URL=${{secrets.NEXT_PUBLIC_NEXTAUTH_URL}} --build-arg NEXT_PUBLIC_EXPIRATION_TIME=${{secrets.NEXT_PUBLIC_EXPIRATION_TIME}} --build-arg NEXT_PUBLIC_ENABLE_AZURE=${{secrets.NEXT_PUBLIC_ENABLE_AZURE}} -t split.azurecr.io/frontend${{ github.ref_name == 'dev' && '-dev' || '' }}:latest - docker push split.azurecr.io/frontend${{ github.ref_name == 'dev' && '-dev' || '' }}:latest - - - uses: azure/webapps-deploy@v2 - with: - app-name: ${{ github.ref_name == 'dev' && 'split-fe-dev' || 'split-fe' }} - publish-profile: ${{ secrets.AZURE_FRONTEND_PUBLISH_PROFILE }} - images: ${{ github.ref_name == 'dev' && 'split.azurecr.io/frontend-dev:latest' || 'split.azurecr.io/frontend:latest' }} + build_backend: + name: Build backend + uses: ./.github/workflows/build_docker.yaml + with: + DOCKERFILE: ./backend/docker/prod/Dockerfile + REPOSITORY: backend + + build_frontend: + name: Build frontend + uses: ./.github/workflows/build_docker.yaml + with: + DOCKERFILE: ./frontend/docker/prod/Dockerfile + REPOSITORY: frontend + BUILD_ARGS: | + NEXT_PUBLIC_BACKEND_URL=${{secrets.NEXT_PUBLIC_BACKEND_URL}} + NEXT_PUBLIC_NEXTAUTH_URL=${{secrets.NEXT_PUBLIC_NEXTAUTH_URL}} + NEXT_PUBLIC_EXPIRATION_TIME=${{secrets.NEXT_PUBLIC_EXPIRATION_TIME}} + NEXT_PUBLIC_ENABLE_AZURE=${{secrets.NEXT_PUBLIC_ENABLE_AZURE}} + + deploy_backend_dev: + name: Deploy backend DEV + needs: build_backend + uses: ./.github/workflows/on_call_deploy.yaml + with: + ENVIRONMENT: dev + APP_NAME: split-be-dev + PUBLISH_PROFILE: ${{ secrets.AZURE_BACKEND_PUBLISH_PROFILE }} + REPOSITORY: backend + + deploy_frontend_dev: + name: Deploy frontend DEV + needs: build_frontend + uses: ./.github/workflows/on_call_deploy.yaml + with: + ENVIRONMENT: dev + APP_NAME: split-fe-dev + PUBLISH_PROFILE: ${{ secrets.AZURE_FRONTEND_PUBLISH_PROFILE }} + REPOSITORY: frontend + + deploy_backend_prod: + name: Deploy backend PROD + needs: deploy_backend_dev + uses: ./.github/workflows/on_call_deploy.yaml + with: + ENVIRONMENT: prod + APP_NAME: split-be + PUBLISH_PROFILE: ${{ secrets.AZURE_BACKEND_PUBLISH_PROFILE }} + REPOSITORY: backend + + deploy_frontend_prod: + name: Deploy frontend PROD + needs: deploy_frontend_dev + uses: ./.github/workflows/on_call_deploy.yaml + with: + ENVIRONMENT: prod + APP_NAME: split-fe + PUBLISH_PROFILE: ${{ secrets.AZURE_FRONTEND_PUBLISH_PROFILE }} + REPOSITORY: frontend + + update_release_draft: + name: Release Drafter + runs-on: ubuntu-latest + needs: + - deploy_backend_dev + - deploy_frontend_dev + steps: + - name: Update release draft + uses: release-drafter/release-drafter@v5 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + pre_release: + name: Create Pre Release + runs-on: ubuntu-latest + permissions: + contents: write + needs: + - deploy_backend_dev + - deploy_frontend_dev + steps: + - name: Update release draft + uses: release-drafter/release-drafter@v5 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + prerelease: true + + release: + name: Create Release + runs-on: ubuntu-latest + permissions: + contents: write + needs: + - deploy_backend_prod + - deploy_frontend_prod + steps: + - name: Update release draft + uses: release-drafter/release-drafter@v5 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + publish: true \ No newline at end of file diff --git a/.github/workflows/on_call_deploy.yaml b/.github/workflows/on_call_deploy.yaml new file mode 100644 index 000000000..38230a71f --- /dev/null +++ b/.github/workflows/on_call_deploy.yaml @@ -0,0 +1,31 @@ +name: Deploy + +on: + workflow_call: + inputs: + ENVIRONMENT: + required: true + type: string + APP_NAME: + required: true + type: string + PUBLISH_PROFILE: + required: true + type: string + REPOSITORY: + required: true + type: string + +env: + TAG: ${{ github.sha }} + +jobs: + deploy: + runs-on: ubuntu-latest + environment: ${{ inputs.ENVIRONMENT }} + steps: + - uses: azure/webapps-deploy@v2 + with: + app-name: ${{ inputs.APP_NAME }} + publish-profile: ${{ inputs.PUBLISH_PROFILE }} + images: split.azurecr.io/${{ inputs.REPOSITORY }}:${{ env.TAG }} \ No newline at end of file diff --git a/.github/workflows/tests_backend_frontend.yml b/.github/workflows/tests_backend_frontend.yml index 419913fc0..17c9f95e2 100644 --- a/.github/workflows/tests_backend_frontend.yml +++ b/.github/workflows/tests_backend_frontend.yml @@ -10,6 +10,10 @@ on: env: NODE_VERSION: 18.x +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number }} + cancel-in-progress: true + jobs: install: name: Install Dependencies diff --git a/backend/docker/prod/Dockerfile b/backend/docker/prod/Dockerfile index 58c5ef249..c22c2239f 100644 --- a/backend/docker/prod/Dockerfile +++ b/backend/docker/prod/Dockerfile @@ -1,35 +1,22 @@ -FROM node:16-alpine +ARG TAG=18.12.1-alpine -ENV NODE_ENV build +FROM node:$TAG AS builder -WORKDIR /backend - -COPY package*.json ./ +WORKDIR /app +COPY . . RUN npm ci - -COPY ./ ./ - RUN npm run build - -ARG NODE_ENV -ENV NODE_ENV=${NODE_ENV} - -EXPOSE 3200 -ENV PORT 3200 - -CMD ["node", "dist/src/main.js"] +RUN npm prune --production # --- -# FROM node:16-alpine - +FROM node:$TAG AS production -# USER node -# WORKDIR /backend +WORKDIR /app -# COPY --from=builder ./package*.json /backend/ -# COPY --from=builder ./node_modules /backend/node_modules/ -# COPY --from=builder ./dist /backend/dist +COPY --from=builder /app/package*.json ./ +COPY --from=builder /app/dist ./dist +COPY --from=builder /app/node_modules ./node_modules -# CMD ["node", "dist/src/main.js"] \ No newline at end of file +CMD npm run start:prod \ No newline at end of file From 4bcd50dfd026d8f52e99c373a8d8a721453c995b Mon Sep 17 00:00:00 2001 From: Rui Silva Date: Tue, 22 Nov 2022 12:36:22 +0000 Subject: [PATCH 8/8] feat(ci): added update changelog workflow --- .github/workflows/on_release.yaml | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 .github/workflows/on_release.yaml diff --git a/.github/workflows/on_release.yaml b/.github/workflows/on_release.yaml new file mode 100644 index 000000000..3552d7ef2 --- /dev/null +++ b/.github/workflows/on_release.yaml @@ -0,0 +1,28 @@ +name: Update Changelog + +on: + release: + types: + - published + +jobs: + update: + name: Update Changelog + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Update Changelog + uses: stefanzweifel/changelog-updater-action@v1 + with: + release-notes: ${{ github.event.release.body }} + latest-version: ${{ github.event.release.name }} + + - name: Commit changes + uses: stefanzweifel/git-auto-commit-action@v4 + with: + branch: main + commit_message: 'ci: update version to ${{ github.event.release.name }} [skip ci]' + push_options: --force