[Git] Github Actions로 AWS EC2 서버에 배포 자동화

JiKwang Jeong·2021년 10월 2일
2

Github Actions

github에 Actions는 push되는 브랜치에 대해 workflow를 자동화 해주는 도구다.
배포의 순서는 다음과 같다.
1. 소스를 수정한다.
2. git add, git commit 명령으로 소스관리를 한다.
3. git push로 github에 올린다.
4. ssh로 AWS EC2 인스턴스에 접속한다.
5. 원하는 작업폴더로 이동한다.
6. git reset --hard를 통해 현재 작업 위치인 HEAD의 포인터를 특정위치로 변경한다.
7. git fetch를 통해 리모트 저장소 데이터를 가져온다.
8. git pull을 통해 소스를 내려받는다.

4번 내용부터는 Github Actions로 자동화할 수 있다.

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

name: deploy

# 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

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  SSH:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
      - name: Run scripts in server
        uses: appleboy/ssh-action@master
        with:
          key: ${{ secrets.SSH_KEY }}
          host: ${{ secrets.HOST }}
          username: ${{ secrets.USER }}
          script: |
            cd server
            git reset --hard
            git fetch
            git pull
  • name
    : 현재 workflow의 이름
  • applyboy/ssh-action@master
    : ssh로 접속을하고 script를 실행하게 한다.
  • script
    : 원격지 서버에 접속 후 실행할 명령을 작성한다.
  • 보안이 필요한 정보는 Github repository Settings에 Secret에 내용을 넣어 사용한다.
    • SSH_KEY: AWS EC2 인스턴스 생성 시 키 페어 내용 (.pem)
    • HOST: EC2 인스턴스 IP
    • USER: 사용자 이름

profile
기억보다 기록, 난리보다 정리

3개의 댓글

comment-user-thumbnail
2021년 12월 4일

안녕하세요
ssh_key는 어떻게 구해서 넣어야 할까요?

.ssh/authorized_keys 값인지 혹시 알 수 있을까요?

1개의 답글
comment-user-thumbnail
2022년 6월 30일

applyboy/ssh-action@master 는 뭔가요?? uses는 그냥 막지어도 되는건가요?
ㄴ 해결했습니다. applyboy/ssh-action@master를 가져다 쓰는거라서 그대로 써야하네요.

답글 달기