Skip to content

Node.js CICD with Docker and EC2 #78

Node.js CICD with Docker and EC2

Node.js CICD with Docker and EC2 #78

Workflow file for this run

name: Node.js CICD with Docker and EC2
on:
pull_request:
types: [closed]
workflow_dispatch: # ์ˆ˜๋™ ์‹คํ–‰ ๊ฐ€๋Šฅํ•˜๋„๋ก
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v3
with:
node-version: 16
# # .env ํŒŒ์ผ ์ƒ์„ฑ
# - name: Create .env file
# run: |
# touch .env.production
# echo "PORT=3000" >> .env.production
# echo "JWT_SECRET=${{ secrets.JWT_SECRET }}" >> .env.production
# echo "SOCKETIO_URL=https://socket.gamegoo.co.kr:3000/socket.io/socket.io.js" >> .env.production
# echo "API_SERVER_URL=https://api.gamegoo.co.kr" >> .env.production
# echo "NODE_SERVER_URL=https://socket.gamegoo.co.kr" >> .env.production
# cat .env.production
# - name: Verify .env.production file
# run: cat .env.production
- name: Install dependencies
run: npm install
docker:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
# Docker ์ด๋ฏธ์ง€ ๋นŒ๋“œ
- name: Build Docker Image
run: docker build -t rimi3226/gamegoo-socket:${{ github.sha }} .
# DockerHub ๋กœ๊ทธ์ธ
- name: DockerHub Login
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PW }}
# DockerHub์— ์ด๋ฏธ์ง€ ํ‘ธ์‹œ
- name: Push Docker Image
run: docker push rimi3226/gamegoo-socket:${{ github.sha }}
deploy-to-ec2:
needs: docker
runs-on: ubuntu-latest
steps:
# GitHub IP ๊ฐ€์ ธ์˜ค๊ธฐ
- name: Get GitHub IP
id: ip
run: echo "ipv4=$(curl -s https://api.ipify.org)" >> $GITHUB_ENV
# AWS ์ž๊ฒฉ์ฆ๋ช… ๊ตฌ์„ฑ - AWS ์ ‘๊ทผ ๊ถŒํ•œ ์ทจ๋“(IAM)
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ap-northeast-2
# AWS์— GitHub IP ์ถ”๊ฐ€
- name: Add GitHub IP to AWS
run: |
aws ec2 authorize-security-group-ingress --group-id ${{ secrets.AWS_SG_ID }} --protocol tcp --port 22 --cidr ${{ env.ipv4 }}/32
# # .env.production ํŒŒ์ผ ๋ณต์‚ฌ
# - name: Copy .env.production file to EC2
# uses: appleboy/scp-action@master
# with:
# host: ${{ secrets.EC2_HOST }}
# username: ${{ secrets.EC2_USERNAME }}
# key: ${{ secrets.EC2_PEM_KEY }}
# port: ${{ secrets.EC2_SSH_PORT }}
# source: ".env.production"
# target: "./home/ec2-user/.env.production"
# AWS EC2 ์„œ๋ฒ„ ์—ฐ๊ฒฐ ๋ฐ Docker ๋ช…๋ น์–ด ์‹คํ–‰
- name: Deploy to EC2
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_USERNAME }}
key: ${{ secrets.EC2_PEM_KEY }}
port: ${{ secrets.EC2_SSH_PORT }}
script: |
echo "Pulling new Docker image..."
sudo docker pull rimi3226/gamegoo-socket:${{ github.sha }}
echo "Stopping existing Docker container..."
sudo docker stop gamegoo_socket || true
echo "Removing existing Docker container..."
sudo docker rm gamegoo_socket || true
echo "Running new Docker container..."
sudo docker run -d -p 3000:3000 \
-e PORT=3000 \
-e JWT_SECRET=${{ secrets.JWT_SECRET }} \
-e SOCKETIO_URL=https://socket.gamegoo.co.kr:3000/socket.io/socket.io.js \
-e API_SERVER_URL=https://api.gamegoo.co.kr \
-e NODE_SERVER_URL=https://socket.gamegoo.co.kr \
--name gamegoo_socket rimi3226/gamegoo-socket:${{ github.sha }}
# AWS ๋ณด์•ˆ ๊ทธ๋ฃน์—์„œ GitHub IP ์ œ๊ฑฐ
- name: Remove IP from Security Group
run: |
aws ec2 revoke-security-group-ingress --group-id ${{ secrets.AWS_SG_ID }} --protocol tcp --port 22 --cidr ${{ env.ipv4 }}/32