[Fix/108] 3000->8080μλ² μμ² μ internal API μ¬μ©νλλ‘ λ³κ²½ #77
Workflow file for this run
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 |