BackEnd๐ŸŒฑ/Etc

Spring Boot + GitHub Actions + AWS CodeDeploy๋ฅผ ํ™œ์šฉํ•œ CI/CD ๊ตฌ์ถ•

dkswnkk 2022. 12. 4. 12:55

์„œ๋ก 

์ง€๊ธˆ๊นŒ์ง€ ๋งค๋ฒˆ ํ”„๋กœ์ ํŠธ๋ฅผ ๊ตฌ์ถ•ํ•  ๋•Œ๋งˆ๋‹ค ์ด์ „ ์ฝ”๋“œ๋“ค์„ ๋ฒˆ๊ฑฐ๋กญ๊ฒŒ ๋ด๊ฐ€๋ฉด์„œ CI/CD๋ฅผ ๊ตฌ์ถ•ํ–ˆ์—ˆ๋Š”๋ฐ, ์ด๋ฒˆ ๊ธฐํšŒ์— ํ•œ๋ฒˆ ๋ฌธ์„œํ™”๋ฅผ ํ•ด๋ณด๊ณ ์ž ๊ธ€์„ ์ž‘์„ฑํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

 

[CI/CD] CI/CD๋ž€?, ์ง€์†์  ํ†ตํ•ฉ(Continuous Integration) ๊ณผ ์ง€์†์  ๋ฐฐํฌ(Continuous Deployment)

์„œ๋ก  ํ˜„์žฌ ์ž๊ทธ๋งŒ ํ† ์ด ํ”„๋กœ์ ํŠธ๋ฅผ ํ˜‘์—…ํ•˜์—ฌ ์ง„ํ–‰ ์ค‘์ธ๋ฐ, ํŒ€์›๊ณผ ์ œ๋Œ€๋กœ branch ์ถฉ๋Œ์„ ํ•ด๊ฒฐํ•˜์ง€ ๋ชปํ•ด ์ผ์–ด๋‚˜๋Š” ์—๋Ÿฌ์™€, ์ˆ˜์ž‘์—…์œผ๋กœ EC2์— ์ ‘์†ํ•˜์—ฌ ์ง์ ‘ ๋ฐฐํฌ๋ฅผ ํ•ด์•ผ ํ•˜๋Š” ์‚ฌ์†Œํ•˜์ง€๋งŒ ์ž‘์—…๋“ค์ด

dkswnkk.tistory.com

CI/CD๋ฅผ ์ ์šฉํ•˜๋Š” ์ด์œ ๋Š” ์ด์ „์— ์ž‘์„ฑํ•œ ๊ฒŒ์‹œ๊ธ€์ด ์žˆ์œผ๋‹ˆ ์ฐธ๊ณ ํ•ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

๊ธฐ๋ณธ์ ์œผ๋กœ EC2๋Š” ์ƒ์„ฑ๋˜์–ด์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•˜๊ณ  ์ง„ํ–‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. 

 

ํ™˜๊ฒฝ

EC2(Amazone Linux), RDS(MySQL), S3, Spring Boot 2.6.4

์ง„ํ–‰ํ•˜๋Š” ํ™˜๊ฒฝ์€ ์œ„์™€ ๊ฐ™์œผ๋ฉฐ, ์—ฌ๊ธฐ์„œ ์ €๋Š” Ubuntu๊ฐ€ ์•„๋‹ˆ๋ผ Linux๋กœ ์ง„ํ–‰ํ–ˆ์Œ์„  ๊ผญ ํ™•์ธํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ์ง€๊ธˆ์€ ํ•ด๊ฒฐ๋˜์—ˆ๋Š”์ง€ ๋ชจ๋ฅด๊ฒ ์œผ๋‚˜ ์ด์ „์— Ubuntu๋กœ ์ง„ํ–‰ํ•˜๋Š” ๊ณผ์ •์—์„œ CodeDeply Agent๋ฅผ ์„ค์น˜ํ•˜๋Š” ๋ถ€๋ถ„์—์„œ Ruby๋ฒ„์ „์ด ๋ฌธ์ œ๋ฅผ ์ผ์œผ์ผœ ์ง„ํ–‰ํ•˜์ง€ ๋ชปํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค.

 

๊ณผ์ •

https://velog.io/@bluewind8791/Github-Actions

CI/CD ํ๋ฆ„์€ ์œ„ ์ด๋ฏธ์ง€์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. 

  1. ๊นƒํ—ˆ๋ธŒ์ƒ์œผ๋กœ ์ฝ”๋“œ๋ฅผ Push ํ•˜๊ฒŒ ๋˜๋ฉด GitHub Actions์ด ์ž๋™์œผ๋กœ ์‹คํ–‰๋˜์–ด CI (๋นŒ๋“œ) ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
  2. ์ฝ”๋“œ์ƒ์— ๋ฌธ์ œ๊ฐ€ ์—†๋‹ค๋ฉด ์ด๋•Œ ๋นŒ๋“œ ์ž‘์—…์„ ํ†ตํ•ด jarํŒŒ์ผ์ด ์ƒ์„ฑ๋˜๊ฒŒ ๋˜๊ณ  ์‚ฌ์ „์— ์ž‘์„ฑํ•œ ๋ฐฐํฌ ์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ๊ณผ ํ•จ๊ป˜ AWS์˜ ์Šคํ† ๋ฆฌ์ง€ ์„œ๋น„์Šค์ธ S3์—๊ฒŒ ์ „๋‹ฌ๋˜์–ด ์ €์žฅ๋˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
  3. S3์— ์„ฑ๊ณต์ ์œผ๋กœ ์ €์žฅ๋˜์—ˆ๋‹ค๋ฉด EC2์— ์„ค์น˜ํ•œ CodeDeply Agent๊ฐ€ S3์— ์ €์žฅ๋œ ํ”„๋กœ์ ํŠธ ํŒŒ์ผ์„ ๊ฐ€์ ธ์™€์„œ ๋‚ด๋ ค๋ฐ›์Šต๋‹ˆ๋‹ค.
  4. ๊ทธ ํ›„, ๋ฐฐํฌ ์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์„ ์ฝ์–ด ๋“ค์—ฌ์„œ ํ”„๋กœ์ ํŠธ ์‹คํ–‰์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.

