Skip to content

Feat: docker 실행 sudo 스크립트 추가 (#43) #33

Feat: docker 실행 sudo 스크립트 추가 (#43)

Feat: docker 실행 sudo 스크립트 추가 (#43) #33

name: dev-deploy
on:
push:
branches:
- develop
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v2
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ap-northeast-2
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build and push Docker image
run: |
docker build -t meta-test .
docker tag meta-test:latest ${{ secrets.DOCKER_USERNAME }}/meta-test:latest
docker push ${{ secrets.DOCKER_USERNAME }}/meta-test:latest
- name: SSH and deploy to EC2
run: |
echo "$SSH_KEY" > key.pem
chmod 600 key.pem
ssh -i key.pem -o StrictHostKeyChecking=no ${USER}@${HOST} << 'EOF'
# Docker가 설치되어 있는지 확인
if ! command -v docker &> /dev/null
then
echo "Docker가 설치되어 있지 않습니다. Docker를 설치합니다."
# 기존 패키지 목록 업데이트 및 필수 패키지 설치
sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common -y
# Docker 공식 GPG 키 추가
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
# Docker 리포지토리 추가
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# 패키지 목록 업데이트 및 Docker 설치
sudo apt update
sudo apt install docker-ce -y
# Docker 권한 설정 (옵션)
sudo usermod -aG docker ${USER}
# Docker 데몬 시작 및 부팅 시 자동 시작 설정
sudo systemctl enable docker
sudo systemctl start docker
else
echo "Docker가 이미 설치되어 있습니다."
fi
sudo docker pull ${{ secrets.DOCKER_USERNAME }}/meta-test:latest
sudo docker stop meta-test-container || true
sudo docker rm meta-test-container || true
sudo docker run -d --name meta-test-container \
-e NODE_ENV=production \
-e JWT_SECRET=${{ secrets.JWT_SECRET }} \
-e ACCESS_TOKEN_EXPIRATION=1h \
-e REFRESH_TOKEN_EXPIRATION=3y \
-e AWS_ACCESS_KEY_ID=${{ secrets.AWS_ACCESS_KEY }} \
-e AWS_SECRET_ACCESS_KEY=${{ secrets.AWS_SECRET_ACCESS_KEY }} \
-e REGION=ap-northeast-2 \
-e AWS_DYNAMODB_TABLE_NAME=Dev-Metacognition-Test \
-e AWS_DYNAMODB_EMAIL_VERIFICATION_TABLE_NAME=Dev-Metacognition-Email-Verification \
-e [email protected] \
-e KAKAO_CLIENT_ID=${{ secrets.KAKAO_CLIENT_ID }} \
-e KAKAO_CLIENT_SECRET=${{ secrets.KAKAO_CLIENT_SECRET }} \
-e KAKAO_CALLBACK_URL=https://www.meta-cognition.site/auth \
-e GOOGLE_CLIENT_ID=${{ secrets.GOOGLE_CLIENT_ID }} \
-e GOOGLE_CLIENT_SECRET=${{ secrets.GOOGLE_CLIENT_SECRET }} \
-e GOOGLE_CALLBACK_URL=https://www.meta-cognition.site/auth \
-e GOOGLE_API_KEY=${{ secrets.GOOGLE_API_KEY }} \
-p 8080:8080 \
${{ secrets.DOCKER_USERNAME }}/meta-test:latest
sudo docker logs meta-test-container
EOF
env:
USER: ${{ secrets.EC2_USER }}
HOST: ${{ secrets.EC2_HOST }}
SSH_KEY: ${{ secrets.EC2_SSH_KEY }}