๐Ÿงฐ DevOps

CircleCI๋กœ SpringBoot+Postgresql ํ”„๋กœ์ ํŠธ ๋นŒ๋“œ ๋ฐฐํฌํ•˜๊ธฐ(3)

์—ฐ_์šฐ๋ฆฌ 2022. 3. 18. 17:56
๋ฐ˜์‘ํ˜•

CircleCI๋กœ SpringBoot+Postgresql ํ”„๋กœ์ ํŠธ ๋นŒ๋“œ ๋ฐฐํฌํ•˜๊ธฐ(1)

๋นŒ๋“œ ๋ฐฐํฌ ํ๋ฆ„
์‚ฌ์ „์ค€๋น„
AWS IAM ์—ญํ• (Role) ์„ค์ •
     EC2CodeDeployRole ์ƒ์„ฑ
     CodeDeployRole ์ƒ์„ฑ
AWS EC2 IAM ์—ญํ•  ์ˆ˜์ •
AWS IAM ์‚ฌ์šฉ์ž ์ถ”๊ฐ€

CircleCI๋กœ SpringBoot+Postgresql ํ”„๋กœ์ ํŠธ ๋นŒ๋“œ ๋ฐฐํฌํ•˜๊ธฐ(2)

AWS S3 ๋ฒ„ํ‚ท ์ƒ์„ฑ
AWS CodeDeploy ์ƒ์„ฑ
AWS EC2 ์ ‘์†ํ•˜์—ฌ CodeDeploy Agent ์„ค์น˜
AWS EC2 Postgresql ์„ค์น˜

CircleCI๋กœ SpringBoot+Postgresql ํ”„๋กœ์ ํŠธ ๋นŒ๋“œ ๋ฐฐํฌํ•˜๊ธฐ(3)

์†Œ์Šค์ฝ”๋“œ ์ˆ˜์ •
     .circleci > config.ymlํŒŒ์ผ
     ํ”„๋กœ์ ํŠธํด๋” > appspec.yml
     ํ”„๋กœ์ ํŠธํด๋” > scripts > deploy.sh
     ํ”„๋กœ์ ํŠธํด๋” > src > main > resources > application.yml
CircleCI Context์„ค์ •
     AWS ํ™˜๊ฒฝ๋ณ€์ˆ˜ ์ €์žฅ
     ํ”„๋กœ์ ํŠธ ํ™˜๊ฒฝ๋ณ€์ˆ˜ ์ €์žฅ
๊นƒํ—ˆ๋ธŒ ์†Œ์Šค์ฝ”๋“œ pushํ•˜์—ฌ ๋นŒ๋“œ ๋ฐฐํฌ ํ™•์ธ

 

ํŒ€ํ”Œํ•˜๋ฉด์„œ ๊ณต๋ถ€ํ•œ ๋‚ด์šฉ์ด๋‹ค (์‚ฌ์‹ค ๊ณต๋ถ€๋„ ์•„๋‹ˆ๊ณ  ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ•˜๋Š”์ง€๋งŒ ์ฐ์–ด๋จน์–ด๋ณธ ์ •๋„..)
์ž˜ ์ดํ•ดํ•˜์ง€ ๋ชปํ•œ ๋ถ€๋ถ„๋„ ๋งŽ๊ณ  ์—„์ฒญ ํ—ค๋งค์—ˆ๋‹ค. ์ผ๋‹จ ์–ด๋–ป๊ฒŒ ๋™์ž‘๋˜์—ˆ๋Š”์ง€ ๋‚จ๊ฒจ๋†“๋Š” ์šฉ๋„๋กœ ์ž‘์„ฑํ•˜๋Š” ๊ธ€์ด๋‹ค

 

 

 

 

 

 

 

์†Œ์Šค์ฝ”๋“œ ์ˆ˜์ •

.circleci > config.ymlํŒŒ์ผ

version: 2.1
orbs:
  aws-s3: circleci/aws-s3@1.0.13
  aws-code-deploy: circleci/aws-code-deploy@0.0.12

