WEB/Server

GithubAction์„ ์ด์šฉํ•œ CD (feat: appleboy/ssh-actions@master)

kite707 2023. 4. 26. 00:28

CD๋ฅผ ํ•˜๋ฉด ๋ณดํ†ต codeploy๋ฅผ ์ด์šฉํ•ด ๋งŽ์ด ํ•˜๋Š” ๊ฒƒ ๊ฐ™๋˜๋ฐ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ• ์—ญ์‹œ ๊ธฐ๋กํ•ด๋ณด๊ณ ์ž ํ•œ๋‹ค. ์„œ๋ฒ„์— ssh๋กœ ์ ‘์†ํ•˜์—ฌ ์ง€์ •๋œ ๋ช…๋ น์–ด๋ฅผ ์—ฐ์†ํ•ด์„œ ์‹คํ–‰ํ•ด์ฃผ๋Š” ์ž‘์—…์„ ์ž๋™ํ™” ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๊ธฐ๋กํ•œ ๊ธ€์ด๋‹ค. ์ด ๋ฐฉ๋ฒ•์€ ์•ˆ์ „ํ•˜๊ณ  ๊ณต์‹์ ์ธ ๋ฐฉ๋ฒ•์ด๋ผ๊ธฐ ๋ณด๋‹ค๋Š” ์•ผ๋งค ๋Š๋‚Œ์ด ๊ฐ•ํ•˜์ง€๋งŒ ์ผ๋‹จ ๋Œ์•„๊ฐ€๋„๋ก ํ•˜๋Š” ๋ฐ์— ๊ด€์‹ฌ์ด ์žˆ๋Š” ์‚ฌ๋žŒ์„ ์œ„ํ•ด ๊ทธ ๋ฐฉ๋ฒ•์„ ๊ธฐ๋กํ•ด๋ณด๊ณ ์ž ํ•œ๋‹ค.

 

์‚ฌ์ „ ์š”๊ตฌ ์‚ฌํ•ญ

EC2 ๋ฐ ๋ฐฐํฌํ•  ์ฝ”๋“œ

EC2 password๋กœ ์ ‘์†ํ•˜๋Š” ์„ค์ •

 

 

์ƒํ™ฉ

๋‚˜์˜ ๊ฒฝ์šฐ ์„œ๋ฒ„์— ์ ‘์†ํ•ด์„œ ์‹คํ–‰๋˜์–ด์•ผ ํ•˜๋Š” ๋ช…๋ น์–ด๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

pm2 kill
rm -rf dist  //ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ๋นŒ๋“œ ํŒŒ์ผ ์‚ญ์ œ
git pull origin main //์ดํ›„ ๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅ
yarn run buildOnly  //ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ ๋นŒ๋“œ
pm2 start dist

 

์ตœ์ข…์ ์œผ๋กœ ์™„์„ฑ๋œ ๋ชจ์Šต์€ ๋‹ค์Œ๊ณผ ๊ฐ™์„ ๊ฒƒ์ด๋‹ค. ๊นƒํ—™ ์•ก์…˜์—๋Š” ์„œ๋ฒ„์— ssh์ ‘์†์„ ํ•˜๋Š” ๊ฒƒ์„ ๋•๋Š” workflow๊ฐ€ ์žˆ๋‹ค. ์ด๊ฒƒ์„ ์ด์šฉํ•ด์„œ ์„œ๋ฒ„์— ์ ‘์†ํ•œ ๋’ค, ์œ„ ๋ช…๋ น์–ด๋“ค์„ ์ฐจ๋ก€๋กœ ์‹คํ–‰ํ•  ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๊ธฐ ์œ„ํ•ด 3๊ฐ€์ง€ ๋‹จ๊ณ„๋กœ ๋‚˜๋ˆ„์–ด ์ง„ํ–‰ํ•  ๊ฒƒ์ด๋‹ค.

  1. appleboy/ssh-action@master๋ฅผ ์ด์šฉํ•ด ์„œ๋ฒ„ ์ ‘์†
  2. git credential๋ฅผ ์ด์šฉํ•ด ๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅ ๊ณผ์ • ์Šคํ‚ต
  3. ์œ„์— ์ ํ˜€์žˆ๋Š” ๋ช…๋ น์–ด ์‹คํ–‰

 

