Skip to content

Commit

Permalink
chore : add code deploy trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
PMtHk committed Feb 27, 2024
1 parent 062694b commit 3287ec9
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 47 deletions.
47 changes: 47 additions & 0 deletions .github/workflows/ci-cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

name: CI/CD (Blue-Green Deployment)

jobs:
ci-cd:
name: CI/CD
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read

steps:
- name: Checkout Repository
uses: actions/checkout@v4

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
aws-region: ${{ secrets.AWS_REGION }}

- name: Compress files
env:
IMAGE_TAG: ${{ github.sha }}
run: zip -r deploy-$IMAGE_TAG.zip ./appspec.yml ./scripts ./index.js ./package.json

- name: Upload S3 bucket
env:
IMAGE_TAG: ${{ github.sha }}
run: |
aws s3 cp --region ${{ secrets.AWS_REGION }} --acl private deploy-$IMAGE_TAG.zip |
s3://${{ secrets.AWS_S3_BUCKET_NAME }}/deploy-$IMAGE_TAG.zip
- name: Trigger CodeDeploy
env:
IMAGE_TAG: ${{ github.sha }}
run: |
aws deploy create-deployment --application-name ${{ secrets.AWS_CODEDEPLOY_APPLICATION_NAME }} |
--deployment-config-name CodeDeployDefault.AllAtOnce |
--deployment-group-name ${{ secrets.AWS_CODEDEPLOY_DEPLOYMENT_GROUP_NAME }} |
--s3-location bucket=${{ secrets.AWS_S3_BUCKET_NAME }},bundleType=zip,key=deploy-$IMAGE_TAG.zip
45 changes: 0 additions & 45 deletions .github/workflows/ci_on_ecr.yml

This file was deleted.

4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules

package-lock.json
package-lock.json

.DS_Store
20 changes: 20 additions & 0 deletions appspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
version: 0.0
os: linux

files:
- source: /
destination: /app
overwrite: yes

hooks:
BeforeInstall:
- location: scripts/before_install.sh
timeout: 300

AfterInstall:
- location: scripts/after_install.sh
timeout: 300

ApplicationStart:
- location: scripts/application_start.sh
timeout: 300
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const express = require("express");
const app = express();
const port = 3000;
const port = 8080;

app.get("/", (req, res) => {
res.send("Hello World!");
Expand Down
5 changes: 5 additions & 0 deletions scripts/after_install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

# move to the server directory and install the node modules
cd /app
sudo npm install
9 changes: 9 additions & 0 deletions scripts/application_start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

# Mapport 80 to 3000
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8080

# Stop all servers and start the server
forever start /app/index.js


15 changes: 15 additions & 0 deletions scripts/before_install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

# install node 20.x
sudo apt-get update
sudo apt-get install build-essential
sudo apt-get install curl -y
curl -sL https://deb.nodesource.com/setup_20.x | sudo -E bash --
sudo apt-get install nodejs -y

# Install npm
sudo apt install npm -y
sudo npm install npm@latest -g

# Install forever
sudo npm install forever -g

0 comments on commit 3287ec9

Please sign in to comment.