jobs:
  build:
    docker:
      - image: circleci/openjdk:11-jdk
        environment:
          DATABASE_URL: ${MY_POSTGRES_URL} #CircleCI Project Settings > Environment์˜ ๋‚ด์šฉ

    steps:
      - checkout  
      - run:
          name: gradlew ๊ถŒํ•œ ๋ถ€์—ฌ
          command: chmod +x ./gradlew
      - run:
          name: gradle ๋นŒ๋“œ
          command: ./gradlew build
      - run:
          name: ์†Œ์Šค ์••์ถ•
          command: |
            zip back.zip -r ./* 	#ํ˜„์žฌ ํด๋”์˜ ํ•˜์œ„๋‚ด์šฉ๊นŒ์ง€ ํฌํ•จํ•˜์—ฌ back.zipํŒŒ์ผ ์ƒ์„ฑ
            mkdir -p deploy	
            mv back.zip deploy/back.zip		#back.zipํŒŒ์ผ deployํด๋”๋กœ ์ด๋™
            
      - persist_to_workspace:	#build์™€ deploy ๋‹จ๊ณ„๊ฐ€ ๋‚˜๋‰˜์–ด์กŒ๊ธฐ๋•Œ๋ฌธ์— ์••์ถ•ํ•œ ํŒŒ์ผ์ด deploy๋‹จ๊ณ„์—์„œ ์‚ฌ๋ผ์งˆ ์ˆ˜ ์žˆ๋‹ค
          root: .
          paths: deploy		#deploy ํด๋”๋Š” ๊ทธ๋Œ€๋กœ ์œ ์ง€ํ•˜๊ฒ ๋‹ค

  deploy:
    docker:
      - image: circleci/python:2.7

    steps:
      - attach_workspace:	#์ €์žฅํ•œ ํด๋” ๊ฐ€์ ธ์˜ค๊ธฐ
          at: .

      - aws-s3/copy:
          from: deploy/*					#deploy ๋‚ด๋ถ€์˜ ๋ชจ๋“  ํŒŒ์ผ์„ aws s3๋กœ ๋ณด๋‚ธ๋‹ค
          to: 's3://ec2-s3-mybucket'		#s3://์ƒ์„ฑํ•œ ๋ฒ„ํ‚ท๋ช…
          aws-region: AWS_DEFAULT_REGION	#CircleCI์— ์„ค์ •ํ•œ Context์˜ ๋‚ด์šฉ์ด ๋“ค์–ด๊ฐˆ๊ฒƒ์ด๋‹ค

      - aws-code-deploy/deploy-bundle:
          application-name: EC2CodeDeployTest			#CodeDeploy ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ด๋ฆ„ ์ž‘์„ฑ
          deployment-group: EC2CodeDeployTestGroup		#EC2CodeDeployTest๊ฐ€ ์†ํ•œ ๋ฐฐํฌ๊ทธ๋ฃน๋ช… ์ž‘์„ฑ
          deployment-config: CodeDeployDefault.AllAtOnce	#EC2CodeDeployTestGroup์˜ ๋ฐฐํฌ๊ทธ๋ฃน์„ธ๋ถ€์ •๋ณด์˜ ๋ฐฐํฌ๊ตฌ์„ฑ์˜ ๊ฐ’ ์ž‘์„ฑ
          bundle-bucket: ec2-s3-mybucket			#s3://์ƒ์„ฑํ•œ ๋ฒ„ํ‚ท๋ช…
          bundle-key: back 					#๋ณด๋‚ด๋Š” ํŒŒ์ผ ์ด๋ฆ„
          bundle-type: zip 					#๋ณด๋‚ด๋Š” ํŒŒ์ผ ํ™•์žฅ์ž


workflows:
  version: 2.1

  build-deploy:
    jobs:
      - build
      - deploy:
          context: test #CircleCI์— ์„ค์ •ํ•œ Context ์ด๋ฆ„์„ ์ž‘์„ฑํ•œ๋‹ค (aws ๊ณ„์ •์ •๋ณด ๋“ค์–ด์žˆ๋Š”)
          requires:
            - build

 

 

ํ”„๋กœ์ ํŠธํด๋” > appspec.yml

version: 0.0
os: linux
files:
  - source: / 			#CodeDeploy์—์„œ ์ „๋‹ฌํ•ด ์ค€ ํŒŒ์ผ ์ „๋ถ€๋ฅผ destination์œผ๋กœ ์ „๋‹ฌํ•˜๊ฒ ๋‹ค
    destination: /home/ubuntu/deploy        
    overwrite: yes

permissions:  	#CodeDeploy์—์„œ ๋ฐ›์€ ํŒŒ์ผ์˜ ๊ถŒํ•œ์„ ์„ค์ •ํ•œ๋‹ค (ubuntu๋Š” aws ec2 ๊ธฐ๋ณธ์‚ฌ์šฉ์ž)
  - object: /
    pattern: "**"
    owner: ubuntu
    group: ubuntu

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

 

ํ”„๋กœ์ ํŠธํด๋” > scripts > deploy.sh

#!/bin/bash

cd /home/ubuntu/deploy/build/libs

nohup java -jar jlpt-0.0.1-SNAPSHOT.jar > /home/ubuntu/jlpt/nohup.out 2>&1 &
# nohup์‹คํ–‰ ์‹œ CodeDeploy๋Š” ๋ฌดํ•œ ๋Œ€๊ธฐ ํ•ฉ๋‹ˆ๋‹ค. ์ด ์ด์Šˆ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด nohub.outํŒŒ์ผ์„ ํ‘œ์ค€ ์ž…์ถœ๋ ฅ์šฉ์œผ๋กœ ๋ณ„๋„๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
# ์ด๋ ‡๊ฒŒ ํ•˜์ง€ ์•Š์œผ๋ฉด nohup.outํŒŒ์ผ์ด ์ƒ๊ธฐ์ง€ ์•Š๊ณ , CodeDeploy ๋กœ๊ทธ์— ํ‘œ์ค€ ์ž…์ถœ๋ ฅ์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค. nohub์ด ๋๋‚˜๊ธฐ ์ „๊นŒ์ง€ CodeDeploy๋„ ๋๋‚˜์ง€ ์•Š์œผ๋‹ˆ ๊ผญ ์ด๋ ‡๊ฒŒ ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

 

 

ํ”„๋กœ์ ํŠธํด๋” > src > main > resources > application.yml

spring:
  datasource:
    url: ${MY_POSTGRES_URL} 	#jdbc:postgresql://~~:5432/jlpt
    username: ${MY_POSTGRES_USER}	
    password: ${MY_POSTGRES_PASSWORD}
    #CircleCI Project Settings > Environment์— ์ •์˜๋œ ๊ฐ’๋“ค์ด ๋“ค์–ด์˜ฌ ๊ฒƒ์ด๋‹ค

  jpa:
    hibernate:
      ddl-auto: create
    properties:
      hibernate:
        dialect: org.hibernate.dialect.PostgreSQL10Dialect
        show_sql: true
        format_sql: true

 

 

CircleCI Context์„ค์ •

AWS ํ™˜๊ฒฝ๋ณ€์ˆ˜ ์ €์žฅ

1. CircleCI > Organization Settings > Contexts > Create Context

 

2. Add Environment Variable ํด๋ฆญํ•˜์—ฌ 3๊ฐ€์ง€ ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.

default region์€ ap-northeast-2(์„œ์šธ)๋กœ ์ž‘์„ฑํ•˜๊ณ , accesskey์™€ secretkey๋Š” [AWS IAM ์‚ฌ์šฉ์ž ์ถ”๊ฐ€] ์—์„œ ์ €์žฅํ•œ csvํŒŒ์ผ์˜ ๋‚ด์šฉ์„ ์ž‘์„ฑํ•ด์ค€๋‹ค.

 

ํ”„๋กœ์ ํŠธ ํ™˜๊ฒฝ๋ณ€์ˆ˜ ์ €์žฅ

Project๋กœ ๋„˜์–ด์™€์„œ Project Settings > Environment Variables์— ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋‚ด์šฉ์„ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค

 

 

 

๊นƒํ—ˆ๋ธŒ ์†Œ์Šค์ฝ”๋“œ pushํ•˜์—ฌ ๋นŒ๋“œ ๋ฐฐํฌ ํ™•์ธ

 

 

 

 

 

 

 

 

์ฐธ๊ณ 

https://velog.io/@hwany/AWS-EC2-CodeDeploy-์—ฐ๋™ํ•˜๊ธฐ

https://jojoldu.tistory.com/281

https://stackoverflow.com/questions/46195725/accessing-postgresql-9-6-from-spring-boot-in-circleci

https://www.sunny-son.space/AWS/Github%20Action%20CICD/

https://jinmay.github.io/2020/05/13/aws/how-to-install-code-deploy-agent-ubuntu/

https://backtony.github.io/spring/aws/2021-08-22-spring-cicd-2/

 

 

๋ฐ˜์‘ํ˜•
  • ๋„ค์ด๋ฒ„ ๋ธ”๋Ÿฌ๊ทธ ๊ณต์œ ํ•˜๊ธฐ
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ ํ•˜๊ธฐ
  • ํŠธ์œ„ํ„ฐ ๊ณต์œ ํ•˜๊ธฐ
  • ๊ตฌ๊ธ€ ํ”Œ๋Ÿฌ์Šค ๊ณต์œ ํ•˜๊ธฐ
  • ์นด์นด์˜คํ†ก ๊ณต์œ ํ•˜๊ธฐ