Skip to content

Commit bfec3c6

Browse files
committed
Update Both Dockerfile and Pipeline yaml for the railway prod #still-working-locally
1 parent 48ea491 commit bfec3c6

4 files changed

Lines changed: 202 additions & 63 deletions

File tree

.github/workflows/pipeline.yml

Lines changed: 142 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,174 @@
1+
# name: CI/CD pipeline for the eventify API
2+
3+
# on:
4+
# push:
5+
# branches:
6+
# - master
7+
# pull_request:
8+
# branches:
9+
# - master
10+
11+
# jobs:
12+
# build-and-test:
13+
# runs-on: ubuntu-latest
14+
15+
# steps:
16+
# # Checkout code from the repository
17+
# - name: Checkout code
18+
# uses: actions/checkout@v3
19+
20+
# # Set up Node.js environment
21+
# - name: Set up Node.js
22+
# uses: actions/setup-node@v3
23+
# with:
24+
# node-version: '20'
25+
26+
# # Install dependencies
27+
# - name: Install dependencies
28+
# run: npm install
29+
30+
# # Create .env file
31+
# - name: Create .env file
32+
# run: |
33+
# echo ${{ secrets.PORT }} > .env
34+
# echo ${{ secrets.MONGO_ATLAS_URI }} >> .env
35+
# echo ${{ secrets.MAIL_HOST }} >> .env
36+
# echo ${{ secrets.MAIL_PORT }} >> .env
37+
# echo ${{ secrets.MAIL_USER }} >> .env
38+
# echo ${{ secrets.MAIL_PASSWORD }} >> .env
39+
# echo ${{ secrets.JWT_SECRET }} >> .env
40+
# echo ${{ secrets.JWT_REFRESH_SECRET }} >> .env
41+
# echo ${{ secrets.JWT_EXPIRES_IN }} >> .env
42+
# echo ${{ secrets.JWT_REFRESH_TOKEN_EXPIRES_IN }} >> .env
43+
# echo ${{ secrets.JWT_ACCESS_EXPIRES_IN }} >> .env
44+
45+
# # Run tests
46+
# - name: Run unit tests
47+
# run: npm test
48+
49+
# dockerize:
50+
# runs-on: ubuntu-latest
51+
# needs: build-and-test
52+
53+
# steps:
54+
# # Checkout code from the repository
55+
# - name: Checkout code
56+
# uses: actions/checkout@v3
57+
58+
# # Set up Docker Buildx
59+
# - name: Set up Docker Buildx
60+
# uses: docker/setup-buildx-action@v2
61+
62+
# # Log in to Docker Hub
63+
# - name: Log in to Docker Hub
64+
# uses: docker/login-action@v2
65+
# with:
66+
# username: ${{ secrets.DOCKER_USERNAME }}
67+
# password: ${{ secrets.DOCKER_PASSWORD }}
68+
69+
# # Build And Push Docker image
70+
# - name: Build and push Docker image
71+
# uses: docker/build-push-action@v4
72+
# with:
73+
# context: .
74+
# push: true
75+
# tags: ${{ secrets.DOCKER_USERNAME }}/eventify-api:latest
76+
77+
# # Log out from Docker Hub
78+
# - name: Log out from Docker Hub
79+
# run: docker logout
80+
181
name: CI/CD pipeline for the eventify API
282

383
on:
484
push:
5-
branches:
6-
- master
85+
branches: [ master ]
786
pull_request:
8-
branches:
9-
- master
87+
branches: [ master ]
1088

1189
jobs:
1290
build-and-test:
1391
runs-on: ubuntu-latest
14-
92+
env:
93+
NODE_ENV: test
94+
NODE_OPTIONS: --max-old-space-size=256
1595
steps:
16-
# Checkout code from the repository
17-
- name: Checkout code
18-
uses: actions/checkout@v3
96+
- name: Checkout
97+
uses: actions/checkout@v4
1998

20-
# Set up Node.js environment
21-
- name: Set up Node.js
22-
uses: actions/setup-node@v3
99+
- name: Use Node.js 20.x
100+
uses: actions/setup-node@v4
23101
with:
24102
node-version: '20'
103+
cache: 'npm'
104+
105+
- name: Install deps (CI)
106+
run: npm ci
107+
108+
# If your unit tests need env vars, write them as KEY=VALUE lines.
109+
# (What you had before wrote only values, which doesn't work.)
110+
- name: Create .env for tests
111+
run: |
112+
{
113+
echo "PORT=${{ secrets.PORT }}"
114+
echo "MONGO_URI=${{ secrets.MONGO_ATLAS_URI }}"
115+
echo "MAIL_HOST=${{ secrets.MAIL_HOST }}"
116+
echo "MAIL_PORT=${{ secrets.MAIL_PORT }}"
117+
echo "MAIL_USER=${{ secrets.MAIL_USER }}"
118+
echo "MAIL_PASSWORD=${{ secrets.MAIL_PASSWORD }}"
119+
echo "JWT_SECRET=${{ secrets.JWT_SECRET }}"
120+
echo "JWT_REFRESH_SECRET=${{ secrets.JWT_REFRESH_SECRET }}"
121+
echo "JWT_EXPIRES_IN=${{ secrets.JWT_EXPIRES_IN }}"
122+
echo "JWT_REFRESH_TOKEN_EXPIRES_IN=${{ secrets.JWT_REFRESH_TOKEN_EXPIRES_IN }}"
123+
echo "JWT_ACCESS_EXPIRES_IN=${{ secrets.JWT_ACCESS_EXPIRES_IN }}"
124+
} > .env
25125
26-
# Install dependencies
27-
- name: Install dependencies
28-
run: npm install
29-
30-
# Create .env file
31-
- name: Create .env file
32-
run: |
33-
echo ${{ secrets.PORT }} > .env
34-
echo ${{ secrets.MONGO_ATLAS_URI }} >> .env
35-
echo ${{ secrets.MAIL_HOST }} >> .env
36-
echo ${{ secrets.MAIL_PORT }} >> .env
37-
echo ${{ secrets.MAIL_USER }} >> .env
38-
echo ${{ secrets.MAIL_PASSWORD }} >> .env
39-
echo ${{ secrets.JWT_SECRET }} >> .env
40-
echo ${{ secrets.JWT_REFRESH_SECRET }} >> .env
41-
echo ${{ secrets.JWT_EXPIRES_IN }} >> .env
42-
echo ${{ secrets.JWT_REFRESH_TOKEN_EXPIRES_IN }} >> .env
43-
echo ${{ secrets.JWT_ACCESS_EXPIRES_IN }} >> .env
44-
45-
# Run tests
46-
- name: Run unit tests
47-
run: npm test
126+
- name: TypeScript build (ensure dist exists)
127+
run: npm run build
128+
129+
- name: Run unit tests (reduced workers to avoid OOM)
130+
run: npm test -- --runInBand --maxWorkers=50%
48131