์ฆ‰, ๊นƒํ—ˆ๋ธŒ ์ƒ์œผ๋กœ ์ฝ”๋“œ๋ฅผ push ํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ์ž๋™์œผ๋กœ ์ตœ์‹  ๋ฒ„์ „์˜ ํ”„๋กœ์ ํŠธ๊ฐ€ EC2์— ๋‚ด๋ ค๋ฐ›์•„์ ธ์„œ ์ด ํ”„๋กœ์ ํŠธ๋ฅผ ์‹คํ–‰ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

๋ชฉ์ฐจ

  1. CI Actions ์„ค์ •
  2. IAM ์„ค์ •
  3. S3 ์„ค์ •
  4. GitHub Actions secrets ์ถ”๊ฐ€
  5. depley shell script ์ž‘์„ฑ
  6. CD Actions S3 ์—…๋กœ๋“œ ์ž‘์„ฑ
  7. AWS CodeDeploy & AWS EC2 ์—ฐ๋™(EC2 ์„ค์ •, CodeDeploy ์„ค์ •)
  8. CD Actions Deploy ์ž‘์„ฑ

 

1. CI Actions ์„ค์ •

ํ”„๋กœ์ ํŠธ ๋ ˆํฌ์ง€ํ† ๋ฆฌ์˜ Actions ํด๋ฆญ

๋จผ์ € ๋ณธ์ธ์˜ ํ”„๋กœ์ ํŠธ ๋ ˆํฌ์ง€ํ† ๋ฆฌ์— ์ ‘๊ทผ ํ›„ ์šฐ์ธก ์ƒ๋‹จ์— ์žˆ๋Š” Actions๋ฅผ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค.

set up a workflow yourself ํด๋ฆญ

ํŒŒ๋ž€์ƒ‰ ๊ธ€์”จ์˜ set up a workflow yourself๋ฅผ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค.

CI ์ž‘์„ฑ


# workflow์˜ ์ด๋ฆ„
name: CI

# ํ•ด๋‹น workflow๊ฐ€ ์–ธ์ œ ์‹คํ–‰๋  ๊ฒƒ์ธ์ง€์— ๋Œ€ํ•œ ํŠธ๋ฆฌ๊ฑฐ๋ฅผ ์ง€์ •
on:
  push:
    branches: [ main ] # main branch๋กœ push ๋  ๋•Œ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.
  pull_request:
    branches: [ main ]  # main branch๋กœ pull request๋  ๋•Œ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

# workflow๋Š” ํ•œ๊ฐœ ์ด์ƒ์˜ job์„ ๊ฐ€์ง€๋ฉฐ, ๊ฐ job์€ ์—ฌ๋Ÿฌ step์— ๋”ฐ๋ผ ๋‹จ๊ณ„๋ฅผ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
jobs:
  build:
    name: CI
    # ํ•ด๋‹น jobs์—์„œ ์•„๋ž˜์˜ steps๋“ค์ด ์–ด๋– ํ•œ ํ™˜๊ฒฝ์—์„œ ์‹คํ–‰๋  ๊ฒƒ์ธ์ง€๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
    runs-on: ubuntu-latest

    steps:
     # ์ž‘์—…์—์„œ ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ๋„๋ก $GITHUB_WORKSPACE์—์„œ ์ €์žฅ์†Œ๋ฅผ ์ฒดํฌ์•„์›ƒํ•ฉ๋‹ˆ๋‹ค.
      - uses: actions/checkout@v2
      - name: Set up JDK 11
        uses: actions/setup-java@v2
        with:
          java-version: '11'
          distribution: 'zulu'

      - name: Grant execute permission for gradlew
        run: chmod +x ./gradlew
        shell: bash
      
      - name: Build with Gradle
        run: ./gradlew build
        shell: bash

์œ„ CI ๋‚ด์šฉ์„ ์ž‘์„ฑ ํ›„ Start commit๋ฅผ ๋ˆŒ๋Ÿฌ์ค๋‹ˆ๋‹ค.

main branch๋กœ push ํ˜น์€ pull_request๋ฅผ ์ˆ˜ํ–‰ํ–ˆ์„ ๋•Œ ์œ„ CI ์•ก์…˜์ด ๋™์ž‘ํ•˜๊ฒŒ ๋˜๋ฉฐ, ํ•ด๋‹น CI์˜ ์—ญํ• ์€ ubuntu ์ƒ์—์„œ ์ž๋™์œผ๋กœ ํ”„๋กœ์ ํŠธ๋ฅผ ๋นŒ๋“œํ•˜์—ฌ ํ”„๋กœ์ ํŠธ์˜ ์˜ค๋ฅ˜๊ฐ€ ์žˆ๋Š”์ง€ ์—†๋Š”์ง€ ๊ฒ€์ฆ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์œ„ ์ž‘์—…์„ ํ†ตํ•ด ํ”„๋กœ์ ํŠธ์˜ ์˜ค๋ฅ˜๊ฐ€ ์žˆ๋‹ค๋ฉด ์‚ฌ์ „์— main branch๋กœ์˜ ๋ถˆ๋ฏธ์Šค๋Ÿฌ์šด merge๊ฐ€ ์ผ์–ด๋‚˜์ง€ ์•Š๋„๋ก ๋ง‰์„ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค๋ฉด ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ๋˜ํ•œ ์ž๋™์œผ๋กœ ์ˆ˜ํ–‰ํ•˜์—ฌ ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ๋ฅผ ์ž๋™์œผ๋กœ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

