[Github Action] Github Action์ ์ด์ฉํด์ EC2์ ์๋๋ฐฐํฌํ๊ธฐ (์์ฑ์ค)
์๋ ๋ฐฐํฌ ๋ก์ง์,
๊ฐ๋ฐ ๋ณ๊ฒฝ ์ฌํญ์ด main ๋ธ๋ ์น์ merge ๋๋ฉด ec2์ ์ ์ํด์ ํด๋น ํ๋ก์ ํธ์ ๋ธ๋ ์น๋ฅผ pull ํ๋ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ๊ณ ์์๋ค.
๊ทธ๋ฐ๋ฐ Github Action์ ์ฌ์ฉํด์ ์ ๊ณผ์ ์ ์๋ํ์ํฌ ์ ์๋ค๋ ๊ฒ์ ์๊ฒ๋์ด ์ ์ฉํด๋ณด์๋ค.
ํ์ดํ๋ผ์ธ
1. ๋ก์ปฌ์ปดํจํฐ์์ main ๋ธ๋ ์น๋ก push ํ๋ฉด
2. Github Action ์คํ
1) ec2์ ์ ์
2) ์์ฑ๋ script ์คํ
- cloneํ ํ๋ก์ ํธ๋ก ์ด๋
- ์ง์ ํ ๋ธ๋ ์น๋ฅผ git pull
3. ๋ฐฐํฌ ์๋ฃ
๋ฐฉ๋ฒ
1. Github Action ์์ฑํ๊ธฐ
Github > ์ ์ฉํ ํ๋ก์ ํธ > Action

set up a workflow yourself ํด๋ฆญ
.yml ํ์ผ ์์ฑํ๊ธฐ
name: main branch auto ci process script
on: # ์๋ job์ ์คํ์ํฌ action
push:
branches: [ main ]
jobs:
deploy:
name: deploy
runs-on: ubuntu-latest # ์คํ๋ ์ธ์คํด์ค OS์ ๋ฒ์
steps:
- name: excuting remote ssh commands
uses: appleboy/ssh-action@v0.1.9 # ssh ์ ์ํ๋ ์คํ์์ค
with:
host: ${{ secrets.REMOTE_IP }} # ec2 ์ธ์คํด์ค ip
username: ${{ secrets.REMOTE_USER }} # ubuntu ์์ด๋
key: ${{ secrets.REMOTE_PRIVATE_KEY }} # ec2 ์ธ์คํด์ค pem key
port: ${{ secrets.REMOTE_SSH_PORT }} # ์ ์ํฌํธ๋ฒํธ (๋ณดํต 22)
script: | # ์คํ๋ ์คํฌ๋ฆฝํธ
cd /var/www/html/test-project
git pull
Commit changes...
2. .yml ํ์ผ์์ ์์ฑํ ์์ ์ํธ ๋ฐ ๋ณ์ ์์ฑํ๊ธฐ
Github > ์ ์ฉํ ํ๋ก์ ํธ > Settings > Secrets ans variables > Actions

New repository secret ํด๋ฆญ
์์ฑํ๊ธฐ

Add secret