๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
DevOps

GitHub Action์„ ํ†ตํ•œ CI ๊ตฌ์ถ•ํ•˜๊ธฐ (SpringBoot + Maven)

by ์ฃผ๋ฐœ2 2021. 5. 14.
๋ฐ˜์‘ํ˜•

 ์•ˆ๋…•ํ•˜์„ธ์š”~ ์ด์ „์— ์šด์˜ํ•˜๋˜ ๋ธ”๋กœ๊ทธ ๋ฐ GitHub, ๊ณต๋ถ€ ๋‚ด์šฉ์„ ์ •๋ฆฌํ•˜๋Š” Study-GitHub ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค!

 ๋„ค์ด๋ฒ„ ๋ธ”๋กœ๊ทธ

 GitHub

Study-GitHub

 ๐Ÿ”


 

๐Ÿ“Ž GitHub Action์„ ํ†ตํ•œ Build ์ž๋™ํ™” (SpringBoot + Maven)

์•ˆ๋…•ํ•˜์„ธ์š”! ์ด๋ฒˆ์— ์ •๋ฆฌํ•  ๋‚ด์šฉ์€ GitHub Action์„ ํ†ตํ•ด ํ”„๋กœ์ ํŠธ๋ฅผ ์ž๋™์œผ๋กœ Build ํ•˜๋„๋ก ์„ค์ •ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

ํ˜„์žฌ AWS EC2์— Spring Boot ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์‹คํ–‰๋˜๊ณ  ์žˆ๋Š”๋ฐ์š”, ๋ฐฐํฌํ•˜๊ธฐ๊นŒ์ง€์˜ ๊ณผ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • ๋กœ์ปฌ์—์„œ ์ฝ”๋“œ ์ˆ˜์ • -> Commit -> GitHub PR -> Merge
  • AWS์—์„œ ์ˆ˜์ •๋œ ์ฝ”๋“œ pull -> ์‹คํ–‰์ค‘์ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ •์ง€(kill) -> ๋ฉ”์ด๋ธ ๋นŒ๋“œ -> ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹คํ–‰

 

์‹œ๊ฐ„์ด ์—„์ฒญ ์žก์•„๋จน๋Š” ๋‚ด์šฉ์€ ์•„๋‹ˆ์ง€๋งŒ, ๊ทธ๋ž˜๋„ ํ•œ ๋ฒˆ ๋ฐฐํฌํ• ๋•Œ๊นŒ์ง€ ๊ต‰์žฅํžˆ ๋งŽ์€ ์ž‘์—…์ด ์ด๋ฃจ์–ด์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

๋˜ํ•œ ์ง์ ‘ ์†์œผ๋กœ ์ž‘์—…์„ ํ•œ๋‹ค๋ฉด ์‹ค์ˆ˜ํ•  ์—ฌ์ง€๋„ ์ƒ๊ธฐ๊ณ , ๋ฐฐํฌ๋ฅผ ๋งŽ์ดํ•˜๋ฉด ํ• ์ˆ˜๋ก ๋” ๋งŽ์ด ์‹œ๊ฐ„์„ ์žก์•„๋จน๊ฒ ์ฃ ?

 

๋”ฐ๋ผ์„œ ์ด๋Ÿฌํ•œ ๊ณผ์ •์„ ํ•œ ๋ฒˆ ์‹œ๊ฐ„์„ ๋“ค์—ฌ ์ž๋™ํ™”๋ฅผ ์ง„ํ–‰ํ•ด๋†“์œผ๋ฉด, ์ดํ›„ ๋ฐ˜๋ณต์ ์ธ ์ž‘์—…์€ ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค!!

์ฆ‰, ์ด์™€ ๊ฐ™์€ ๊ณผ์ •์„ CI(Continuous Integration) - ์ง€์†์  ํ†ตํ•ฉ , CD(Continuous Delivery) - ์ง€์†์  ์ „๋‹ฌ

์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

 

์ƒˆ๋กœ ๊ฐœ๋ฐœํ•œ ๊ธฐ๋Šฅ, ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง ๋“ฑ์„ ์‹ค์ œ ์„œ๋น„์Šค์— ๋ฐ˜์˜ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์œ„์™€ ๊ฐ™์€ ๊ณผ์ •์ธ ๋จธ์ง€, ํ…Œ์ŠคํŠธ, ๋นŒ๋“œ, ๋ฐฐํฌ ๋“ฑ๋“ฑ์˜ ์ž‘์—…์ด ํ•„์š”ํ•œ๋ฐ ์ด๋Ÿฌํ•œ ์ž‘์—…์„ ์ž๋™ํ™”ํ•˜๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

 