2. IAM ์„ค์ •

AWS๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์™ธ๋ถ€ ์„œ๋น„์Šค์˜ ์ ‘๊ทผ์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ IAM(Identify & Access Management)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ ‘๊ทผ ๊ถŒํ•œ์„ ํš๋“ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

AWS IAM ์ ‘๊ทผ
์‚ฌ์šฉ์ž ์ถ”๊ฐ€

AWS์˜ ๋Œ€์‹œ๋ณด๋“œ์—์„œ IAM์— ์ ‘๊ทผ ํ›„ '์‚ฌ์šฉ์ž ์ถ”๊ฐ€'๋ฅผ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๊ธฐ์„œ ์‚ฌ์šฉ์ž๋ž€ AWS ์™ธ๋ถ€์—์„œ ์ ‘์†ํ•  ์ˆ˜ ์žˆ๋Š” ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•  ๋Œ€์ƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

์‚ฌ์šฉ์ž ์ถ”๊ฐ€

์‚ฌ์šฉ์ž์˜ ์ด๋ฆ„๊ณผ ์•ก์„ธ์Šค ์œ ํ˜•์„ ์„ ํƒํ•œ ๋’ค ๋‹ค์Œ์„ ๋ˆ„๋ฆ…๋‹ˆ๋‹ค.

๊ถŒํ•œ ์„ค์ •

ํ•ด๋‹น ํŽ˜์ด์ง€์—์„œ AWSCodeDeployFullAccess || AmazonS3FullAccess ์ด ๋‘ ๊ฐœ์˜ ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ 3, 4๋ฒˆ ๊ณผ์ •์€ ์„ ํƒ์‚ฌํ•ญ์ด๊ธฐ์— ๊ทธ๋ƒฅ ๋„˜์–ด๊ฐ€๋ฉด ์œ„ ์ด๋ฏธ์ง€์™€ ๊ฐ™์ด ์šฐ๋ฆฌ๊ฐ€ ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•œ ์‚ฌ์šฉ์ž๊ฐ€ ์ƒ์„ฑ๋˜๊ณ  ACCESS KEY์™€ SECRET KEY๊ฐ€ ๋ฐœ๊ธ‰๋ฉ๋‹ˆ๋‹ค. 

์œ„ ์•Œ๋ฆผ์—๋„ ๋‚˜์™€์žˆ๋“ฏ์ด ์ง€๊ธˆ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ACCESS KEY์™€ SECRET KEY๋Š” ์ง€๊ธˆ ์ด ํŽ˜์ด์ง€๊ฐ€ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š” ๋งˆ์ง€๋ง‰ ๊ธฐํšŒ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋”ฐ๋กœ ACCESS KEY์™€ SECRET KEY๋ฅผ ๋‹ค๋ฅธ ๊ณณ์— ๋ฉ”๋ชจํ•ด ๋‘๊ฑฐ๋‚˜ .csvํŒŒ์ผ๋กœ ๋‹ค์šด๋กœ๋“œ ํ•ด๋†“๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

 

3. S3 Bucket ์„ค์ •

Amazone S3๋Š” Simple Stroage Service์˜ ์ค„์ž„๋ง๋กœ์จ ํ™•์žฅ์„ฑ, ๋ฐ์ดํ„ฐ ๊ฐ€์šฉ์„ฑ, ๋ณด์•ˆ ๋ฐ ์„ฑ๋Šฅ์„ ์ œ๊ณตํ•˜๋Š” ์•„๋งˆ์กด์˜ ๊ฐ์ฒด ์Šคํ† ๋ฆฌ์ง€ ์„œ๋น„์Šค์ž…๋‹ˆ๋‹ค. ์ฆ‰ ํŒŒ์ผ์„ ์ €์žฅํ•˜๋Š” ํŒŒ์ผ์„œ๋ฒ„๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

๋Œ€์‹œ๋ณด๋“œ

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋Œ€์‹œ๋ณด๋“œ์—์„œ S3๋ฅผ ํด๋ฆญํ•˜์—ฌ ์ ‘๊ทผํ•ฉ๋‹ˆ๋‹ค.

๋ฒ„ํ‚ท ๋งŒ๋“ค๊ธฐ

๋ฒ„ํ‚ท ๋งŒ๋“ค๊ธฐ๋ฅผ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค.

๋”ฑํžˆ ๋ฐฐํฌ๋งŒ์„ ์œ„ํ•ด์„œ๋ผ๋ฉด ๋”ฐ๋กœ ์„ค์ •ํ•ด ์ค„ ๊ฒƒ์ด ์—†์Šต๋‹ˆ๋‹ค. ๋ฒ„ํ‚ท ์ด๋ฆ„๋งŒ ์ž‘์„ฑํ•ด์ฃผ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ƒ์„ฑ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

4. GitHub Actions secrets ์ถ”๊ฐ€

2๋ฒˆ์—์„œ ์ƒ์„ฑํ•œ IAM ACCESS KEY์™€ SECRET KEY๋ฅผ ๊นƒํ—ˆ๋ธŒ ๋ ˆํฌ์ง€ํ† ๋ฆฌ ์ƒ์— ํ‰๋ฌธ ๊ทธ๋Œ€๋กœ ๋…ธ์ถœํ•˜๊ฒŒ ๋˜๋ฉด ๋ณด์•ˆ์ƒ ๋งค์šฐ ํฐ ๋ฌธ์ œ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ repository์˜ 'Setting -> Security -> Secrets -> Actions'์—์„œ ํ•ด๋‹น ํ‚ค ๊ฐ’์„ ๋“ฑ๋กํ•˜์—ฌ ๋…ธ์ถœํ•˜์ง€ ์•Š๊ณ  ์‚ฌ์šฉํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

