๐Ÿ“Œ CI์™€ AWS ํ™˜๊ฒฝ์„ค์ •์„ ๋จผ์ € ํ•ด์•ผํ•œ๋‹ค.

 

CI/CD] Github Actions CD - AWS ํ™˜๊ฒฝ ์„ค์ •

ํ•„์š” ์š”์†Œ ๊ตฌ์„ฑ ๐Ÿ“Œ 1. EC2 ์ธ์Šคํ„ด์Šค - ubuntu๋กœ ์•„ํ‚คํ…์ณ 64๋น„ํŠธ, t2.micero ํ”„๋ฆฌํ‹ฐ์–ด ์„œ๋ฒ„๊ฐ€ ํ•˜๋‚˜ ์žˆ์–ด์„œ ๊ทธ๊ฑธ ์‚ฌ์šฉ (์•„๋ž˜ ํฌ์ŠคํŒ… ๋•Œ ๋งŒ๋“ค๊ฑธ ์‚ฌ์šฉ) 000 - ์›น๊ฐœ๋ฐœ ์ข…ํ•ฉ๋ฐ˜ 5์ฃผ์ฐจ (AWS) 5-1 / 5์ฃผ์ฐจ ์˜ค๋Š˜ ๋ฐฐ์šธ๊ฒƒ

littlezero48.tistory.com

 

CI/CD] Github Actions CI

๐Ÿ“Œ Workflow ์„ ํƒ ๊นƒํ—™์—์„œ Actions > gradle ๊ฒ€์ƒ‰ > java with Gradle ์˜ Cofigure ํด๋ฆญ ๐Ÿ“Œ Workflow ์„ ํƒ ๐Ÿ“ yml์ด ๋ญ” ํŒŒ์ผ์ด์ง€? Yet another Markup Language์˜ ์•ฝ์ž๋กœ yml ๋˜๋Š” yaml ํ™•์žฅ์ž๋กœ ์‚ฌ์šฉ๋œ๋‹ค. ์—ฌ๋Ÿฌ configuration์„

littlezero48.tistory.com

 

 

๐Ÿ“Œ ๊นƒํ—™  > Actions

 

 

๐Ÿ“Œ Actions secrets ์ƒ์„ฑ 

 

๐Ÿ“Setting > Secrets and variables > Actions > New repository secret ํด๋ฆญ

 

๐Ÿ“2๊ฐœ ์ƒ์„ฑ ๊ฐ๊ฐ ( AWS ํ™˜๊ฒฝ์„ค์ •๊ธ€ 4๋ฒˆ์—์„œ ๋‹ค์šด๋ฐ›์€ CSVํŒŒ์ผ์—์„œ ํ‚คID ๊ฐ’๊ณผ ์•ก์„ธ์Šคํ‚ค๋ฅผ ๊ฐ€์ ธ์™€ ๊ฐ๊ฐ ์ ์šฉ)

- CSV๋Š” ,๋กœ ๊ฐ’์„ ๋‚˜๋ˆˆ๋‹ค. 

 

 

 

๐Ÿ“Œ  AppSpec ํŒŒ์ผ ์ƒ์„ฑ

********* AppSpec ํŒŒ์ผ๋ช…์€ appspec์œผ๋กœ ๋ชจ๋‘ ์†Œ๋ฌธ์ž๋กœ ํ•ด์•ผํ•จ!!!!!!! ์•„๋ž˜์ฒ˜๋Ÿผ ํ–ˆ๋‹ค๊ฐ€ 255 Error๋ด„ 

root ํด๋” ์ฆ‰, builda.gradle ์ด๋‚˜ gradlew ๋“ฑ์ด ์žˆ๋Š” ๊ฒฝ๋กœ์— ์ƒ์„ฑ. ์ด ํŒŒ์ผ์€ CodeDeploy๊ฐ€ ๋ฐฐํฌ๋ฅผ ์œ„ํ•ด ์ฐธ์กฐํ•˜๋Š” ํŒŒ์ผ!

์—ฌ๊ธฐ์„œ ํŒŒ์ผ์„ EC2 ์–ด๋Š ๊ฒฝ๋กœ์— ๋ณต์‚ฌํ• ๊ฑด์ง€, ๋ฐฐํฌ ์ดํ›„์— ์–ด๋–ค ํ–‰๋™์„ ํ•˜๊ฒŒ ํ•  ๊ฑด์ง€ ํ•  ์ˆ˜ ์žˆ์–ด ์„œ๋ฒ„ ๋ฐฐํฌ์— ๋Œ€ํ•œ ์„ค์ •์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