appleboy/ssh-action@master๋ฅผ ์ด์šฉํ•ด ์„œ๋ฒ„ ์ ‘์†

๋จผ์ € ์„œ๋ฒ„์— ์ ‘์†ํ•ด๋ณด๋„๋ก ํ•˜์ž. ์ƒ์„ธํ•œ ์˜ต์…˜์€ ์•„๋ž˜ ๋งํฌ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

https://github.com/appleboy/ssh-action

 

GitHub - appleboy/ssh-action: GitHub Actions for executing remote ssh commands.

GitHub Actions for executing remote ssh commands. Contribute to appleboy/ssh-action development by creating an account on GitHub.

github.com

๋จผ์ € ์ž์‹ ์˜ ๊นƒํ—™ ๋ ˆํฌ์— ๋ฃจํŠธ ๋””๋ ‰ํ† ๋ฆฌ์—์„œ .github/workflowsํด๋” ์•ˆ์— yamlํŒŒ์ผ์„ ํ•˜๋‚˜ ์ƒ์„ฑํ•˜๋„๋ก ํ•˜์ž. ์ด๋ฆ„์€ ๋งˆ์Œ๋Œ€๋กœ ํ•ด๋„ ๋œ๋‹ค. ๋‚˜๋Š” deploy.yamlํŒŒ์ผ์„ ๋งŒ๋“ค์—ˆ๋‹ค.

name: deploy

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]
jobs:
  AUTO_DEPLOY:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2   //point 1
      - name: Run scripts in server
        uses: appleboy/ssh-action@master. //point 2
        with:
          password: ${{ secrets.PASSWORD }}
          host: ${{ secrets.HOST }}
          username: ${{ secrets.USER_NAME }}
          port: 22
          script: |
            //์„œ๋ฒ„์— ์ ‘์†ํ•œ ๋’ค ์‹คํ–‰ํ•  ๋ช…๋ น์–ด๋“ค

์œ„ ํŒŒ์ผ์—์„œ password, host, username์€ ๊นƒํ—™ ๋ ˆํฌ์˜ ํ™˜๊ฒฝ๋ณ€์ˆ˜๋กœ ๋ฏธ๋ฆฌ ๋“ฑ๋กํ•ด๋‘์–ด์•ผ ํ•œ๋‹ค. password๋Š” ์•„๋ž˜ ๊ธ€์„ ์ฐธ๊ณ ํ•ด์„œ password๋กœ ์„œ๋ฒ„์— ์ ‘์†ํ•˜๋Š” ์„ค์ •์„ ํ•ด์•ผํ•œ๋‹ค.

AWS EC2 ๋น„๋ฐ€๋ฒˆํ˜ธ๋กœ ์ ‘์†ํ•˜๊ธฐ

 

AWS EC2 ๋น„๋ฐ€๋ฒˆํ˜ธ๋กœ ์ ‘์†ํ•˜๊ธฐ

aws์˜ ec2 ์ธ์Šคํ„ด์Šค์— ์ ‘์†ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ aws์—์„œ ๋ฐœ๊ธ‰ํ•œ pem(key pair)์œผ๋กœ ์ธ์ฆํ•˜๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋ž˜์„œ key pair์—†์ด๋Š” ์ ‘์†์„ ํ•  ์ˆ˜๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ถ€๋“์ดํ•˜๊ฒŒ ์™ธ๋ถ€

bgpark.tistory.com

๋น„๋ฐ€๋ฒˆํ˜ธ๋กœ ์ ‘๊ทผํ•˜๋Š” ์„ค์ •์„ ํ–ˆ๋‹ค๋ฉด ์•„๋ž˜ ๊ฐ’๋“ค์„ ํ™˜๊ฒฝ๋ณ€์ˆ˜๋กœ ๋“ฑ๋กํ•ด์ฃผ๋„๋ก ํ•˜์ž.