CircleCI, TravisCI, Jenkins ๋“ฑ๋“ฑ ์—ฌ๋Ÿฌ ์œ ๋ช…ํ•œ CI / CD ํˆด์ด ์กด์žฌํ•˜๋Š”๋ฐ์š”, ์ €๋Š” GitHub Action์„ ํ†ตํ•ด ์ง„ํ–‰ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

GitHub Action์€ ๊นƒํ—ˆ๋ธŒ์—์„œ ๊ณต์‹์ ์œผ๋กœ ์ œ๊ณตํ•˜๋Š” CI / CD ํˆด์ธ๋ฐ ์ €๋Š” ์‚ฌ์šฉํ•ด๋ณด์ง€ ์•Š์•˜๊ณ , ๊นƒํ—ˆ๋ธŒ์—์„œ ์ผ์›ํ™”๋œ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์œผ๋กœ ๋ชจ๋“  ๊ณผ์ •์„ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ํŽธ๋ฆฌํ•จ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— GitHub Action์„ ์„ ํƒํ–ˆ์Šต๋‹ˆ๋‹ค!

 

(์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ ์ง„ํ–‰ํ•  ๋‚ด์šฉ์€ GitHub์— ์ด๋ฒคํŠธ ๋ฐœ์ƒ ์‹œ(Push & PR) ์ž๋™์œผ๋กœ Build๋ฅผ ์ง„ํ–‰ํ•˜๋Š” ๊ณผ์ •์ž…๋‹ˆ๋‹ค.)


 

๐ŸŽฏ GitHub Action yml ํŒŒ์ผ ์ƒ์„ฑํ•˜๊ธฐ

์ €๋Š” Spring Boot + Maven + IntelliJ ํ™˜๊ฒฝ์—์„œ ์ž‘์—…์„ ์ง„ํ–‰ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

yml ํŒŒ์ผ์€ GitHub์˜ Actions ํƒญ์—์„œ ์ง์ ‘ ์ƒ์„ฑ์„ ํ•  ์ˆ˜ ์žˆ๊ณ , ์ง์ ‘ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด๋„ ๋ฉ๋‹ˆ๋‹ค.

 

์—ฐ๋™ํ•  GitHub ํ”„๋กœ์ ํŠธ์—์„œ Actions ํƒญ์œผ๋กœ ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค.

 

 

Java with Maven ์„ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค. (Set up this workflow)

 

 

๊ฒฝ๋กœ๋Š” ๋ณด์‹œ๋ฉด ํ”„๋กœ์ ํŠธ > .github/workflows/ymlํŒŒ์ผ 

ํ•ญ์ƒ ์œ„์™€ ๊ฐ™์€ ๊ตฌ์กฐ๊ฐ€ ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

yml ํŒŒ์ผ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ตฌ์กฐ๋กœ ์ž‘์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

 

on: 

  • ์ด๋ฒคํŠธ๋ฅผ ๊ฐ์ง€ํ•˜๋ฉฐ ์›Œํฌํ”Œ๋กœ์šฐ๋ฅผ ์‹คํ–‰์‹œํ‚ค๋Š” ์กฐ๊ฑด
  • ์œ„ ํŒŒ์ผ์€ main ๋ธŒ๋žœ์น˜๋กœ push ๋ฐ pr(pull_request)์˜ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•  ๋•Œ ์‹คํ–‰์ด ๋ฉ๋‹ˆ๋‹ค.

 

jobs:

  • ํ•˜๋‚˜์˜ ์ธ์Šคํ„ด์Šค์—์„œ ์—ฌ๋Ÿฌ Step(์ปดํŒŒ์ผ, ํ…Œ์ŠคํŠธ, ๋นŒ๋“œ, ๋ฐฐํฌ ๋“ฑ๋“ฑ)์„ ๊ทธ๋ฃนํ™”์‹œ์ผœ ์‹คํ–‰ํ•˜๋Š” ์—ญํ• ์œผ๋กœ, ๋ณ‘๋ ฌ๋กœ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.
  • runs-on: GitHub Action ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์ž‘๋™๋  OS ํ™˜๊ฒฝ์„ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.
  • uses: actions/checkout@v2: ํ”„๋กœ์ ํŠธ ์ฝ”๋“œ๋กœ checkout์„ ํ•ฉ๋‹ˆ๋‹ค.
  • uses: actions/setup-java@v2: GitHub Action์ด ์‹คํ–‰๋  OS์— Java๋ฅผ ์„ค์น˜ํ•ฉ๋‹ˆ๋‹ค.
  • steps: ํ•˜์œ„ ๋ถ€๋ถ„์€ ํ•ด๋‹น steps์— ๋Œ€ํ•œ ์†์„ฑ์ž…๋‹ˆ๋‹ค.