version: 0.0                                        # ๋ฒ„์ ผ
os: linux                                           # OS

files:                                              # ๋ฐฐํฌ ํŒŒ์ผ ์„ค์ •
  - source:  /                                      # ์ธ์Šคํ„ด์Šค์— ๋ณต์‚ฌํ•  ๋””๋ ‰ํ„ฐ๋ฆฌ ๊ฒฝ๋กœ
    destination: /home/ubuntu/app                   # ์ธ์Šคํ„ด์Šค์—์„œ ํŒŒ์ผ์ด ๋ณต์‚ฌ๋  ๋ชฉ์ ์ง€
    overwrite: yes                                  # ๋ฎ์–ด์“ฐ๊ธฐ ํ—ˆ์šฉ

permissions:                                        # ํŒŒ์ผ์— ๊ด€ํ•œ ๊ถŒํ•œ ์„ค์ •
  - object: /                                       # ๊ถŒํ•œ ์ง€์ •๋˜๋Š” ํŒŒ์ผ ๋˜๋Š” ๊ฒฝ๋กœ
    pattern: "**"                                   # ๋งค์นญ๋˜๋Š” ํŒจํ„ด์—๋งŒ ๊ถŒํ•œ๋ถ€์—ฌ
    owner: ubuntu                                   # Object ์†Œ์œ ์ž
    group: ubuntu                                   # Object ๊ทธ๋ฃน ์ด๋ฆ„

hooks:                                              # ๋ฐฐํฌ ์ดํ›„ ์ˆ˜ํ–‰ํ•  ์Šคํฌ๋ฆฝํŠธ ๋ถ€๋ถ„
  AfterInstall:                                     # ํŒŒ์ผ ์„ค์น˜ ํ›„ ์‹คํ–‰๋˜๋Š” ์Šคํฌ๋ฆฝํŠธ
    - location: scripts/stop.sh                     # ์‹คํ–‰๋˜๋Š” ์Šคํฌ๋ฆฝํŠธ ๊ฒฝ๋กœ, ์œ„์น˜
      timeout: 60                                   # ์Šคํฌ๋ฆฝํŠธ ์‹คํ–‰ ํ—ˆ์šฉ ์‹œ๊ฐ„, ๋„˜์œผ๋ฉด ๋ฐฐํฌ ์‹คํŒจ
      runas: ubuntu                                 # ์Šคํฌ๋ฆฝํŠธ ์‹คํ–‰ ์‚ฌ์šฉ์ž
  ApplicationStart:                                 # ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹คํ–‰ ์Šคํฌ๋ฆฝํŠธ
    - location: scripts/start.sh
      timeout: 60
      runas: ubuntu

๐Ÿ“ Code Deploy ํ๋ฆ„ ์ƒ๊ฐํ•ด์„œ hook ์ž‘์„ฑ

 

 

 

 

๐Ÿ“Œ  ์‹คํ–‰ ์Šคํฌ๋ฆฝํŠธ ์ž‘์„ฑ

rootํด๋”์— scripts ํด๋” ์ƒ์„ฑ

appspec ํŒŒ์ผ์—์„œ ์‹คํ–‰ ์‹œํ‚ฌ ์‹คํ–‰ ์Šคํฌ๋ฆฝํŠธ ๋˜ํ•œ ์ž‘์„ฑํ•œ๋‹ค.

 

๋ฑ€๊ทค๋‹˜ ์ฝ”๋“œ๋ฅผ ๊ทธ๋Œ€๋กœ ์จ์„œ ๊ณต๋ถ€ํ–ˆ์Šต๋‹ˆ๋‹ค! 7-1๊ณผ 7-2๋ฒˆ ์ฝ”๋“œ!

 

Github Actions CD: AWS EC2 ์— Spring Boot ๋ฐฐํฌํ•˜๊ธฐ

Overview ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ฐœ๋ฐœํ•˜๋ฉด ์™ธ๋ถ€์—์„œ๋„ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋„๋ก ํด๋ผ์šฐ๋“œ ํ™˜๊ฒฝ์— ๋ฐฐํฌํ•ฉ๋‹ˆ๋‹ค. ์ด์ „์— ํฌ์ŠคํŒ… ํ–ˆ๋˜ AWS 1ํŽธ์—์„œ๋Š” ๋งˆ์ง€๋ง‰์— scp ๋ช…๋ น์–ด๋กœ ๋กœ์ปฌ์— ์กด์žฌํ•˜๋Š” ๋นŒ๋“œ ํŒŒ์ผ์„ EC2 ์ธ์Šคํ„ด์Šค