password : ์œ„์—์„œ ์„ค์ •ํ•œ ๋น„๋ฐ€๋ฒˆํ˜ธ
host : public IPv4 DNS๊ฐ’ (AWS console์—์„œ ํ™•์ธ ๊ฐ€๋Šฅ)
username : home/000์— ๋“ค์–ด๊ฐ€๋Š” ์œ ์ €. ์ผ๋ฐ˜์ ์œผ๋กœ ubuntu์ด๋‚˜ ๋‚˜๋Š” ec2-user์˜€์Œ. ec2์ ‘์†ํ•ด์„œ ํด๋” ์ด๋ฆ„์„ ํ™•์ธํ•ด ์•Œ ์ˆ˜ ์žˆ์Œ

๊ฐ’๋“ค์„ ํ™˜๊ฒฝ๋ณ€์ˆ˜๋กœ ๋“ฑ๋กํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

Github action ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ๋“ฑ๋ก

 

GitHub Actions - ํ™˜๊ฒฝ๋ณ€์ˆ˜ ๋“ฑ๋ก ๋ฐฉ๋ฒ•

github actionsํ™˜๊ฒฝ๋ณ€์ˆ˜ ๋“ฑ๋กํ•˜๊ธฐ 1. ์ƒ๋‹จ ํƒญ์˜ Settings ๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. 2. ์ขŒ์ธก Secrets ํ•ญ๋ชฉ์œผ๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. 3. "Add a New secret" ํ•ญ๋ชฉ ํด๋ฆญ 4. Name , Value ๋ฅผ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. 5. "Add secret" ํด๋ฆญ 6. ๋“ฑ๋ก๋œ Name ์„ ๋ณต

dnight.tistory.com

์ด์ œ ๋‹ค์Œ ๋‹จ๊ณ„์—์„œ๋Š” ๊นƒํ—ˆ๋ธŒ์—์„œ ํด๋ก ์„ ๋ฐ›์„ ๋•Œ ๋งˆ๋‹ค ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•˜๋Š” ๊ณผ์ •์„ ์ƒ๋žตํ•ด๋ณด๋„๋ก ํ•˜์ž.

 

๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅ ๊ณผ์ • ์Šคํ‚ต

2๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. ์• ์ดˆ์— ec2์—์„œ ๊ฐ’์„ ํด๋ก ๋ฐ›์„ ๋•Œ ssh๋กœ ํด๋ก ๋ฐ›์•„๋„ ๋˜๊ณ  git credentials๋ฅผ ์ด์šฉํ•ด๋„ ๋œ๋‹ค. ์—ฌ๊ธฐ์„œ๋Š” git credentials๋ฅผ ์ด์šฉํ•  ๊ฒƒ์ด๋‹ค. ๋ฐฉ๋ฒ•์€ ๊ฐ„๋‹จํ•˜๋‹ค. ec2์— ์ ‘์†ํ•œ ๋’ค ์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰์‹œํ‚ค๋ฉด ๋œ๋‹ค.

git config --global credential.helper store

์ด๋ ‡๊ฒŒ ํ•œ ๋’ค ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ๋ฉด ๊ทธ ๊ฐ’์ด ์ €์žฅ๋˜์–ด ๋‹ค์Œ๋ถ€ํ„ฐ๋Š” ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅ๋ฐ›์ง€ ์•Š๋Š”๋‹ค.

 

๋ช…๋ น์–ด ์ž…๋ ฅ

์ด์ œ ์„œ๋ฒ„์— ์ ‘์†ํ–ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ  ์ˆ˜ํ–‰ํ•ด์•ผ ํ•  ์ฝ”๋“œ๋“ค์„ ์ž…๋ ฅํ•ด์ฃผ๋ฉด ๋์ด ๋‚œ๋‹ค. ์ „์ฒด deploy.yaml์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

name: deploy

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]
jobs:
  AUTO_DEPLOY:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2   //point 1
      - name: Run scripts in server
        uses: appleboy/ssh-action@master. //point 2
        with:
          password: ${{ secrets.PASSWORD }}
          host: ${{ secrets.HOST }}
          username: ${{ secrets.USER_NAME }}
          port: 22
          script: |
        	pm2 kill
		rm -rf dist  //ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ๋นŒ๋“œ ํŒŒ์ผ ์‚ญ์ œ
		git pull origin main //git credentials์— ์˜ํ•ด ๋น„๋ฐ€๋ฒˆํ˜ธ ์ž…๋ ฅ ๊ณผ์ • ์Šคํ‚ต
		yarn run buildOnly  //ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ ๋นŒ๋“œ
		pm2 start dist