Test : 테스트 중 #104
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 CI/CD | |
on: | |
push: | |
branches: [ "main" ] | |
pull_request: | |
branches: [ "main" ] | |
types: [ closed ] | |
permissions: | |
contents: read | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v3 | |
- name: Set up Node.js | |
uses: actions/setup-node@v3 | |
with: | |
node-version: '20' # 필요한 Node.js 버전 설정 | |
- name: Install dependencies | |
run: npm install | |
# - name: Build project | |
# run: npm run build # 프로젝트 빌드 명령 추가 | |
docker-build: | |
runs-on: ubuntu-latest | |
needs: build | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v3 | |
- name: Set up Node.js | |
uses: actions/setup-node@v3 | |
with: | |
node-version: '20' | |
- name: Build Docker image | |
run: | | |
docker login -u ${{ secrets.DOCKER_HUB_USERNAME }} -p ${{ secrets.DOCKER_HUB_PWD }} | |
docker build -t ${{ secrets.DOCKER_HUB_USERNAME }}/${{ secrets.DOCKER_HUB_REPO_NAME }}:latest . | |
docker images | |
- name: Push Docker image to Docker Hub | |
run: docker push ${{ secrets.DOCKER_HUB_USERNAME }}/${{ secrets.DOCKER_HUB_REPO_NAME }}:latest | |
deploy: | |
runs-on: ubuntu-latest | |
needs: docker-build | |
steps: | |
- name: Deploy to server | |
uses: appleboy/ssh-action@master | |
with: | |
username: ubuntu | |
host: ${{ secrets.WAS_HOST }} | |
key: ${{ secrets.PRIVATE_KEY }} | |
port: ${{ secrets.WAS_PORT }} | |
script: | | |
# 기존 컨테이너가 실행 중이면 종료하고 삭제 | |
CONTAINER_ID=$(sudo docker ps -q --filter ancestor=${{ secrets.DOCKER_HUB_USERNAME }}/${{ secrets.DOCKER_HUB_REPO_NAME }}) | |
if [ ! -z "$CONTAINER_ID" ]; then | |
sudo docker stop $CONTAINER_ID | |
sudo docker rm $CONTAINER_ID | |
fi | |
# 최신 이미지를 가져와서 실행 | |
sudo docker pull ${{ secrets.DOCKER_HUB_USERNAME }}/${{ secrets.DOCKER_HUB_REPO_NAME }}:latest | |
sudo docker run -d -p 9000:3000 ${{ secrets.DOCKER_HUB_USERNAME }}/${{ secrets.DOCKER_HUB_REPO_NAME }}:latest | |
# 로그 확인 | |
sudo docker logs $(sudo docker ps -q --filter ancestor=${{ secrets.DOCKER_HUB_USERNAME }}/${{ secrets.DOCKER_HUB_REPO_NAME }}) | |
# 사용하지 않는 도커 이미지 제거 | |
sudo docker image prune -f |