๋จผ์ € ์œ„์™€ ๊ฐ™์ด ์„ค์ •์„ํ•˜๊ณ  main ๋ธŒ๋žœ์น˜์— push๋‚˜ pr์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„๋•Œ, ์ •์ƒ์ ์œผ๋กœ build๊ฐ€ ๋˜๋Š”์ง€ ํ…Œ์ŠคํŠธํ•ด๋ด…์‹œ๋‹ค.

 

 

 

 

๐ŸŽฏ GitHub Action์„ ํ†ตํ•ด ๋นŒ๋“œํ•˜๊ธฐ

์œ„ ์ž‘์„ฑ์ด ๋๋‚˜๋ฉด push ํ˜น์€ PR์„ ํ†ตํ•ด ๋นŒ๋“œ๋ฅผ ํ™•์ธํ•ด๋ด…๋‹ˆ๋‹ค.

๊ฐ„๋‹จํ•œ ์ž‘์—…์„ ์ง„ํ–‰ํ•œ ํ›„ main ๋ธŒ๋žœ์น˜๋กœ PR์„ ์š”์ฒญํ•˜๋ฉด ์œ„์™€ ๊ฐ™์ด GitHub Action ์ด ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.

 

 

ํ˜„์žฌ Actions ์ด ์ง„ํ–‰์ค‘์ด๋ฉฐ, ์ž‘์—… ์™„๋ฃŒ ์‹œ ์•„๋ž˜์™€ ๊ฐ™์€ ํ‘œ์‹œ๊ฐ€ ๋‚˜ํƒ€๋‚˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

workflow์˜ ๋กœ๊ทธ๋ฅผ ํ™•์ธํ•˜๊ณ  ์‹ถ์œผ๋ฉด ํ•ด๋‹น build๋ฅผ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค.

 

 

๊ทธ๋Ÿผ ์œ„์™€ ๊ฐ™์ด ๊ฐ job ๋ณ„๋กœ ๋กœ๊ทธ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

 

 

๐ŸŽฏ GitHub Action์— Time ๋กœ๊ทธ ์ถ”๊ฐ€ํ•˜๊ธฐ

yml ํŒŒ์ผ์— build ์‹œ์ ์˜ ํ˜„์žฌ ์‹œ๊ฐ„์„ ์ถœ๋ ฅํ•˜๋Š” job์„ ์ถ”๊ฐ€ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

      - name: Get Current Time
        uses: 1466587594/get-current-time@v2
        id: current-time
        with:
          format: YYYY-MM-DDTHH:mm:ss
          utcOffset: "+09:00" # ๊ธฐ์ค€์ด UTC์ด๊ธฐ ๋•Œ๋ฌธ์— ํ•œ๊ตญ์‹œ๊ฐ„์ธ KST๋ฅผ ๋งž์ถ”๊ธฐ ์œ„ํ•ด +9์‹œ๊ฐ„ ์ถ”๊ฐ€

      - name: Print Current Time
        run: echo "Current Time=${{steps.current-time.outputs.formattedTime}}" # current-time ์—์„œ ์ง€์ •ํ•œ ํฌ๋งท๋Œ€๋กœ ํ˜„์žฌ ์‹œ๊ฐ„ ์ถœ๋ ฅ
        shell: bash

์œ„ ์ฝ”๋“œ๋ฅผ yml ํŒŒ์ผ์— ํฌํ•จํ•œ ๋’ค ์ „์ฒด ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

 

# This workflow will build a Java project with Maven
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven

name: Oneline-GitHub Action

on: # ์ด๋ฒคํŠธ ๊ฐ์ง€, ์›Œํฌํ”Œ๋กœ์šฐ๋ฅผ ์‹คํ–‰์‹œํ‚ค๋Š” ์กฐ๊ฑด ์„ค์ •
  push:
    branches: [ main ] # push ๋˜๋ฉด ์‹คํ–‰๋  ๋ธŒ๋žœ์น˜ ์„ ํƒ(main)
  pull_request:
    branches: [ main ] # pull_request(PR) ๋˜๋ฉด ์‹คํ–‰๋  ๋ธŒ๋žœ์น˜ ์„ ํƒ(main)