bcp0109.tistory.com

๐Ÿ“ stop.sh (๊ธฐ์กด์— ์‹คํ–‰ ์ค‘์ด๋˜ ํŒŒ์ผ์„ ์ข…๋ฃŒ)

#!/usr/bin/env bash

PROJECT_ROOT="/home/ubuntu/app"
JAR_FILE="$PROJECT_ROOT/CICD_githubActions-0.0.1-SNAPSHOT.jar"

APP_LOG="$PROJECT_ROOT/application.log"
ERROR_LOG="$PROJECT_ROOT/error.log"
DEPLOY_LOG="$PROJECT_ROOT/deploy.log"

TIME_NOW=$(date +%c)

# build ํŒŒ์ผ ๋ณต์‚ฌ
echo "$TIME_NOW > $JAR_FILE ํŒŒ์ผ ๋ณต์‚ฌ" >> $DEPLOY_LOG
cp $PROJECT_ROOT/build/libs/*.jar $JAR_FILE

# jar ํŒŒ์ผ ์‹คํ–‰
echo "$TIME_NOW > $JAR_FILE ํŒŒ์ผ ์‹คํ–‰" >> $DEPLOY_LOG
nohup java -jar $JAR_FILE > $APP_LOG 2> $ERROR_LOG &                          # ๊ณ„์† ์‹คํ–‰๋˜๊ฒŒ nohup์œผ๋กœ ์‹คํ–‰

CURRENT_PID=$(pgrep -f $JAR_FILE)                                             # nohup์œผ๋กœ ์‹คํ–‰ ํ›„ ํ”„๋กœ์„ธ์Šค ์•„์ด๋””๋ฅผ ์–ป์Œ
echo "$TIME_NOW > ์‹คํ–‰๋œ ํ”„๋กœ์„ธ์Šค ์•„์ด๋”” : $CURRENT_PID" >> $DEPLOY_LOG

๐Ÿ“ start.sh (์ƒˆ๋กœ์šด jarํŒŒ์ผ์„ ์‹คํ–‰)

#!/usr/bin/env bash
# #!์€ ๋ช…๋ น์–ด ์ง‘ํ•ฉํ‘œ์‹œ, ๋’ค๋Š” ์ด ๋ช…๋ น์–ด๋“ค์„ ํ•ด์„ํ•  ํ”„๋กœ๊ทธ๋žจ ์œ„์น˜์™€ ํ”„๋กœ๊ทธ๋žจ

#-------------------------------------------------- ๋ณ€์ˆ˜ ์„ ์–ธ START
PROJECT_ROOT="/home/ubuntu/app"                                             # ์ข…๋ฃŒํ•  jarํŒŒ์ผ ์œ„์น˜
JAR_FILE="$PROJECT_ROOT/CICD_githubActions-0.0.1-SNAPSHOT.jar"                                  # ์ข…๋ฃŒํ•  jarํŒŒ์ผ ์ด๋ฆ„

DEPLOY_LOG="$PROJECT_ROOT/deploy.log"                                       # ๋กœ๊ทธํŒŒ์ผ ์ƒ์„ฑ

TIME_NOW=$(date +%c)                                                        # ํ˜„์žฌ์‹œ๊ฐ„

# ํ˜„์žฌ ๊ตฌ๋™ ์ค‘์ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ pid ํ™•์ธ
CURRENT_PID=$(pgrep -f $JAR_FILE)
#-------------------------------------------------- ๋ณ€์ˆ˜ ์„ ์–ธ END

#-------------------------------------------------- ๋ช…๋ น์–ด START
# ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ผœ์ ธ ์žˆ์œผ๋ฉด ์ข…๋ฃŒ
if [ -z $CURRENT_PID ]; then
  # ๋ฆฌ๋ˆ…์Šค์—์„œ > ์™€ >> ์˜ ์ฐจ์ด : > (๋’ค์— ๋‚˜์˜ค๋Š” ํŒŒ์ผ์— write or overwrite), >> (๋’ค์— ๋‚˜์˜ค๋Š” ํŒŒ์ผ์— ์ถ”๊ฐ€ append)
  echo "$TIME_NOW > ํ˜„์žฌ ์‹คํ–‰์ค‘์ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์—†์Šต๋‹ˆ๋‹ค" >> $DEPLOY_LOG         # ๋ฆฌ๋ˆ…์Šค ํ„ฐ๋ฏธ๋„ ์ถœ๋ ฅ ๋ช…๋ น์–ด
else
  echo "$TIME_NOW > ์‹คํ–‰์ค‘์ธ $CURRENT_PID ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ข…๋ฃŒ " >> $DEPLOY_LOG
  kill -15 $CURRENT_PID                                                     # -15 ํ”„๋กœ์„ธ์Šค๋ฅผ ์•ˆ์ „ํ•˜๊ฒŒ ์ข…๋ฃŒ
fi

 

๐Ÿ“Œ  plain.jar ์ƒ์„ฑ๋˜์ง€ ์•Š๊ฒŒ build.gradle ์„ค์ •

๋นŒ๋“œ์‹œ ์ผ๋ฐ˜ jarํŒŒ์ผ ๋ง๊ณ ๋„ plain.jarํŒŒ์ผ์ด ํ•˜๋‚˜ ๋” ์ƒ์„ฑ๋˜๋Š” ๋ฐ ์ด๋ฅผ ๋ฐฉ์ง€, ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ๋œ๋‹ค.

* plain.jar๋Š” plain archive๋ผ๊ณ  ํ•ด์„œ dependency๋ฅผ ๋ชจ๋‘ ์ œ์™ธํ•œ ์ˆœ์ˆ˜ํ•œ ์†Œ์Šค์ฝ”๋“œ์™€ ๋ฆฌ์†Œ์Šค๋งŒ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํŒŒ์ผ 

jar {
    enabled = false
}

 

 

๐Ÿ“Œ  gradle.yml ์ˆ˜์ •

๐Ÿ“ ์ˆ˜์ • ์ „

name: Java CI with Gradle

on:                                                               # ์›Œํฌํ”Œ๋กœ์šฐ๋ฅผ ์‹คํ–‰ํ•  ์ด๋ฒคํŠธ ์ข…๋ฅ˜์™€ ํŠน์ • ๋ธŒ๋žœ์น˜ ์„ค์ •
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

permissions:                                                      # ๊ถŒํ•œ ์„ค์ •
  contents: read

jobs:                                                             # ์ˆ˜ํ–‰ํ•  ์›Œํฌํ”Œ๋กœ์šฐ 
  build:                                                          # ๋นŒ๋“œ
    runs-on: ubuntu-latest                                        # ๋นŒ๋“œ๊ฐ€ ์‹คํ–‰ํ•  OS

    steps:                                                        # ๋‹จ๊ณ„ ์„ค์ •
    
    - uses: actions/checkout@v3                                   # ์›Œํฌํ”Œ๋กœ์šฐ ์‹คํ–‰์ „ ์ฒดํฌ์•„์›ƒ

    - name: Set up JDK 11                                         # JDK 11 ๋ฒ„์ „ ์„ค์น˜
      uses: actions/setup-java@v3
      with:
        java-version: '11'
        distribution: 'temurin'

    - name: Grant execute permission for gradlew
      run: chmod +x ./gradlew
      # Build

    - name: Build with Gradle                                     # gradle๋กœ ๋นŒ๋“œ ์‹คํ–‰
      uses: gradle/gradle-build-action@67421db6bd0bf253fb4bd25b31ebb98943c375e1
      with:
        arguments: build

 

๐Ÿ“ ์ˆ˜์ • ํ›„

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
# This workflow will build a Java project with Gradle and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-gradle

name: Java CI/CD with Gradle

on:                                                               # ์›Œํฌํ”Œ๋กœ์šฐ๋ฅผ ์‹คํ–‰ํ•  ์ด๋ฒคํŠธ ์ข…๋ฅ˜์™€ ํŠน์ • ๋ธŒ๋žœ์น˜ ์„ค์ •
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

env:                                                              # ํ™˜๊ฒฝ ์„ค์ •
  AWS_REGION: ap-northeast-2
  S3_BUCKET_NAME: littlezerobucket
  CODE_DEPLOY_APPLICATION_NAME: cicd_Test
  CODE_DEPLOY_DEPLOYMENT_GROUP_NAME: cicd_group        # IAM์˜ CodeDeploy์šฉ ์—ญํ• 

permissions:                                                      # ๊ถŒํ•œ ์„ค์ •
  contents: read

jobs:                                                             # ์ˆ˜ํ–‰ํ•  ์›Œํฌํ”Œ๋กœ์šฐ 
  deploy:                                                         # Deploy
    name: Deploy
    runs-on: ubuntu-latest                                        # ๋นŒ๋“œ๊ฐ€ ์‹คํ–‰ํ•  OS
    environment: production

    steps:                                                        # ๋‹จ๊ณ„ ์„ค์ •

    # ์ฒดํฌ์•„์›ƒ
    - name: Checkout
      uses: actions/checkout@v3                                   # ์›Œํฌํ”Œ๋กœ์šฐ ์‹คํ–‰์ „ ์ฒดํฌ์•„์›ƒ

    # JDK 11 ์„ธํŒ…
    - name: Set up JDK 11                                         # JDK 11 ๋ฒ„์ „ ์„ค์น˜
      uses: actions/setup-java@v3
      with:
        java-version: '11'
        distribution: 'temurin'

    # BUILD ๊ถŒํ•œ ๋ถ€์—ฌ
    - name: Grant execute permission for gradlew
      run: chmod +x ./gradlew
      # Build

    # BUILD
    - name: Build with Gradle                                     # gradle๋กœ ๋นŒ๋“œ ์‹คํ–‰
      uses: gradle/gradle-build-action@67421db6bd0bf253fb4bd25b31ebb98943c375e1
      with:
        arguments: clean build

    # AWS ์ธ์ฆ
    - 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: ${{ env.AWS_REGION }}

      # ๋นŒ๋“œํ•œ ํŒŒ์ผ S3 ๋ฒ„ํ‚ท์— ์—…๋กœ๋“œ
    - name: Upload to AWS S3
      run: |
        aws deploy push \
          --application-name ${{ env.CODE_DEPLOY_APPLICATION_NAME }} \
          --ignore-hidden-files \
          --s3-location s3://$S3_BUCKET_NAME/$GITHUB_SHA.zip \
          --source .

      # S3 ๋ฒ„ํ‚ท์— ์˜ฌ๋ฆฐ ํŒŒ์ผ๋กœ CodeDeploy ์‹คํ–‰
    - name: Deploy to AWS EC2 from S3
      run: | 
        aws deploy create-deployment \
          --application-name ${{ env.CODE_DEPLOY_APPLICATION_NAME }} \
          --deployment-config-name CodeDeployDefault.AllAtOnce \
          --deployment-group-name ${{ env.CODE_DEPLOY_DEPLOYMENT_GROUP_NAME }} \
          --s3-location bucket=$S3_BUCKET_NAME,key=$GITHUB_SHA.zip,bundleType=zip

 

 

 

๐Ÿ“Œ ๊ทธ๋ฆฌ๊ณ  ์•„๋ž˜ ๋ฐœ์ƒํ•œ ๋‹ค์–‘ํ•œ ํŠธ๋Ÿฌ๋ธ”... ๋“ค์„ ํ•ด๊ฒฐํ•˜๊ณ  ๋‚˜์„œ ๋“œ๋””์–ด ์ž๋™ ๋ฐฐํฌ ์„ฑ๊ณต! 

 

 ํŠธ๋Ÿฌ๋ธ” ์ŠˆํŒ…๋“ค ****  

๋”๋ณด๊ธฐ

*** 1)

Error: The security token included in the request invalid.

 

๋ฌธ์ œ: credential ๊ฒ€์ฆ์—์„œ ์˜ค๋ฅ˜

์›์ธ: Code Deploy Application Name ๊ฐ’๊ณผ Code Deploy Application Group Name์„ ์ž˜๋ชป๋„ฃ์–ด์„œ ๋ฐœ์ƒ

ํ•ด๊ฒฐ: AWS CodeDeploy์—์„œ ์ƒ์„ฑํ•œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ด๋ฆ„๊ณผ ๊ทธ ์•ˆ์˜ ๋ฐฐํฌ ๊ทธ๋ฃน ์ด๋ฆ„์œผ๋กœ ์„ค์ •ํ•ด์ฃผ๋‹ˆ ํ•ด๊ฒฐ

 

๐Ÿ“ ๋‘˜๋‹ค AWSํ™˜๊ฒฝ ์„ค์ • ๊ธ€์—์„œ 9๋ฒˆ์—์„œ ๋งŒ๋“  ๊ฒƒ์„ yml์— ๋„ฃ์–ด์ค˜์•ผ ํ•œ๋‹ค.  

์œ„์— ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ํ•ด๋‹น ์ด๋ฆ„ ๋ˆ„๋ฅด๋ฉด ์žˆ๋Š” ๋ฐฐํฌ ๊ทธ๋ฃน

 


 *** 2)

Error: Process completed with exit code 255.

appspec.yml was not found.

 

๋ฌธ์ œ: appspec.yml์„ ์ฐพ์ง€ ๋ชปํ•ด ์‹คํ–‰ ๋ชปํ•จ

์›์ธ: appspec ํŒŒ์ผ๋ช…์„ AppSpec์œผ๋กœ ํ•ด์„œ ์ฐพ์ง€ ๋ชปํ•จ

ํ•ด๊ฒฐ: ํŒŒ์ผ๋ช…์„ ๋ชจ๋‘ ์†Œ๋ฌธ์ž๋กœ ํ•ด์คฌ๋”๋‹ˆ ํ•ด๊ฒฐ

 

 


*** 3)

Error: Process completed with exit code 127.

command not found

 

๋ฌธ์ œ: ์š”๊ตฌํ•˜๋Š” arguments๋ฅผ ๋ชป๋ถˆ๋Ÿฌ์˜ด

์›์ธ: ํ•ด๋‹น arguments ์•ž์— ๋งž์ง€ ์•Š๋Š” arguments๊ฐ€ ๋ถ™์–ด ๋’ค๋ฅผ ์ฝ์ง€ ๋ชปํ•จ

ํ•ด๊ฒฐ: ๋ถˆํ•„์š”ํ•œ arguments ์‚ญ์ œ๋กœ ํ•ด๊ฒฐ

 

 


*** 4) 

CodeDeploy agent was not able to receive the lifecycle event. Check the CodeDeploy agent logs on your host and make sure the agent is running and can connect to the CodeDeploy server.

- CodeDeploy agent๊ฐ€ ๋ผ์ดํ”„์‚ฌ์ดํด ์ด๋ฒคํŠธ๋ฅผ ๋ฐ›์„ ์ˆ˜ ์—†์œผ๋‹ˆ host์˜ ๋กœ๊ทธ๋ฅผ ์‚ดํŽด๋ณด๋ผ๋Š” ๋‚ด์šฉ

 

EC2 ๋กœ๊ทธ๋Š” ์ฒ˜์Œ ์šฐ๋ถ„ํˆฌ ์ ‘์† ๊ธฐ์ค€

../../var/log/aws/codedeploy-agent/codedeploy-agent.log 

์— ์œ„์น˜ํ•˜๊ณ  ์žˆ๋‹ค. ๊ทธ๊ณณ์— ์จ์žˆ๋Š” ๋กœ๊ทธ๋ฅผ ๋ณด๋ฉด

 

ERROR [codedeploy-agent(1163)]: InstanceAgent::Plugins::CodeDeployPlugin::CommandPoller: Missing credentials - please check if this instance was started with an IAM instance profile

 

๋ฌธ์ œ: github actions์—์„œ Deploy๊ฐ€ ์„ฑ๊ณตํ–ˆ์Œ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ์‹ค์ œ๋กœ AWS Deploy๊ฒฐ๊ณผ๋ฅผ ๋ดค์„ ๋•Œ ApplicationStop์—์„œ ๋ฐ”๋กœ ์‹คํŒจํ•œ ์ผ€์ด์Šค

์›์ธ: IAM ์—ญํ• ์„ ์ง€์ •ํ•˜์ง€ ์•Š์€ ์ธ์Šคํ„ด์Šค์— CodeDeployAgent๋ฅผ ๋จผ์ € ์„ค์น˜ํ•ด ๋ฒ„๋ ค CodeDeploy์— ํ•ด๋‹น ์—ญํ• ์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ์ž๊ฒฉ ์ฆ๋ช…์ด ์—†์–ด์„œ ์ƒ๊ธด ๋ฌธ์ œ

ํ•ด๊ฒฐ: CodeDeploy Agent๋ฅผ EC2์—์„œ ๋‹ค์‹œ ์‹คํ–‰ ์‹œํ‚ค๋ฉด ๋œ๋‹ค.

 

- 1) ๋‹ค์‹œ ์‹คํ–‰

- 2) ์ƒํƒœ ํ™•์ธ

sudo service codedeploy-agent restart
sudo service codedeploy-agent status

 

 

 

๐Ÿ“Œ ์œ„์— ์„ฑ๊ณต ๋กœ๊ทธ ๋ณด๋‹ค๊ฐ€ ๋“  ์˜๋ฌธ์ 

์ด ๋กœ๊ทธ๋ฅผ ๋ณด๋ฉด ํ˜„์žฌ ์‹คํ–‰์ค‘์ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์—†๋‹ค๊ณ  ํ•œ๋‹ค. ํ•˜์ง€๋งŒ ์•„๋ž˜์ฒ˜๋Ÿผ ์‰˜ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋„ฃ์–ด์คฌ๊ณ , ๊ธฐ์กด nohup์„ ์ฃฝ์ด๊ณ  ์ƒˆ๋กœ์šด ๋นŒ๋“œ ํŒŒ์ผ๋กœ๋„ ๋ณ€๊ฒฝ์ด๋˜์„œ nohup์œผ๋กœ ๋Œ์•„๊ฐ€๋Š” ์ƒํ™ฉ. ๊ทผ๋ฐ ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ผœ์ ธ์žˆ์–ด์„œ stop.sh๊ฐ€ killํ•ด์คฌ๋‹ค๋ฉด ์‹คํ–‰์ค‘์ธ ํ”„๋กœ์„ธ์Šค ์•„์ด๋””์™€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ข…๋ฃŒ์— ๋Œ€ํ•œ ๋กœ๊ทธ๋„ ์ฐํ˜”์–ด์•ผํ•˜๋Š” ๋ฐ ์—†๋‹ค. ์ด๋ฏธ stop์ „์— ํ”„๋กœ์„ธ์Šค๊ฐ€ kill์ด ๋œ ๊ฒƒ์œผ๋กœ ๋ณด์ธ๋‹ค.

# ํ”„๋กœ์„ธ์Šค๊ฐ€ ์ผœ์ ธ ์žˆ์œผ๋ฉด ์ข…๋ฃŒ
if [ -z $CURRENT_PID ]; then
  # ๋ฆฌ๋ˆ…์Šค์—์„œ > ์™€ >> ์˜ ์ฐจ์ด : > (๋’ค์— ๋‚˜์˜ค๋Š” ํŒŒ์ผ์— write or overwrite), >> (๋’ค์— ๋‚˜์˜ค๋Š” ํŒŒ์ผ์— ์ถ”๊ฐ€ append)
  echo "$TIME_NOW > ํ˜„์žฌ ์‹คํ–‰์ค‘์ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์—†์Šต๋‹ˆ๋‹ค" >> $DEPLOY_LOG         # ๋ฆฌ๋ˆ…์Šค ํ„ฐ๋ฏธ๋„ ์ถœ๋ ฅ ๋ช…๋ น์–ด
else
  echo "$TIME_NOW > ์‹คํ–‰์ค‘์ธ $CURRENT_PID ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ข…๋ฃŒ " >> $DEPLOY_LOG
  kill -15 $CURRENT_PID                                                     # -15 ํ”„๋กœ์„ธ์Šค๋ฅผ ์•ˆ์ „ํ•˜๊ฒŒ ์ข…๋ฃŒ
fi

์ง์ ‘ killํ•˜์ง€ ์•Š์•„๋„ application์„ stopํ•ด ์ฃผ๋Š” ๋กœ์ง์ด ์žˆ๋Š” ๊ฑธ๊นŒ ์‹ถ์–ด ์ด๋ฒคํŠธ๋ฅผ ๋ณด๋‹ˆ ApplicationStop์ด ๋งˆ์Œ์— ๊ฑธ๋ฆฐ๋‹ค.

 

ํ˜น์‹œ ์ •๋ง ์•ž์—์„œ ํ•ด์ฃผ๊ณ  ์žˆ๋Š” ๊ฑฐ ์•„๋‹๊นŒ ์‹ถ์–ด์„œ

AfterInstall๊ณผ์ •์—์„œ stop.sh์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ์„ค์ •ํ•œ yml ํŒŒ์ผ ๋ถ€๋ถ„์„ ์•„์˜ˆ ์ฃผ์„์ฒ˜๋ฆฌ ํ•ด๋ฒ„๋ฆฌ๊ณ  ํ…Œ์ŠคํŠธ๋ฅผ ํ–ˆ๋Š”๋ฐ

๋ ์šฉ ์ž˜๋œ๋‹ค. ์•ž์„œ ์ฐํžŒ ๋กœ๊ทธ์™€ ๋‹ฌ๋ฆฌ ๋งˆ์ง€๋ง‰ ๋กœ๊ทธ์—” stop.sh์— ์žˆ๋Š” ํ˜„์žฌ ์‹คํ–‰์ค‘์ธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์—†์Šต๋‹ˆ๋‹ค ๋กœ๊ทธ๋„ ์—†๋‹ค.  ๊ทธ๋Ÿผ stop.sh๊ฐ€ ์—†๋Š” ๋ฒ„์ ผ์œผ๋กœ ์ž˜ ์‹คํ–‰๋ฌ๋‹ค๋Š” ์ด์•ผ๊ธฐ..

 

์ฐพ์•„๋ณด๋‹ˆ ๋ฐฐํฌ ๋ผ์ดํ”„์‚ฌ์ดํด ์ด๋ฒคํŠธ ์ค‘์— ApplicationStop์€ ๋งˆ์ง€๋ง‰์œผ๋กœ ์„ฑ๊ณต ๋ฐฐํฌํ•œ ๋ฒ„์ ผ์—์„œ appspec file๊ณผ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๊ฐ€์ ธ์™€์„œ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•œ๋‹ค. ์ด ๋ง์€ ์ฆ‰ ์ƒˆ๋กœ ๋ฐฐํฌํ•œ ํŒŒ์ผ์—์„œ์˜ applicationStop๊ด€๋ จ์€ ์ด๋ฒˆ ๋ฐฐํฌ๊ฐ€ ์•„๋‹Œ ๋‹ค์Œ ๋ฐฐํฌ๋ฅผ ์œ„ํ•ด ์ ์šฉ๋œ๋‹ค๋Š” ๋œป์ด๊ธฐ๋„ ํ•˜๋‹ค. ํ•˜์ง€๋งŒ ๋‚˜๋Š” ์ „์—๋„ ํ›„์—๋„ applicationStop์— killํ•˜๋Š” ๊ฑธ ์„ค์ •ํ•ด์ค€ ์ ์ด ์—†๋Š”๋ฐ? ์•„๋‹ˆ๋ฉด ์ „์— ํŒŒ์ผ์„ ๋‹ค์‹œ ์—ญํ–‰ํ•˜๋Š” ๋ฐฉ์‹? ์€.. ์•„๋‹Œ๊ฑฐ ๊ฐ™๊ณ  ์™œ kill์ฒ˜๋ฆฌ๋ฅผ ์ฃผ์„ํ•ด์คฌ์Œ์—๋„ killํ•˜๋Š”๊ฒŒ ๋˜๋Š” ๊ฑธ๊นŒ ๊ถ๊ธˆํ•ด!!

 

์ด์— ๋Œ€ํ•œ ๋‚ด์šฉ์€ ์ข€ ๋” ์กฐ์‚ฌํ•ด๋ด์•ผํ•  ๊ฑฐ ๊ฐ™๋‹ค. 

 

 

 


 

โž• CI/CD ์—ฐ์Šตํ•œ ๊นƒํ—™ ๋ ˆํฌ์ง€ํ† ๋ฆฌ

 

GitHub - littlezero48/CICD_githubActions: CI/CD GithubAction์„ ์—ฐ์Šต

CI/CD GithubAction์„ ์—ฐ์Šต. Contribute to littlezero48/CICD_githubActions development by creating an account on GitHub.

github.com

 

โž• ๊ณต๋ถ€์ž๋ฃŒ 

 

AWS 1ํŽธ: EC2 ์ƒ์„ฑ ํ›„ Spring Boot ๋„์šฐ๊ธฐ

Overview AWS EC2 ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ  Spring Boot ์„œ๋ฒ„๋ฅผ ๋„์›Œ๋ณด๋Š” ๊ฒƒ๊นŒ์ง€ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ฃผ ๋ชฉํ‘œ๋Š” ์„œ๋ฒ„๋ฅผ ์™ธ๋ถ€์— ์ œ๊ณตํ•˜๋Š” ๊ฑฐ๋ผ์„œ ๋”ฐ๋กœ ๋ฐฐํฌ ์‹œ์Šคํ…œ์„ ๊ตฌ์ถ•ํ•˜์ง€ ์•Š๊ณ  ๋‹จ์ˆœํžˆ ๋นŒ๋“œ ํŒŒ์ผ์„ ๋ณต์‚ฌํ•ด์„œ ์ˆ˜

bcp0109.tistory.com

 

[SpringBoot] Github Action์œผ๋กœ AWS EC2 ์ž๋™ ๋นŒ๋“œ/๋ฐฐํฌํ•˜๊ธฐ(CI/CD)

[SpringBoot] Github Action์œผ๋กœ AWS EC2 ์ž๋™ ๋นŒ๋“œ/๋ฐฐํฌํ•˜๊ธฐ(CI/CD) ๋น—์ธํ…Œํฌ์•„์นด๋ฐ๋ฏธ์˜ ๊ฐ•์˜๊ฐ€ ๋๋‚˜๊ณ  ํŒ€ํ”„๋กœ์ ํŠธ๊ฐ€ ์‹œ์ž‘๋˜์—ˆ๋Š”๋ฐ, ๊ฐœ๋ฐœ์— ์•ž์„œ ์šฐ์„  aws์— ๋ฐฐํฌํ•ด์„œ Hello world๋ฅผ ๋จผ์ € ์ฐ์–ด๋ณด๊ณ  ๊ฐœ๋ฐœ์„ ํ•˜

be-developer.tistory.com

 

+ Recent posts