Skip to content

Commit 04d7ded

Browse files
committed
Changes for deployment
1 parent c77d629 commit 04d7ded

File tree

4 files changed

+39
-64
lines changed

4 files changed

+39
-64
lines changed

.github/workflows/pipeline.yml

+24-61
Original file line numberDiff line numberDiff line change
@@ -67,67 +67,33 @@ jobs:
6767
start: npm run server:test, npm start
6868
wait-on: 'http://localhost:3001/health, http://localhost:3000'
6969
working-directory: ./frontend
70-
71-
build:
72-
runs-on: ubuntu-20.04
73-
defaults:
74-
run:
75-
working-directory: backend
76-
needs: [test_lint_backend, test_lint_frontend]
77-
steps:
78-
- uses: actions/checkout@v3
79-
- uses: actions/setup-node@v2
80-
with:
81-
node-version: '16'
82-
- name: npm install
83-
run: npm install
84-
- name: install frontend dependencies
85-
run: cd ../frontend && npm install
86-
- name: build ui
87-
run: npm run build:ui
88-
89-
build-and-push-docker:
90-
runs-on: ubuntu-20.04
91-
needs: [build]
92-
steps:
93-
-
94-
name: Checkout
95-
uses: actions/checkout@v4
96-
-
97-
name: Set up QEMU
98-
uses: docker/setup-qemu-action@v3
99-
-
100-
name: Set up Docker Buildx
101-
uses: docker/setup-buildx-action@v3
102-
-
103-
name: Login to Docker Hub
104-
uses: docker/login-action@v3
105-
with:
106-
username: ${{ secrets.DOCKER_USERNAME }}
107-
password: ${{ secrets.DOCKER_PASSWORD }}
108-
-
109-
name: Build and push
110-
uses: docker/build-push-action@v5
111-
with:
112-
context: .
113-
push: true
114-
tags: user/instaclone:latest
115-
11670
deploy:
11771
if: ${{ github.ref == 'refs/head/main' }}
72+
needs: [test_lint_backend, test_lint_frontend]
11873
runs-on: ubuntu-20.04
119-
needs: [build-and-push-docker]
12074
steps:
121-
- uses: actions/checkout@v2
122-
- uses: akhileshns/[email protected]
123-
with:
124-
heroku_api_key: ${{secrets.HEROKU_API_KEY}}
125-
heroku_app_name: "instaclone-sc"
126-
heroku_email: "[email protected]"
127-
appdir: "backend"
128-
healthcheck: ""
129-
checkstring: "ok"
130-
rollbackonhealthcheckfailed: true
75+
- uses: actions/checkout@v2
76+
- name: Login to Heroku Container registry
77+
env:
78+
HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}
79+
run: heroku container:login
80+
- name: Set Heroku Config Vars
81+
env:
82+
HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}
83+
run: |
84+
heroku config:set JWT_SECRET=${{ secrets.JWT_SECRET }} -a instaclone
85+
heroku config:set CLOUDINARY_API_SECRET=${{ secrets.CLOUDINARY_API_SECRET }} -a instaclone
86+
heroku config:set CLOUDINARY_API_KEY=${{ secrets.CLOUDINARY_API_KEY }} -a instaclone
87+
heroku config:set CLOUDINARY_NAME=${{ secrets.CLOUDINARY_NAME }} -a instaclone
88+
heroku config:set PROD_MONGODB_URI=${{ secrets.PROD_MONGODB_URI }} -a instaclone
89+
- name: Build and push
90+
env:
91+
HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}
92+
run: heroku container:push -a instaclone web
93+
- name: Release
94+
env:
95+
HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}
96+
run: heroku container:release -a instaclone web
13197
tag_release:
13298
if: ${{ github.ref == 'refs/head/main' }}
13399
needs: [deploy]
@@ -139,7 +105,4 @@ jobs:
139105
- name: Bump version and push tag
140106
uses: anothrNick/[email protected]
141107
env:
142-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
143-
144-
# for deploying, make sure to have all final dependencies installed before deploying at all. aka, don't deploy, realize you need a new dep and then deploy again
145-
# todo: research how i can add new packages after deployment and then redeploy
108+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

backend/src/app.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,16 @@ import { errorHandler, logErrorCodes } from './utils/middleware';
1616
const { NODE_ENV } = process.env;
1717
const app = express();
1818

19-
if (NODE_ENV === 'development') {
20-
mongodbConnect(config.DEV_MONGODB_URI!);
19+
if (NODE_ENV === 'production' || NODE_ENV === 'development') {
20+
const mongodbUri = NODE_ENV === 'production'
21+
? config.PROD_MONGODB_URI
22+
: config.DEV_MONGODB_URI;
23+
24+
if (!mongodbUri) {
25+
throw new Error(`The ${NODE_ENV} MongoDB URI is not defined.`);
26+
}
27+
28+
mongodbConnect(mongodbUri);
2129
} else if (NODE_ENV === 'cypress') {
2230
(async () => {
2331
await testMongodb.connect();

backend/src/mongo/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import mongoose from 'mongoose';
22
import logger from '../utils/logger';
33

4+
const { NODE_ENV } = process.env;
5+
46
const connect = async (uri: string) => {
57
try {
68
await mongoose.connect(uri);
79
logger.info('Connected to: ', uri);
810
} catch (error) {
911
const message = logger.getErrorMessage(error);
10-
logger.error('Error connecting to dev MongoDB: ', message);
12+
logger.error(`Error connecting to ${NODE_ENV} MongoDB: `, message);
1113
}
1214
};
1315

backend/src/utils/config.ts

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ require('dotenv').config();
22

33
const {
44
DEV_MONGODB_URI,
5+
PROD_MONGODB_URI,
56
PORT,
67
CLOUDINARY_NAME,
78
CLOUDINARY_API_KEY,
@@ -16,4 +17,5 @@ export default {
1617
CLOUDINARY_API_KEY,
1718
CLOUDINARY_API_SECRET,
1819
JWT_SECRET,
20+
PROD_MONGODB_URI,
1921
};

0 commit comments

Comments
 (0)