This file contains hidden or 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: Writon Nodejs(TypeScript) CI with NPM and CD with Docker & AWS EC2 | |
| on: | |
| push: | |
| branches: ['master'] | |
| pull_request: | |
| branches: ['master'] | |
| permissions: | |
| contents: read | |
| jobs: | |
| CI: | |
| runs-on: ubuntu-latest | |
| steps: | |
| # 소스코드 다운로드 | |
| - name: Checkout code | |
| uses: actions/checkout@v3 | |
| - name: Set up Node.js | |
| uses: actions/setup-node@v3 | |
| with: | |
| node-version: '23' # 또는 프로젝트에 맞는 Node.js 버전 | |
| # NPM 종속성 설치 | |
| - name: Install dependencies | |
| run: npm install | |
| # FIREBASE SDK 세팅 | |
| - name: create-json | |
| id: create-json | |
| uses: jsdaniell/create-json@1.1.2 | |
| with: | |
| name: 'writon-firebase-admin.json' | |
| json: ${{ secrets.FIREBASE }} | |
| # 빌드 | |
| - name: Build | |
| run: npm run build | |
| # 린팅 | |
| # - name: Run Lint | |
| # run: npm run lint # package.json에 정의된 lint 스크립트 실행 | |
| # 보안 검사 (예: npm audit) | |
| # - name: Run Security Audit with audit-ci | |
| # run: npx audit-ci --high --production --config ./audit-ci.json | |
| CD: | |
| needs: CI | |
| runs-on: ubuntu-latest | |
| if: github.ref == 'refs/heads/master' | |
| steps: | |
| # 소스코드 다운로드 | |
| - name: Checkout code | |
| uses: actions/checkout@v3 | |
| - name: Set up Node.js | |
| uses: actions/setup-node@v3 | |
| with: | |
| node-version: '16' # 또는 프로젝트에 맞는 Node.js 버전 | |
| # ENV SDK 세팅 | |
| - name: Setting env file | |
| run: | | |
| touch .env | |
| echo "${{ secrets.ENV }}" >> .env | |
| shell: bash | |
| # FIREBASE SDK 세팅 | |
| - name: create-json | |
| id: create-json | |
| uses: jsdaniell/create-json@1.1.2 | |
| with: | |
| name: 'writon-firebase-admin.json' | |
| json: ${{ secrets.FIREBASE }} | |
| - name: Log in to GHCR with PAT | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ghcr.io | |
| username: ${{ github.repository_owner }} | |
| password: ${{ secrets.GHCR_PAT }} | |
| - name: Build Docker Image For Node.js | |
| run: | | |
| docker build -t ghcr.io/${{ github.repository_owner }}/blue:1.0 -f blue.Dockerfile . | |
| docker push ghcr.io/${{ github.repository_owner }}/blue:1.0 | |
| docker build -t ghcr.io/${{ github.repository_owner }}/green:1.0 -f green.Dockerfile . | |
| docker push ghcr.io/${{ github.repository_owner }}/green:1.0 | |
| # 서버에서 Docker 이미지 실행 | |
| - name: EC2 Docker Run | |
| uses: appleboy/ssh-action@v0.1.6 | |
| with: | |
| host: ${{ secrets.EC2_HOST }} | |
| username: ubuntu | |
| key: ${{ secrets.EC2_SSH_KEY }} | |
| script: | | |
| echo ${{ secrets.GHCR_PAT }} | docker login ghcr.io -u ${{ github.repository_owner }} --password-stdin | |
| docker pull ghcr.io/${{ github.repository_owner }}/green:1.0 | |
| docker run -d -p 3001:3000 --name green ghcr.io/${{ github.repository_owner }}/green:1.0 | |
| for i in {1..60}; do | |
| if curl -sf http://localhost:3001/health > /dev/null; then | |
| echo "Green container is ready" | |
| break | |
| fi | |
| echo "Waiting for green container (attempt $i)..." | |
| sleep 1 | |
| if [ $i -eq 60]; then | |
| echo "Green container failed to start in time" | |
| exit 1 | |
| fi | |
| done | |
| sudo touch /etc/nginx/green.flag | |
| sudo rm /etc/nginx/blue.flag | |
| docker stop blue | |
| docker rm blue | |
| docker rmi ghcr.io/${{ github.repository_owner }}/blue:1.0 || true | |
| docker pull ghcr.io/${{ github.repository_owner }}/blue:1.0 | |
| docker run -d -p 3002:3000 --name blue ghcr.io/${{ github.repository_owner }}/blue:1.0 | |
| sleep 10 | |
| for i in {1..60}; do | |
| if curl -sf http://localhost:3002/health > /dev/null; then | |
| echo "Blue container is ready!" | |
| break | |
| fi | |
| echo "Waiting for blue container (attempt $i)..." | |
| sleep 1 | |
| if [ $i -eq 60]; then | |
| echo "Blue container failed to start in time." | |
| exit 1 | |
| fi | |
| done | |
| sudo touch /etc/nginx/blue.flag | |
| sudo rm /etc/nginx/green.flag | |
| docker stop green | |
| docker rm green | |
| docker rmi ghcr.io/${{ github.repository_owner }}/green:1.0 || true |