Node.js CICD with Docker and EC2 #78
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |