깃 액션 CD 구축

00_8_3·2022년 3월 18일
0

SEEME 이슈

목록 보기
2/10

깃 액션 CD 구축 이슈

  • 처음 설계를 할 때 프리티어 ec2에 컨테이너를 띄우고 깃 액션에서 이미지를 만들어 latest 이미지를 재실행으로 배포 하려함

  • 프리티어 메모리 부족문제로 빌드 부터 막힘 ㅜㅜ

해결

  • 깃 액션에서 빌드 후 dist 폴더를 ec2로 전송하고
  • pm2를 재실행하는 방식으로 배포 완료
name: CD

on:
  push:
    tags:
      - "v*"
jobs:
  wait-for-ci:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [16.x]

    steps:
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v2
        with:
          node-version: ${{ matrix.node-version }}
          registry-url: "https://npm.pkg.github.com"

      - name: Wait for CI check to pass
        uses: fountainhead/action-wait-for-check@v1.0.0
        id: wait-for-ci
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          checkName: build (16.x)
          ref: ${{ github.event.head }}

      - name: Stop workflow
        if: steps.wait-for-ci.outputs.conclusion != 'success'
        run: node -e 'process.exit(1)'

  image-push-scp-build-file:
    needs: wait-for-ci
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [16.x]

    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v2
        with:
          node-version: ${{ matrix.node-version }}
          registry-url: "https://npm.pkg.github.com"

      - name: Install dependencies
        run: npm install

      - name: Run Build
        run: npm run build

      - name: Set env
        run: |
          ... // 정보 보호

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1

      - name: Login to GitHub Container Registry
        uses: docker/login-action@v1
        with:
          registry: ghcr.io
          username: ${{ github.repository_owner }}
          password: ${{ secrets.CR_PAT }}

      - name: Build and push
        uses: docker/build-push-action@v2
        with:
          context: .
          file: ./Dockerfile
          target: prod
          push: true
          tags: |
            ghcr.io/${{ github.repository_owner }}/${{env.REPOSITORY_NAME}}:latest
            ghcr.io/${{ github.repository_owner }}/${{env.REPOSITORY_NAME}}:${{ env.RELEASE_VERSION }}

      - name: build file upload to ec2
        uses: garygrossgarten/github-action-scp@release
        with:
          local: ./dist
          remote: /home/ubuntu/volunteer-server/dist/
          host: ${{ secrets.HOST }}
          username: ${{ secrets.SSH_USER }}
          privateKey: ${{ secrets.SSH_KEY }}
          recursive: true

      - name: ec2 update
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.HOST }}
          username: ${{ secrets.SSH_USER }}
          key: ${{ secrets.SSH_KEY }}
          script: |
            export NVM_DIR=~/.nvm
            source ~/.nvm/nvm.sh
            pm2 restart 0 --update-env

추가 이슈

  • nvm 권한관련 에러 이슈

    appleboy/ssh-action@master
    스크립트 권한

참고

https://padawanr0k.github.io/posts/github/githubAction/
https://github.com/appleboy/scp-action

https://stackoverflow.com/questions/62863080/github-actions-err-bash-line-3-npm-command-not-found

0개의 댓글