프로젝트 로그/테스트x솔루션 JIG 개발기

[Github Action] Github Action을 이용하여 간단한 배포 자동화 구축하기 #2

Life4IoT.rnd 2022. 5. 26. 20:36
반응형

Github Action 설정

Github Action으로 배포 자동화를 위해 workflow를 등록 해야 한다.

본 프로젝트에서는 deployToAWS.yml workflow를 생성 하고, ubuntu 가상 머신을 실행하고 appleboy/ssh-action을 이용하여 SSH 접속 할 수 있도록 내용을 기입 하였다.

 

이때 SSH 접속을 위한 AWS_TEST_X_HOST, AWS_TEST_X_USERNAME, AWS_TEST_X_KEY, AWS_TEST_X_PORT는 Github Repository Settings에 미리 설정 해 둬야 한다.

# This is a basic workflow to help you get started with Actions

name: DEPLOY TESTX TO AWS

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the main branch
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    name: Build
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      - name: executing remote ssh commands using password
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.AWS_TEST_X_HOST }}
          username: ${{ secrets.AWS_TEST_X_USERNAME }}
          key: ${{ secrets.AWS_TEST_X_KEY }}
          port: ${{ secrets.AWS_TEST_X_PORT }}
          script_stop: true
          script: |
            whoami
            ls -al
            cd publish/django_testx/
            ./03_pull_repository.sh
            ./02_update.sh

 

Secrets 파라미터 설정

위에서 사용한 AWS_TEST_X_HOST, AWS_TEST_X_USERNAME, AWS_TEST_X_KEY, AWS_TEST_X_PORT의 값들을 미리 설정하는 방법을 설명한다.

 

AWS_TEST_X_HOST

- SSH 접속을 위한 서버 IP 주소

AWS_TEST_X_KEY

- SSH 접속을 위한 KEY. AWS의 경우 발급받은 KEY 값을 사용한다.

AWS_TEST_X_USERNAME & ASW_TEST_X_PORT

- SSH 접속을 위한 username( AWS의 기본 user는 ubuntu ), SSH 접속을 위한 Port 번호 ( SSH 기본 Port는 22 )

 

 

 

Workflow 수행

workflow 내용에 해당 Repository에 Push나 Pull Request가 발생하면 Workflow가 수행되도록 설정 하였다.

테스트를 위해 Push, Pull Request를 발생 시키기 어려울 경우, 아래와 같이 Run workflow를 수행하면 작성한 workflow가 정상 동작 하는지 확인 할 수 있다.

 

아래와 같이 녹색 체크 표시가 되면, 정상 수행 되었다고 판단 할 수 있다.

workflow 동작이 실패 되었거나, 자세한 동작 로그를 확인 하기 위해서는 아래와 같이 workflow 이벤트 이름 클릭 --> Build --> executing remote ssh commands using password를 클릭하면 서버에서 수행된 로그들을 확인 할 수 있다.

 

 

 

[Github Action] Github Action을 이용하여 간단한 배포 자동화 구축하기 #1

- https://kaizen8501.tistory.com/259

반응형