From f8fe8542fea6e9debb51cabf18237e1a066671ec Mon Sep 17 00:00:00 2001 From: devout-coder Date: Sat, 7 Sep 2024 17:41:05 +0530 Subject: [PATCH] testing stuff --- .github/workflows/deploy.yml | 63 ++++++++++++++++++++++++++++++++++++ .gitignore | 3 +- Dockerfile | 4 ++- docker-compose.yml | 40 +++-------------------- src/index.ts | 15 +++------ 5 files changed, 76 insertions(+), 49 deletions(-) create mode 100644 .github/workflows/deploy.yml diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..7870aee --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,63 @@ +name: CI/CD Pipeline + +on: + push: + branches: + - main + +jobs: + build: + runs-on: ubuntu-latest + + steps: + # Checkout the code + - name: Checkout Code + uses: actions/checkout@v2 + + # Set up AWS CLI + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v2 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: ${{ secrets.AWS_REGION }} + + # Log in to Amazon ECR + - name: Login to Amazon ECR + id: ecr_login + run: | + aws ecr get-login-password --region ${{ secrets.AWS_REGION }} | docker login --username AWS --password-stdin ${{ secrets.ECR_REPOSITORY }} + + # Build the Docker image + - name: Build Docker image + run: | + docker build -t doneify . + docker tag doneify:latest ${{ secrets.ECR_REPOSITORY }}:latest + + # Push the Docker image to Amazon ECR + - name: Push Docker image to ECR + run: | + docker push ${{ secrets.ECR_REPOSITORY }}:latest + + deploy: + runs-on: ubuntu-latest + needs: build + + steps: + # SSH into EC2 and use docker-compose + - name: Deploy to EC2 using Docker Compose + run: | + ssh -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }} << 'EOF' + cd /home/ubuntu/doneify_dir + docker-compose pull doneify + docker-compose down + docker-compose up -d + EOF + env: + EC2_KEY: ${{ secrets.EC2_KEY }} + + # Adding SSH key + - name: Add SSH Key + run: | + echo "${{ secrets.EC2_KEY }}" | tr -d '\r' > ~/.ssh/id_rsa + chmod 600 ~/.ssh/id_rsa diff --git a/.gitignore b/.gitignore index d1e1935..fed344b 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ data/ .vscode/ yarn-error.log yarn.lock -package-lock.json \ No newline at end of file +package-lock.json +Session.vim diff --git a/Dockerfile b/Dockerfile index cd7c1a1..d55861f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,8 +3,10 @@ ENV NODE_ENV=production WORKDIR /usr/src/app COPY ["package.json", "package-lock.json*", "npm-shrinkwrap.json*", "./"] RUN npm install --production --silent && mv node_modules ../ +RUN npm install -g ts-node-dev COPY . . -EXPOSE 3000 +EXPOSE 8000 RUN chown -R node /usr/src/app USER node CMD ["npm", "start"] + diff --git a/docker-compose.yml b/docker-compose.yml index 056c096..8f35cca 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -5,43 +5,11 @@ services: build: context: . dockerfile: Dockerfile + image: 401271784573.dkr.ecr.us-east-1.amazonaws.com/doneify:latest # Change to your ECR repo container_name: doneify_cunt volumes: - ./app:/app ports: - - 3000:3000 - environment: - - PORT=${PORT} - - JWT_SECRET=${JWT_SECRET} - - MONGO_ROOT_USER=${MONGO_ROOT_USER} - - MONGO_ROOT_PASSWORD=${MONGO_ROOT_PASSWORD} - - SERVER_URL=${SERVER_URL} - depends_on: - - mongo - - mongo: - image: mongo - container_name: mongo_db - environment: - - MONGO_INITDB_ROOT_USERNAME=${MONGO_ROOT_USER} - - MONGO_INITDB_ROOT_PASSWORD=${MONGO_ROOT_PASSWORD} - - MONGO_INITDB_DATABASE=project - ports: - - 27017:27017 - - mongo-express: - image: mongo-express - container_name: mongo_web_gui - environment: - - ME_CONFIG_MONGODB_SERVER=mongo - - ME_CONFIG_MONGODB_PORT=27017 - - ME_CONFIG_MONGODB_ENABLE_ADMIN=false - - ME_CONFIG_MONGODB_AUTH_DATABASE=admin - - ME_CONFIG_MONGODB_AUTH_USERNAME=${MONGO_ROOT_USER} - - ME_CONFIG_MONGODB_AUTH_PASSWORD=${MONGO_ROOT_PASSWORD} - - ME_CONFIG_BASICAUTH_USERNAME=${MONGOEXPRESS_LOGIN} - - ME_CONFIG_BASICAUTH_PASSWORD=${MONGOEXPRESS_PASSWORD} - depends_on: - - mongo - ports: - - "8888:8081" + - 8000:8000 + env_file: + - ./.env diff --git a/src/index.ts b/src/index.ts index c86a031..91d8718 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,14 +14,9 @@ import labelController from "./controllers/label.controller"; const app: Express = express(); app.use(express.json()); app.use(cors()); +dotenv.config({}); const PORT = process.env.PORT || 8000; -if (process.env.DEBUG) { - process.on("unhandledRejection", function (reason) { - process.exit(1); - }); -} else { -} app.get("/", (req: Request, res: Response) => { res.send("Fuck this world"); @@ -32,19 +27,17 @@ todoRoutes(app); const server: http.Server = http.createServer(app); export const io = new Server(server); - const mongooseOptions = { - // // serverSelectionTimeoutMS: 5000, + // serverSelectionTimeoutMS: 5000, // directConnection: true, // useNewUrlParser: true, // useUnifiedTopology: true, - user: process.env.MONGO_ROOT_USER, // MongoDB username - pass: process.env.MONGO_ROOT_PASSWORD, // MongoDB password }; +const MONGODB_URI = process.env.MONGODB_URI; mongoose .set("strictQuery", true) - .connect(process.env.SERVER_URL ?? "", mongooseOptions) + .connect(MONGODB_URI! ?? "", mongooseOptions) .then(() => { server.listen(PORT, () => { console.log(`Server is running on ${PORT}`);