Skip to content

Commit 5770af4

Browse files
Merge pull request #101 from DeveloperParana/endersonmenezes/add-dockerfile
Add Dockerfile for server backend deployment to Azure
2 parents 3c207dc + aaca4ef commit 5770af4

File tree

12 files changed

+197
-39
lines changed

12 files changed

+197
-39
lines changed

.dockerignore

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Ignore node_modules directory
2+
node_modules
3+
4+
# Ignore build output directories
5+
dist
6+
tmp
7+
8+
# Ignore git directory
9+
.git
10+
11+
# Ignore environment files
12+
.env
13+
.env.prod
14+
15+
# Ignore IDE and editor directories
16+
.idea
17+
.vscode
18+
19+
# Ignore log files
20+
npm-debug.log
21+
yarn-error.log
22+
testem.log
23+
24+
# Ignore coverage directory
25+
coverage
26+
27+
# Ignore system files
28+
.DS_Store
29+
Thumbs.db
30+
31+
# Ignore nx cache
32+
.nx/cache
33+
.nx/workspace-data
34+
35+
# Ignore angular cache
36+
.angular

.env-example

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
PORT=3000
22

3+
# Only needed in production
4+
MONGO_URI=""
5+
36
DB_HOST=localhost
47
DB_PORT=27017
58
DB_NAME=devparana

.github/workflows/deploy.yml

+44-33
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
1-
# Docs for the Azure Web Apps Deploy action: https://github.com/Azure/webapps-deploy
2-
# More GitHub Actions for Azure: https://github.com/Azure/actions
3-
41
name: Build and deploy Node.js app to Azure Web App - devpr
52

63
on:
74
push:
85
branches:
96
- main
107
workflow_dispatch:
8+
inputs:
9+
deploy_front:
10+
description: 'Mark this checkbox to deploy and build the front-end'
11+
required: true
12+
default: false
13+
type: boolean
1114

1215
jobs:
13-
build:
16+
build-deploy-front:
17+
if: github.event_name != 'workflow_dispatch' || github.event.inputs.deploy_front == true
1418
runs-on: ubuntu-latest
1519
permissions:
1620
contents: write
17-
id-token: write # needed for provenance data generation
21+
id-token: write
1822
timeout-minutes: 10
1923
steps:
2024
- uses: actions/checkout@v4
@@ -27,7 +31,6 @@ jobs:
2731
version: 9.12.1
2832

2933
- name: Install Node
30-
# Cache node_modules
3134
uses: actions/setup-node@v4
3235
with:
3336
node-version: 20
@@ -38,45 +41,53 @@ jobs:
3841

3942
- uses: nrwl/nx-set-shas@v4
4043

41-
- run: pnpm exec nx run-many -t build --projects server,devmx --configuration=production --parallel 10
44+
- run: pnpm exec nx run-many -t build --projects devmx --configuration=production --parallel 10
4245

4346
- name: Deploy GitHub Pages
4447
uses: peaceiris/actions-gh-pages@v3
4548
with:
4649
github_token: ${{ secrets.GITHUB_TOKEN }}
4750
publish_dir: ./dist/apps/devmx/browser
4851