jobs: # job - ํ•˜๋‚˜์˜ ์ธ์Šคํ„ด์Šค์—์„œ ์—ฌ๋Ÿฌ Step์„ ๊ทธ๋ฃน์‹œ์ผœ ์‹คํ–‰ํ•˜๋Š” ์—ญํ• , ๋ณ‘๋ ฌ๋กœ ์‹คํ–‰
  build:
    runs-on: ubuntu-latest

    steps: # ์ˆœ์ฐจ์ ์œผ๋กœ ๋ช…๋ น์–ด ์ˆ˜ํ–‰
      - name: Checkout
        uses: actions/checkout@v2

      - name: Set up JDK 11
        uses: actions/setup-java@v2
        with:
          java-version: '11'
          distribution: 'adopt'

      - name: Build with Maven
        run: mvn -B package --file back/pom.xml

      - name: Get Current Time
        uses: 1466587594/get-current-time@v2
        id: current-time
        with:
          format: YYYY-MM-DDTHH:mm:ss
          utcOffset: "+09:00" # ๊ธฐ์ค€์ด UTC์ด๊ธฐ ๋•Œ๋ฌธ์— ํ•œ๊ตญ์‹œ๊ฐ„์ธ KST๋ฅผ ๋งž์ถ”๊ธฐ ์œ„ํ•ด +9์‹œ๊ฐ„ ์ถ”๊ฐ€

      - name: Print Current Time
        run: echo "Current Time=${{steps.current-time.outputs.formattedTime}}" # current-time ์—์„œ ์ง€์ •ํ•œ ํฌ๋งท๋Œ€๋กœ ํ˜„์žฌ ์‹œ๊ฐ„ ์ถœ๋ ฅ
        shell: bash

 

์œ„ ๊ณผ์ •์„ ๋๋‚ธ ํ›„ push๋ฅผ ์ง„ํ–‰ํ•˜๊ณ  GitHub Action์„ ํ™•์ธํ•˜๋ฉด ์œ„์™€ ๊ฐ™์ด ๋นŒ๋“œ์‹œ์ ์˜ ์‹œ๊ฐ„์ด ์ถœ๋ ฅ๋˜๋Š”๊ฑธ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

 

 

๐ŸŽฏ GitHub Action์— cache ์ ์šฉํ•˜๊ธฐ

GitHub Action์—์„œ workflows ์‹œ๊ฐ„์„ ๋‹จ์ถ•์‹œํ‚ค๊ธฐ ์œ„ํ•ด cache๋ฅผ ์ ์šฉํ•ด ๋ด…์‹œ๋‹ค.

์ž์„ธํ•œ ๋‚ด์šฉ์€ ์•„๋ž˜ ์‚ฌ์ดํŠธ๋ฅผ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”!

https://docs.github.com/en/actions/guides/caching-dependencies-to-speed-up-workflows

 

      # Caching dependencies
      - name: Cache Maven packages
        uses: actions/cache@v2
        with:
          path: ~/.m2
          key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
          restore-keys: ${{ runner.os }}-m2

yml ํŒŒ์ผ์—์„œ build ๋ฐ”๋กœ ์œ„์— ์œ„ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

 

์œ„์™€ ๊ฐ™์ด ์ ์šฉํ•˜๊ณ  ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ•ด๋ด…๋‹ˆ๋‹ค.

 

 

์บ์‹œ๋ฅผ ์ ์šฉํ•˜๊ธฐ ์ „์—๋Š” build ์‹œ๊ฐ„์ด ํ‰๊ท  70 ~ 80์ดˆ์ •๋„ ๊ฑธ๋ ธ๋‹ค๋ฉด,

์บ์‹œ๋ฅผ ์ ์šฉํ•œ ํ›„์—๋Š” ํ‰๊ท  50 ~ 60์ดˆ์ •๋„์˜ ์‹œ๊ฐ„์ด ์†Œ์š”๋ฉ๋‹ˆ๋‹ค.

๊ฒฐ๋ก ์ ์œผ๋กœ 20~30% ์ •๋„์˜ ์‹œ๊ฐ„์ด ๋‹จ์ถ•๋˜์—ˆ๋Š”๋ฐ์š”, ๊ฝค ์ ์ ํ•œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค ๐Ÿ

 

 

 

 

 

References

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€