Dev./Github Actions

[Cloud] CD: Github Actions - ssh, git pull

Ivan'show 2023. 8. 19.
728x90
반응형

ssh 로 접속해서 git pull 받기

ssh 로 접속하기 위해서는 키가 필요한데 이 부분은 github 에 저장해두고 사용한다.

# lession2.yml
name: CD by git pull

on:
    workflow_dispatch: # from now on, this file is not in automation process
    # push:
    #     branches:
    #         - main

jobs:
    deployment:
        runs-on: ubuntu-latest
        steps:
            - uses: actions/checkout@v3
            - name: git pull via SSH
              uses: appleboy/ssh-action@v1.0.0
              with:
                host: ${{ secrets.HOST }}
                username: ${{ secrets.USERNAME }}
                password: ${{ secrets.PASSWORD }}
                script: |
                    cd dev_django_app
                    git pull
                    docker-compose -f docker-compose.prod.yml up --build -d

https://github.com/marketplace/actions/ssh-remote-commands

이제 로컬에서 업데이트한 내용을 push 하게 되면

# /forumapp/views.py

from rest_framework import viewsets
from drf_spectacular.utils import extend_schema # added

from .models import Topic, Post
from .serializers import TopicSerializer, PostSerializer

# 모델 뷰셋 사용
@extend_schema(tags=["Topic"]) # added
class TopicViewSet(viewsets.ModelViewSet):
    # 어떤 모델 오브젝트를 쓸꺼니? 
    # all() -> create ~ list ~ 전부 알아서 작성됨
    queryset = Topic.objects.all()
    # 어떤 시리얼라이저 쓸꺼니?
    serializer_class = TopicSerializer

class PostViewSet(viewsets.ModelViewSet):
    queryset = Post.objects.all()
    serializer_class = PostSerializer

태그로 묶인 부분을 분리해서 api 페이지에 적용

git push origin main
# github actions ..

======CMD======
cd dev_django_app
git pull
docker-compose -f docker-compose.prod.yml up --build -d

======END======
...
...
...
==============================================
✅ Successfully executed commands to all host.
==============================================

forum 에서 Topic 으로 분리되어서 나와 있는 것을 확인 !

 

 

Github actions 와 NCR 을 이용하여 CD 구축하기

네이버클라우드에서 Container Registry 를 제공한다

 

  • Docker 컨테이너 이미지 저장소
  • 이미지를 저장하고 관리하고 배포할 수 있게 해줌 (hub)
  • Repository 에 접근하려면 로그인이 필요
# 이용가이드
docker login likelion-cr-mh.kr.ncr.ntruss.com

docker push likelion-cr-mh.kr.ncr.ntruss.com/<TARGET_IMAGE[:TAG]>

docker pull likelion-cr-mh.kr.ncr.ntruss.com/<TARGET_IMAGE[:TAG]>

https://github.com/marketplace/actions/docker-login

 

순서 과정

push → checkout → buildx → Login to NCP → push to NCP → SSH auth, open server → pull from NCP

name: CD using Docker Image

on:
    push:
        branches:
            -
                main
jobs:
    # build image / push image to NCR
    # pull Image from NCR / Run new container with new image

    deployment:
        name: push to NCP container registry
        runs-on: ubuntu-latest
        steps:
            # checkout to the branch and get the codes
            - name: Checkout code
              uses: actions/checkout@v3

            # buildx - support amd64, arm64
            - name: Set up Docker buildx
              uses: docker/setup-buildx-action@v2
            
            # NCP Login
            - name: Login to NCR
              uses: docker/login-action@v2
              with:
                registry: ${{ secrets.NCP_CONTAINER_REGISTRY }}
                username: ${{ secrets.NCP_ACCESS_KEY }}
                password: ${{ secrets.NCP_SECRET_KEY }}
            
            - name: Build and Export
              uses: docker/build-push-action@v4
              with:
                context: lion_app # location where image will be built
                push: true
                tags: ${{ secrets.NCP_CONTAINER_REGISTRY }}/lion-app:latest
                # outputs: type=docker,dest=/tmp/lion_app.tar

            - name: git pull via SSH
              uses: appleboy/ssh-action@v1.0.0
              with:
                  host: ${{ secrets.HOST }}
                  username: ${{ secrets.USERNAME }}
                  password: ${{ secrets.PASSWORD }}
                  script: |
                      cd dev_django_app
                      git pull
                      echo "${{ secrets.NCP_SECRET_KEY }}" | docker login ${{ secrets.NCP_CONTAINER_REGISTRY }} --username ${{ secrets.NCP_ACCESS_KEY }} --password-stdin
                      docker pull ${{ secrets.NCP_CONTAINER_REGISTRY }}/lion-app:latest
                      docker-compose -f docker-compose.prod.yml up --build -d

 

 

 

 

 

728x90
반응형

댓글