49-
- name: Zip artifact for deployment
50-
run: |
51-
cd dist/apps/server && zip -r release.zip ./*
52-
53-
- name: Upload artifact for deployment job
54-
uses: actions/upload-artifact@v3
52+
build-back:
53+
runs-on: ubuntu-latest
54+
permissions:
55+
contents: write
56+
id-token: write
57+
timeout-minutes: 10
58+
steps:
59+
- uses: actions/checkout@v4
60+
61+
- name: Set up Docker Buildx
62+
uses: docker/setup-buildx-action@v2
63+
64+
- name: Log in to registry
65+
uses: docker/login-action@v2
66+
with:
67+
registry: https://devmx.azurecr.io/
68+
username: ${{ secrets.AzureAppService_ContainerUsername_947982706eed46ce985090481ab47364 }}
69+
password: ${{ secrets.AzureAppService_ContainerPassword_86f3c94c1b5143be904bfc1de2add431 }}
70+
71+
- name: Build and push container image to registry
72+
uses: docker/build-push-action@v3
5573
with:
56-
name: node-app
57-
path: ./dist/apps/server/release.zip
74+
push: true
75+
tags: devmx.azurecr.io/${{ secrets.AzureAppService_ContainerUsername_947982706eed46ce985090481ab47364 }}/devmx-server:${{ github.sha }}
76+
file: ./Dockerfile.server
5877

5978
deploy:
6079
runs-on: ubuntu-latest
61-
needs: build
80+
needs: build-back
6281
environment:
63-
name: 'Production'
82+
name: 'production'
6483
url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
6584

6685
steps:
67-
- name: Download artifact from build job
68-
uses: actions/download-artifact@v3
69-
with:
70-
name: node-app
71-
72-
- name: Unzip artifact for deployment
73-
run: unzip release.zip
74-
75-
- name: 'Deploy to Azure Web App'
76-
id: deploy-to-webapp
77-
uses: azure/webapps-deploy@v2
78-
with:
79-
app-name: 'devpr'
80-
slot-name: 'Production'
81-
publish-profile: ${{ secrets.AZUREAPPSERVICE_PUBLISHPROFILE_AF340B23D99E4DE5BE0B99FA4D5BFB80 }}
82-
package: .
86+
- name: Deploy to Azure Web App
87+
id: deploy-to-webapp
88+
uses: azure/webapps-deploy@v2
89+
with:
90+
app-name: 'devpr'
91+
slot-name: 'production'
92+
publish-profile: ${{ secrets.AzureAppService_PublishProfile_36a287a711bf47e6a54130170298549c }}
93+
images: 'devmx.azurecr.io/${{ secrets.AzureAppService_ContainerUsername_947982706eed46ce985090481ab47364 }}/devmx-server:${{ github.sha }}'

Dockerfile.front

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Use node:22-alpine as the base image
2+
FROM node:22-alpine
3+
4+
# Set the working directory to /app
5+
WORKDIR /app
6+
7+
# Copy package.json and pnpm-lock.yaml to the working directory
8+
COPY package.json pnpm-lock.yaml ./
9+
10+
# Install dependencies using pnpm install --frozen-lockfile
11+
RUN npm install -g pnpm && pnpm install --frozen-lockfile
12+
13+
# Copy the rest of the application code to the working directory
14+
COPY . .
15+
16+
# Build the application using pnpm nx build devmx --verbose
17+
RUN pnpm nx build devmx --verbose
18+
19+
# Expose port 4200
20+
EXPOSE 4200
21+
22+
# Set the command to run the application using pnpm nx serve devmx
23+
CMD ["pnpm", "nx", "serve", "devmx"]

Dockerfile.server

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Backend Dockerfile
2+
3+
# Use node:22-alpine as the base image
4+
FROM node:22-alpine
5+
6+
# Arguments
7+
ENV PORT=3000
8+
9+
# Set the working directory to /app
10+
WORKDIR /app
11+
12+
# Copy package.json and pnpm-lock.yaml to the working directory
13+
COPY package.json pnpm-lock.yaml ./
14+
15+
# Install dependencies using pnpm install --frozen-lockfile
16+
RUN npm install -g pnpm && pnpm install --frozen-lockfile
17+
18+
# Copy the rest of the application code to the working directory
19+
COPY . .
20+
21+
# Build the application using pnpm nx build server --verbose
22+
RUN pnpm nx build server --verbose
23+
24+
# Expose port arg
25+
EXPOSE $PORT
26+
27+
# Set the command to run the application using node dist/apps/server/main.js
28+
CMD ["node", "dist/apps/server/main.js"]

apps/server/src/envs/env.dev.ts

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ export const env = {
99
user: process.env.DB_USER,
1010
pass: process.env.DB_PASS,
1111
},
12+
mongo: {
13+
uri: process.env.MONGO_URI ?? '',
14+
},
1215
jwt: {
1316
secret: process.env.JWT_SECRET,
1417
},

apps/server/src/envs/env.prod.ts

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { join } from 'node:path';
22

3+
if (!process.env.MONGO_URI) {
4+
throw new Error('MONGO_URI environment variable is not set');
5+
}
6+
37
export const env = {
48
production: true,
59
db: {
@@ -9,6 +13,9 @@ export const env = {
913
user: process.env.DB_USER,
1014
pass: process.env.DB_PASS,
1115
},
16+
mongo: {
17+
uri: process.env.MONGO_URI,
18+
},
1219
jwt: {
1320
secret: process.env.JWT_SECRET,
1421
},

docker-compose.yml

+38-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
version: '3'
2-
31
services:
42
mongodb:
53
image: mongo:latest
@@ -9,3 +7,41 @@ services:
97
MONGO_INITDB_DATABASE: ${DB_NAME}
108
ports:
119
- 27017:27017
10+
11+
server:
12+
build:
13+
context: .
14+
dockerfile: Dockerfile.server
15+
args:
16+
- PORT=${PORT}
17+
environment:
18+
PORT: ${PORT}
19+
DB_HOST: mongodb
20+
DB_PORT: ${DB_PORT}
21+
DB_NAME: ${DB_NAME}
22+
DB_USER: ${DB_USER}
23+
DB_PASS: ${DB_PASS}
24+
JWT_SECRET: ${JWT_SECRET}
25+
SMTP_HOST: ${SMTP_HOST}
26+
SMTP_PORT: ${SMTP_PORT}
27+
SMTP_USER: ${SMTP_USER}
28+
SMTP_PASS: ${SMTP_PASS}
29+
CODE_LIFE_TIME: ${CODE_LIFE_TIME}
30+
MONGO_URI: ${MONGO_URI}
31+
NODE_ENV: ${NODE_ENV}
32+
ports:
33+
- 8080:${PORT}
34+
depends_on:
35+
- mongodb
36+
37+
devmx:
38+
build:
39+
context: .
40+
dockerfile: Dockerfile.front
41+
environment:
42+
PORT: ${PORT}
43+
ports:
44+
- 4200:4200
45+
depends_on:
46+
- server
47+
- mongodb

nx.json

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
],
1515
"sharedGlobals": ["{workspaceRoot}/.github/workflows/ci.yml"]
1616
},
17-
"nxCloudId": "66ed082b0f768c3e7a77e1a5",
1817
"plugins": [
1918
{
2019
"plugin": "@nx/webpack/plugin",

package.json

+8-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@
1111
"lint:fix": "pnpm exec nx run-many -t lint --fix",
1212
"dev:setup": "./tools/scripts/setup.sh",
1313
"commit": "czg",
14-
"prepare": "husky"
14+
"prepare": "husky",
15+
"docker:build:server": "docker build -t devmx-server -f Dockerfile.server .",
16+
"docker:build:front": "docker build -t devmx-front -f Dockerfile.front .",
17+
"docker:compose": "docker compose up --build",
18+
"docker:compose:only:mongo": "docker compose up -d mongo",
19+
"docker:compose:only:server": "docker compose up -d server",
20+
"docker:run:server": "docker run -p 3000:3000 devmx-server",
21+
"docker:run:front": "docker run -p 4200:4200 devmx-front"
1522
},
1623
"private": true,
1724
"devDependencies": {

packages/shared/api-interfaces/src/server/envs/env.ts

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ export abstract class Env {
99
pass: string;
1010
};
1111

12+
abstract mongo: {
13+
uri: string;
14+
}
15+
1216
abstract jwt: {
1317
secret: string;
1418
};

packages/shared/data-source/src/lib/providers.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ export function provideMongoURI() {
88
return {
99
provide: 'MONGO_URI',
1010
useFactory(env: Env) {
11-
const { user, pass, host, port, name } = env.db;
11+
const { user, pass, host, port, name, } = env.db;
12+
const { uri } = env.mongo;
1213

1314
if (env.production) {
14-
return `mongodb+srv://${user}:${pass}@${host}/?retryWrites=true&w=majority&appName=${name}`;
15+
return `${uri}`;
1516
}
1617

1718
return `mongodb://${user}:${pass}@${host}:${port}/${name}?authSource=admin`;

0 commit comments

Comments
 (0)