49132
dockerize:
50133
runs-on: ubuntu-latest
51134
needs: build-and-test
52-
135+
permissions:
136+
contents: read
137+
packages: write
53138
steps:
54-
# Checkout code from the repository
55-
- name: Checkout code
56-
uses: actions/checkout@v3
139+
- name: Checkout
140+
uses: actions/checkout@v4
57141

58-
# Set up Docker Buildx
59142
- name: Set up Docker Buildx
60-
uses: docker/setup-buildx-action@v2
143+
uses: docker/setup-buildx-action@v3
61144

62-
# Log in to Docker Hub
63145
- name: Log in to Docker Hub
64-
uses: docker/login-action@v2
146+
uses: docker/login-action@v3
65147
with:
66148
username: ${{ secrets.DOCKER_USERNAME }}
67149
password: ${{ secrets.DOCKER_PASSWORD }}
68150

69-
# Build And Push Docker image
70-
- name: Build and push Docker image
71-
uses: docker/build-push-action@v4
151+
# Optional: Add metadata (labels, semver tags, sha)
152+
- name: Docker meta
153+
id: meta
154+
uses: docker/metadata-action@v5
155+
with:
156+
images: ${{ secrets.DOCKER_USERNAME }}/eventify-api
157+
tags: |
158+
type=raw,value=latest
159+
type=sha
160+
161+
- name: Build and push (prod stage)
162+
uses: docker/build-push-action@v6
72163
with:
73164
context: .
74165
push: true
75-
tags: ${{ secrets.DOCKER_USERNAME }}/eventify-api:latest
166+
target: prod # <- uses the prod stage from the Dockerfile I provided
167+
platforms: linux/amd64
168+
tags: ${{ steps.meta.outputs.tags }}
169+
labels: ${{ steps.meta.outputs.labels }}
170+
cache-from: type=gha
171+
cache-to: type=gha,mode=max
76172

77-
# Log out from Docker Hub
78-
- name: Log out from Docker Hub
173+
- name: Logout Docker Hub
79174
run: docker logout
80-

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ cp .env.example .env
7171
## 🧩 Installation Options
7272

7373
### 🐳 Using Docker
74-
Rename the dockerfile:
75-
``` Rename the dockerfile by removing the '.local'```
7674
Build and run the image:
7775
```bash
7876
docker build -t eventify-api .

dockerfile

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# FROM node:20.7.0
2+
3+
# WORKDIR /usr/src/app
4+
5+
# COPY package*.json ./
6+
7+
# RUN npm install
8+
9+
# COPY . .
10+
11+
# EXPOSE 3000
12+
13+
# CMD [ "npm", "run", "start" ]
14+
15+
# -------------------------------------------------------
16+
# Base image (pin to the current version)
17+
# -------------------------------------------------------
18+
FROM node:20.7.0 AS base
19+
WORKDIR /usr/src/app
20+
21+
# -------------------------------------------------------
22+
# Dev stage (keeps the old behavior, uses ts-node/nest)
23+
# -------------------------------------------------------
24+
FROM base AS dev
25+
COPY package*.json ./
26+
RUN npm ci
27+
COPY . .
28+
EXPOSE 3000
29+
CMD ["npm","run","start:dev"]
30+
31+
# -------------------------------------------------------
32+
# Build stage (compile to dist/, then prune dev deps)
33+
# -------------------------------------------------------
34+
FROM base AS build
35+
COPY package*.json ./
36+
# Keep devDependencies for the build (nest/tsc)
37+
RUN npm ci
38+
COPY . .
39+
RUN npm run build \
40+
&& npm prune --omit=dev
41+
42+
# -------------------------------------------------------
43+
# Production runtime (small, stable, Railway-friendly)
44+
# -------------------------------------------------------
45+
FROM node:20.7.0 AS prod
46+
WORKDIR /usr/src/app
47+
ENV NODE_ENV=production
48+
# Cap heap to avoid OOM on free tiers
49+
ENV NODE_OPTIONS="--max-old-space-size=256"
50+
51+
# Install only runtime deps
52+
COPY package*.json ./
53+
RUN npm ci --omit=dev
54+
55+
# Bring compiled app
56+
COPY --from=build /usr/src/app/dist ./dist
57+
58+
EXPOSE 3000
59+
# Same path Railway tried to run; now it exists.
60+
CMD ["node","dist/main.js"]

dockerfile.local

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)