๋ ˆํฌ์ง€ํ† ๋ฆฌ์˜ Settings ํด๋ฆญ
Security์˜ Secrets ๋ชฉ๋ก์˜ Actions ํด๋ฆญ
New repository secret ํด๋ฆญ

2๋ฒˆ์—์„œ ์ƒ์„ฑํ•œ IAM ACCESS KEY์™€ SECRET KEY๋ฅผ ๋“ฑ๋กํ•ฉ๋‹ˆ๋‹ค.

๋ฆฌ์ „ ์ถ”๊ฐ€

๋˜ํ•œ region๋„ ์ถ”๊ฐ€ํ•ด์ค˜์•ผ ํ•˜๋Š”๋ฐ ์œ„์™€ ๊ฐ™์ด ap-northeast-2๋ฅผ ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค. ๋ณธ์ธ์ด ๋‹ค๋ฅธ region์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค๋ฉด ํ•ด๋‹น region์„ ์ž…๋ ฅํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

์œ„์™€ ๊ฐ™์ด ACCESS KEY์™€ SECRET KEY ๊ทธ๋ฆฌ๊ณ  REGION๊นŒ์ง€ ์„ธ ๊ฐœ ๋‹ค ๋“ฑ๋กํ•˜๋„๋ก ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

5. deploy shell script ์ž‘์„ฑ

ํ”„๋กœ์ ํŠธ ํด๋”์— scripts ํด๋” ์ƒ์„ฑ ํ›„ deploy.sh ์ž‘์„ฑ

#!/bin/bash
BUILD_JAR=$(ls /home/ec2-user/action/build/libs/*.jar)
JAR_NAME=$(basename $BUILD_JAR)
echo "> build ํŒŒ์ผ๋ช…: $JAR_NAME" >> /home/ec2-user/action/deploy.log

echo "> build ํŒŒ์ผ ๋ณต์‚ฌ" >> /home/ec2-user/action/deploy.log
DEPLOY_PATH=/home/ec2-user/action/
cp $BUILD_JAR $DEPLOY_PATH

echo "> ํ˜„์žฌ ์‹คํ–‰์ค‘์ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ pid ํ™•์ธ" >> /home/ec2-user/action/deploy.log
CURRENT_PID=$(pgrep -f $JAR_NAME)

if [ -z $CURRENT_PID ]
then
  echo "> ํ˜„์žฌ ๊ตฌ๋™์ค‘์ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์—†์œผ๋ฏ€๋กœ ์ข…๋ฃŒํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค." >> /home/ec2-user/action/deploy.log
else
  echo "> kill -15 $CURRENT_PID"
  kill -15 $CURRENT_PID
  sleep 5
fi

DEPLOY_JAR=$DEPLOY_PATH$JAR_NAME
echo "> DEPLOY_JAR ๋ฐฐํฌ"    >> /home/ec2-user/action/deploy.log
nohup java -jar $DEPLOY_JAR >> /home/ec2-user/deploy.log 2>/home/ec2-user/action/deploy_err.log &

๋ณธ์ธ ํ”„๋กœ์ ํŠธ ํด๋”์— ์œ„์™€ ๊ฐ™์ด deploy.sh๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ์ถ”๊ฐ€ํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

์œ„ ์Šคํฌ๋ฆฝํŠธ๋Š” ์—ญํ• ์€ ์ œ์ผ ์ดˆ๋ฐ˜์— ์„ค๋ช…ํ•œ ๋Œ€๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  1. ๊นƒํ—ˆ๋ธŒ์— push ํ•˜๊ฒŒ ๋˜๋ฉด ์ฝ”๋“œ์ƒ์— ๋ฌธ์ œ๊ฐ€ ์—†๋‹ค๋ฉด ์ด๋•Œ ๋นŒ๋“œ ์ž‘์—…์„ ํ†ตํ•ด jarํŒŒ์ผ์ด ์ƒ์„ฑ๋˜๊ฒŒ ๋˜๊ณ  ์‚ฌ์ „์— ์ž‘์„ฑํ•œ ๋ฐฐํฌ ์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ๊ณผ ํ•จ๊ป˜ AWS์˜ ์Šคํ† ๋ฆฌ์ง€ ์„œ๋น„์Šค์ธ S3์—๊ฒŒ ์ „๋‹ฌ๋˜์–ด ์ €์žฅ๋˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 
  2. S3์— ์„ฑ๊ณต์ ์œผ๋กœ ์ €์žฅ๋˜์—ˆ๋‹ค๋ฉด EC2์— ์„ค์น˜ํ•œ CodeDeply Agent๊ฐ€ S3์— ์ €์žฅ๋œ ํ”„๋กœ์ ํŠธ ํŒŒ์ผ์„ ๊ฐ€์ ธ์™€์„œ ๋‚ด๋ ค๋ฐ›์Šต๋‹ˆ๋‹ค.
  3. ๊ทธ ํ›„, ๋ฐฐํฌ ์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์„ ์ฝ์–ด ๋“ค์—ฌ์„œ ํ”„๋กœ์ ํŠธ ์‹คํ–‰์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.

์ฆ‰ ์Šคํฌ๋ฆฝํŠธ์˜ ์ฝ”๋“œ๋Š” ํ”„๋กœ์ ํŠธ๋ฅผ EC2์—์„œ ์‹คํ–‰ํ•  ๋•Œ ์ด์ „์— ์‹คํ–‰๋˜๊ณ  ์žˆ๋Š” Spring Boot ํ”„๋กœ์ ํŠธ๊ฐ€ ์žˆ๋‹ค๋ฉด ์ข…๋ฃŒ์‹œํ‚จ ๋’ค ์ƒˆ๋กœ์šด ๋ฒ„์ „์˜ ํ”„๋กœ์ ํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค. 

 

6. CD Actions S3 ์—…๋กœ๋“œ ์„ค์ •

์•„๊นŒ 1๋ฒˆ์—์„œ CI Actions๋ฅผ ์ถ”๊ฐ€ํ•ด์คฌ๊ธฐ ๋•Œ๋ฌธ์— ๋ณธ์ธ ํ”„๋กœ์ ํŠธ ๋ ˆํฌ์ง€ํ† ๋ฆฌ์˜ ./github/workflows ์— ์ ‘๊ทผํ•˜๋ฉด ์œ„์™€ ๊ฐ™์ด ๋‚˜ํƒ€๋‚  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

Add file ํ›„ Create new file ํด๋ฆญ

์ด์ œ CD.yml๋„ ์ž‘์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ Add file ํด๋ฆญ ํ›„ Create new file์„ ํด๋ฆญํ•˜์—ฌ ์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

# workflow์˜ ์ด๋ฆ„
name: CD

# ํ•ด๋‹น workflow๊ฐ€ ์–ธ์ œ ์‹คํ–‰๋  ๊ฒƒ์ธ์ง€์— ๋Œ€ํ•œ ํŠธ๋ฆฌ๊ฑฐ๋ฅผ ์ง€์ •
on:
  push:
    branches: [ main ] # main branch๋กœ push ๋  ๋•Œ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

# ํ•ด๋‹น yml ๋‚ด์—์„œ ์‚ฌ์šฉํ•  key - value
env:
  S3_BUCKET_NAME: project-bucket-v1
  PROJECT_NAME: cicdtest
  
# workflow๋Š” ํ•œ๊ฐœ ์ด์ƒ์˜ job์„ ๊ฐ€์ง€๋ฉฐ, ๊ฐ job์€ ์—ฌ๋Ÿฌ step์— ๋”ฐ๋ผ ๋‹จ๊ณ„๋ฅผ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
jobs:
  build:
    name: CD
    # ํ•ด๋‹น jobs์—์„œ ์•„๋ž˜์˜ steps๋“ค์ด ์–ด๋– ํ•œ ํ™˜๊ฒฝ์—์„œ ์‹คํ–‰๋  ๊ฒƒ์ธ์ง€๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
    runs-on: ubuntu-latest

    steps:
     # ์ž‘์—…์—์„œ ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ๋„๋ก $GITHUB_WORKSPACE์—์„œ ์ €์žฅ์†Œ๋ฅผ ์ฒดํฌ์•„์›ƒํ•ฉ๋‹ˆ๋‹ค.
      - uses: actions/checkout@v2
      - name: Set up JDK 11
        uses: actions/setup-java@v2
        with:
          java-version: '11'
          distribution: 'zulu'

      - name: Grant execute permission for gradlew
        run: chmod +x ./gradlew
        shell: bash
      
      - name: Build with Gradle
        run: ./gradlew build
        shell: bash
        
      - name: Make zip file
        run: zip -r ./$GITHUB_SHA.zip .
        shell: bash

      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: ${{ secrets.AWS_REGION }}
          
      # script files ๋ณต์‚ฌ
      - name: Copy script
        run: cp ./scripts/*.sh ./deploy
      
      # S3์— ์—…๋กœ๋“œ
      - name: Upload to S3
        run: aws s3 cp --region ap-northeast-2 ./$GITHUB_SHA.zip s3://$S3_BUCKET_NAME/$PROJECT_NAME/$GITHUB_SHA.zip

์œ„ ์ฝ”๋“œ์˜ env์˜ S3_BUCKET_NAME ๋ถ€๋ถ„์€ ๋ฐ˜๋“œ์‹œ ๋ณธ์ธ์˜ S3 ๋ฒ„ํ‚ท ์ด๋ฆ„์œผ๋กœ ์„ค์ •ํ•ด์ฃผ์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๋ณธ์ธ์ด 3๋ฒˆ์—์„œ ์ž‘์„ฑํ•œ S3์˜ ๋ฒ„ํ‚ท ์ด๋ฆ„์„ ์ž‘์„ฑํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

main branch์— push ํ–ˆ์„ ๊ฒฝ์šฐ

์ด์ œ ์ปค๋ฐ‹ ๋‚ด์—ญ์„ ์‚ดํŽด๋ณด๋ฉด CI์™€ CD๊ฐ€ ์ˆ˜ํ–‰ ์ค‘์ธ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๊ณ  ์„ฑ๊ณต์ ์ด๋ผ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฒดํฌํ‘œ์‹œ๋กœ ๋ณ€ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์•„์ง๊นŒ์ง€ CD๋Š” S3์— ์—…๋กœ๋“œํ•˜๋Š” ๊ณผ์ •๊นŒ์ง€๋งŒ ์ง„ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ S3์˜ ๋ฒ„ํ‚ท์— ์ ‘์†ํ•˜์—ฌ ๋ณธ์ธ์˜ ํ”„๋กœ์ ํŠธ๊ฐ€ ์„ฑ๊ณต์ ์œผ๋กœ ์—…๋กœ๋“œ๊ฐ€ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

S3์— ์„ฑ๊ณต์ ์œผ๋กœ ์—…๋กœ๋“œ ์™„๋ฃŒ

 

7. AWS CodeDeploy & AWS EC2 ์—ฐ๋™

7-1 AWS ์„ค์ •

์ด์ œ ๋ฐฐํฌ ์‹œ์Šคํ…œ์ธ codedeploy๋ฅผ ํ†ตํ•ด EC2 Instance์— ๋ฐฐํฌ๋ฅผ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๋Š” ์ž‘์—…์„ ์„ค์ •ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๋จผ์ € codedeploy์™€ EC2 ๋‘˜ ์‚ฌ์ด์˜ ์ ‘๊ทผ์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” AWS IAM์—์„œ '์—ญํ•  ๋งŒ๋“ค๊ธฐ'๋ฅผ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž ๋“ฑ๋ก์ด ์™ธ๋ถ€์—์„œ AWS ์™ธ๋ถ€์—์„œ๋ถ€ํ„ฐ์˜ ์ ‘๊ทผ์„ ํ—ˆ๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด๋ผ๋ฉด, ์—ญํ• ์€ AWS ๋‚ด์—์„œ์˜ ์ ‘๊ทผ ๊ถŒํ•œ์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ์œผ๋ฉฐ ๋“ฑ๋ก ์˜ˆ์‹œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • ๋‹ค๋ฅธ ๊ณ„์ •์˜ IAM ์‚ฌ์šฉ์ž
  • AWS ๋ฆฌ์†Œ์Šค์—์„œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ด์•ผ ํ•˜๋Š” EC2 ์ธ์Šคํ„ด์Šค์—์„œ ์‹คํ–‰ ์ค‘์ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ฝ”๋“œ
  • ๊ณ„์ • ๋‚ด ๋ฆฌ์†Œ์Šค์—์„œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜์—ฌ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ด์•ผ ํ•˜๋Š” AWS ์„œ๋น„์Šค
  • SAML์„ ํ†ตํ•ด ์ธ์ฆ ์—ฐ๋™์„ ์‚ฌ์šฉํ•˜๋Š” ์‚ฌ๋‚ด ๋””๋ ‰ํ† ๋ฆฌ์˜ ์‚ฌ์šฉ์ž

AWS IAM ์ ‘๊ทผ

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ IAM์— ์ ‘๊ทผํ•ฉ๋‹ˆ๋‹ค.

์—ญํ•  ์นดํ…Œ๊ณ ๋ฆฌ์— ๋“ค์–ด๊ฐ„ ๋’ค ์—ญํ•  ๋งŒ๋“ค๊ธฐ ํด๋ฆญ
AWS ์„œ๋น„์Šค ๋ฐ EC2 ์„ ํƒ
AmazonEC2RoleforAWSCodeDeploy ์ถ”๊ฐ€

AmazonEC2RoleforAWSCodeDeploy ๊ถŒํ•œ์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.

์—ญํ•  ์ด๋ฆ„ ์ž…๋ ฅ

์—ญํ•  ์ด๋ฆ„๊ณผ ์„ค๋ช… ์ •๋„๋งŒ ์ž‘์„ฑํ•œ ํ›„ ์—ญํ•  ์ƒ์„ฑ์„ ๋ˆŒ๋Ÿฌ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

EC2์—์„œ ํ•ด๋‹น ์—ญํ• ์„ ์‚ฌ์šฉํ•˜๋„๋ก ์ €์žฅ

EC2์—์„œ ๋ฐฉ๊ธˆ ์ž‘์„ฑํ•œ ์—ญํ• ์„ ์‚ฌ์šฉํ•˜๋„๋ก ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

์—ญํ• ์„ ๋ฐ”๊พธ๊ณ  ๋‚˜๋ฉด ์ธ์Šคํ„ด์Šค๋ฅผ ์žฌ๋ถ€ํŒ…ํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

EC2 ์žฌ๋ถ€ํŒ…

์ด๋•Œ ๊ผญ ์žฌ๋ถ€ํŒ…์„ ๋ˆ„๋ฅด์…”์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ข…๋ฃŒ๋Š” ์‚ญ์ œ์™€ ๋™์ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ง ๊ทธ๋Œ€๋กœ ์ข…๋ฃŒ๊ฐ€ ๋˜์–ด๋ฒ„๋ฆฌ๋ฉฐ ๋‹ค์‹œ ์‹คํ–‰ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

์žฌ๋ถ€ํŒ…์ด ์™„๋ฃŒ๋˜๋ฉด EC2 ์ธ์Šคํ„ด์Šค์— ์—ฐ๊ฒฐํ•œ ๋’ค ๋‹ค์Œ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด codedeploy๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.

aws s3 cp s3://aws-codedeploy-ap-northeast-2/latest/install . --region ap-northeast-2

download: s3://aws-codedeploy-ap-northeast-2/latest/install to ./install

๋‹ค์šด๋กœ๋“œ ์„ฑ๊ณต ์‹œ ์œ„์™€ ๊ฐ™์€ ๋ฉ”์‹œ์ง€๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. 

์ด์ œ ์‹คํ–‰ ๊ถŒํ•œ์„ ์ฃผ๊ณ  ์‹คํ–‰ํ•˜์—ฌ ์„ค์น˜๋ฅผ ๋งˆ๋ฌด๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค.

chmod +x ./install
sudo ./install auto

๋งŒ์•ฝ install ๋ถ€๋ถ„์—์„œ /usr/bin/env: ruby: No such file or directory๊ณผ ๊ฐ™์€ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ ruby๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†๋‹ค๋Š” ์˜๋ฏธ์ด๋ฏ€๋กœ ๋‹นํ™ฉํ•˜์ง€ ๋ง๊ณ  ์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ruby๋ฅผ ์„ค์น˜ ํ›„ ๋‹ค์‹œ ์ž…๋ ฅํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

sudo yum install ruby;

์„ค์น˜ ํ™•์ธ์„ ์œ„ํ•ด ์•„๋ž˜์˜ ๋ช…๋ น์–ด๋ฅผ ์ž…๋ ฅํ•˜๋ฉด The AWS CodeDeploy agent is running as PID **** ๋ฉ”์‹œ์ง€๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.

sudo service codedeploy-agent status

์ •์ƒ์ ์œผ๋กœ ์„ค์น˜ ๋˜์—ˆ์„ ๊ฒฝ์šฐ

 

7-2 CodeDeploy ์„ค์ •

EC2์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, CodeDeploy์—์„œ๋„ ์—ญํ• ์„ ์ƒ์„ฑํ•˜๊ณ  ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

CodeDeploy ์„œ๋น„์Šค ์„ ํƒ

7-1๊ณผ ๋™์ผํ•˜๊ฒŒ ์—ญํ•  ์ƒ์„ฑ์— ๋“ค์–ด๊ฐ€์„œ AWS ์„œ๋น„์Šค ์„ ํƒ ํ›„ ์‚ฌ์šฉ ์‚ฌ๋ก€๋Š” CodeDeploy๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.

์—ญํ•  ์ด๋ฆ„ ์ง€์ •

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ด๋ฆ„๋„ ์ž์œ ๋กญ๊ฒŒ ๋ถ€์—ฌํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ด์ œ codedeploy ์„œ๋น„์Šค์—์„œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

๋Œ€์‹œ๋ณด๋“œ

๋Œ€์‹œ๋ณด๋“œ์—์„œ CodeDeploy์— ์ ‘๊ทผํ•ฉ๋‹ˆ๋‹ค.

CodeDeploy ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ƒ์„ฑ
์ด๋ฆ„ ๋ฐ ์ปดํ“จํŒ… ํ”Œ๋žซํผ ์„ ํƒ

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์ƒ์„ฑํ•˜๊ณ , ๋ฐฐํฌ ๊ทธ๋ฃน ์ƒ์„ฑ์„ ์ด์–ด์„œ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

๋ฐฐํฌ ๊ทธ๋ฃน ์ƒ์„ฑ
์ด๋ฆ„ ๋ฐ ์„œ๋น„์Šค ์—ญํ•  ์ž…๋ ฅ

์„œ๋น„์Šค ์—ญํ• ์€ ์•„๊นŒ ๋งŒ๋“ค์—ˆ๋˜ ์—ญํ• ์„ ์„ ํƒํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

ํ™˜๊ฒฝ ๊ตฌ์„ฑ ์„ค์ •

ํ™˜๊ฒฝ ๊ตฌ์„ฑ์€ Amazone EC2 ์ธ์Šคํ„ด์Šค๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.

๋ฐฐํฌ ๊ตฌ์„ฑ ๋ฐ ๋กœ๋“œ ๋ฐธ๋Ÿฐ์„œ ์„ค์ •

๋ฐฐํฌ ๊ตฌ์„ฑ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด CodeDeployDefault.AllAtOnce๋ฅผ ์„ ํƒํ•˜๊ณ  ๋กœ๋“œ ๋ฐธ๋Ÿฐ์„œ๋Š” ์ €๋Š” ํ™œ์„ฑํ™”ํ•˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.

์ด์ œ ํ”„๋กœ์ ํŠธ ํด๋”์— appspec.yml ํŒŒ์ผ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด ํŒŒ์ผ์€ codedeploy๊ฐ€ ์„œ๋ฒ„ ํ™˜๊ฒฝ์— ์„ค์น˜๋ฅผ ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋™์ž‘์„ ์ •์˜ํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

appspec.yml ์ƒ์„ฑ

version: 0.0
os: linux
# S3์— ์žˆ๋Š” zip ํŒŒ์ผ์ด EC2์— ๋ฐฐํฌ๋  ์œ„์น˜๋ฅผ ์ง€์ •
files:
  - source: / # CodeDeploy์—์„œ ์ „๋‹ฌํ•ด ์ค€ ํŒŒ์ผ ์ค‘ destination์œผ๋กœ ์ด๋™์‹œํ‚ฌ ๋Œ€์ƒ์„ ๋ฃจํŠธ๋กœ ์ง€์ •(์ „์ฒดํŒŒ์ผ)
    destination: /home/ec2-user/action/ # source์—์„œ ์ง€์ •๋œ ํŒŒ์ผ์„ ๋ฐ›์„ ์œ„์น˜, ์ดํ›„ jar๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋“ฑ์€ destination์—์„œ ์˜ฎ๊ธด ํŒŒ์ผ๋“ค๋กœ ์ง„ํ–‰
    overwrite: yes

permissions: # CodeDeploy์—์„œ EC2์„œ๋ฒ„๋กœ ๋„˜๊ฒจ์ค€ ํŒŒ์ผ๋“ค์„ ๋ชจ๋‘ ec2-user๊ถŒํ•œ์„ ๊ฐ–๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.
  - object: /
    pattern: "**"
    owner: ec2-user
    group: ec2-user

# ApplicationStart ๋‹จ๊ณ„์—์„œ deploy.sh๋ฅผ ์‹คํ–‰์‹œํ‚ค๋„๋ก ํ•ฉ
hooks: # CodeDeploy๋ฐฐํฌ ๋‹จ๊ณ„์—์„œ ์‹คํ–‰ํ•  ๋ช…๋ น์–ด๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
  ApplicationStart: # deploy.sh๋ฅผ ec2-user๊ถŒํ•œ์œผ๋กœ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.
    - location: scripts/deploy.sh
      timeout: 60 # ์Šคํฌ๋ฆฝํŠธ ์‹คํ–‰ 60์ดˆ ์ด์ƒ ์ˆ˜ํ–‰๋˜๋ฉด ์‹คํŒจ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.
      runas: ec2-user

 

 

8. CD Actions Deploy ์ž‘์„ฑ

์ด์ œ ์ง„์งœ ๋๋‚ฌ์Šต๋‹ˆ๋‹ค. ์•„๊นŒ ์ž‘์„ฑํ–ˆ๋˜ CD.yml์— ์•„๋ž˜์˜ Deploy ์ฝ”๋“œ๋งŒ ์ถ”๊ฐ€ํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

     # Deploy
      - name: Deploy
        run: |
          aws deploy create-deployment \
          --application-name cicdtest-project \
          --deployment-config-name CodeDeployDefault.AllAtOnce \
          --deployment-group-name cicd-test-prod \
          --file-exists-behavior OVERWRITE \
          --s3-location bucket=project-bucket-v1,bundleType=zip,key=cicdtest/$GITHUB_SHA.zip \
          --region ap-northeast-2 \

์—ฌ๊ธฐ์„œ ์ด ๊ธ€์„ ๋ณด์‹œ๋Š” ๋ถ„๋“ค์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ถ€๋ถ„์„ ๋ณธ์ธ์ด ์„ค์ •ํ–ˆ๋˜ ์ด๋ฆ„๋“ค๋กœ ๋ณ€๊ฒฝํ•ด์ฃผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

  • application-name
  • deployment-group-name
  • s3-location bucket
  • key

์—ฌ๊ธฐ์„œ key๋Š” ๋ฒ„ํ‚ท ์•ˆ์—์„œ ์ €์žฅํ•˜๊ณ ์žํ•˜๋Š” ๊ฒฝ๋กœ์ž…๋‹ˆ๋‹ค. ์ฆ‰, s3 ํŒŒ์ผ/๋””๋ ‰ํ† ๋ฆฌ ๋ช…์ž…๋‹ˆ๋‹ค.

์ „์ฒด ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.


# workflow์˜ ์ด๋ฆ„
name: CD

# ํ•ด๋‹น workflow๊ฐ€ ์–ธ์ œ ์‹คํ–‰๋  ๊ฒƒ์ธ์ง€์— ๋Œ€ํ•œ ํŠธ๋ฆฌ๊ฑฐ๋ฅผ ์ง€์ •
on:
  push:
    branches: [ main ] # main branch๋กœ push ๋  ๋•Œ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

# ํ•ด๋‹น yml ๋‚ด์—์„œ ์‚ฌ์šฉํ•  key - value
env:
  S3_BUCKET_NAME: project-bucket-v1
  PROJECT_NAME: cicdtest
  
# workflow๋Š” ํ•œ๊ฐœ ์ด์ƒ์˜ job์„ ๊ฐ€์ง€๋ฉฐ, ๊ฐ job์€ ์—ฌ๋Ÿฌ step์— ๋”ฐ๋ผ ๋‹จ๊ณ„๋ฅผ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
jobs:
  build:
    name: CD
    # ํ•ด๋‹น jobs์—์„œ ์•„๋ž˜์˜ steps๋“ค์ด ์–ด๋– ํ•œ ํ™˜๊ฒฝ์—์„œ ์‹คํ–‰๋  ๊ฒƒ์ธ์ง€๋ฅผ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
    runs-on: ubuntu-latest

    steps:
    # ์ž‘์—…์—์„œ ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ๋„๋ก $GITHUB_WORKSPACE์—์„œ ์ €์žฅ์†Œ๋ฅผ ์ฒดํฌ์•„์›ƒํ•ฉ๋‹ˆ๋‹ค.
      - uses: actions/checkout@v2
      - name: Set up JDK 11
        uses: actions/setup-java@v2
        with:
          java-version: '11'
          distribution: 'zulu'
          
      - name: Grant execute permission for gradlew
        run: chmod +x ./gradlew
        shell: bash
      
      - name: Build with Gradle
        run: ./gradlew build
        shell: bash
        
      - name: Make zip file
        run: zip -r ./$GITHUB_SHA.zip .
        shell: bash

      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: ${{ secrets.AWS_REGION }}
          
      # script files ๋ณต์‚ฌ
      - name: Copy script
        run: cp ./scripts/*.sh ./deploy
      
      # S3์— ์—…๋กœ๋“œ
      - name: Upload to S3
        run: aws s3 cp --region ap-northeast-2 ./$GITHUB_SHA.zip s3://$S3_BUCKET_NAME/$PROJECT_NAME/$GITHUB_SHA.zip
        
     # Deploy
      - name: Deploy
        run: |
          aws deploy create-deployment \
          --application-name cicdtest-project \
          --deployment-config-name CodeDeployDefault.AllAtOnce \
          --deployment-group-name cicd-test-prod \
          --file-exists-behavior OVERWRITE \
          --s3-location bucket=project-bucket-v1,bundleType=zip,key=cicdtest/$GITHUB_SHA.zip \
          --region ap-northeast-2 \

 

๊ฒฐ๋ก 

 

https://velog.io/@bluewind8791/Github-Actions

main branch์— pull request๋ฅผ ํ•˜๊ฒŒ ๋˜๋ฉด CI์— ์˜ํ•ด ๋นŒ๋“œ ๊ฒ€์‚ฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋˜๊ณ  push๋ฅผ ํ•˜๊ฒŒ ๋˜๋ฉด ์ž๋™์ ์œผ๋กœ ๋ฐฐํฌ๊ฐ€ ๋˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์ถ”๊ฐ€์ ์œผ๋กœ aws-codeDeploy-๋ฐฐํฌ-๋ฐฐํฌID-View events์— ๋“ค์–ด๊ฐ€๋ฉด ๋ฐฐํฌ ๊ณผ์ •์— ์ด์ƒ์ด ์—†๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์•„๋ฌด ์ด์ƒ์ด ์—†๋Š”๋ฐ ํ”„๋กœ์ ํŠธ๊ฐ€ ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค๋ฉด EC2์— java๊ฐ€ ์„ค์น˜๋˜์–ด์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.

๋‹ค์Œ์œผ๋กœ๋Š” Nginx+Cerbot/SSL์„ ์‚ฌ์šฉํ•œ https ์„ค์ •๊ณผ ๋ฌด์ค‘๋‹จ ๋ฐฐํฌ ์„ค์ • ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์ž‘์„ฑํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.