Computer Science/CI&CD

[CI/CD] Github Action을 사용한 자동 배포 구축(CD), Github Secrets 생성, Github Action 스크립트 작성

Bay Im 2024. 1. 17. 10:24

Github Secrets 생성

  • Github- Settings- Secrets and variables
  • New repository secret에서 IAM 권한 액세스 키 저장
    • AWS_ACCESS_KEY_ID
    • AWS_SECRET_ACCESS_KEY
    • APPLICATION_YML
  • Github Action 스크립트 사용 예시
aws_access_key: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws_secret_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}

 

 

Github Action 스크립트 작성

  • 예시 deploy.yml
# 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: Deploy-to-AWS-Elastic-Beanstalk

on:
  push:
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout source code
        uses: actions/checkout@v3

      - name: Set up JDK 11  # JDK 설치
        uses: actions/setup-java@v3
        with:
          java-version: '11'
          distribution: 'temurin'

      - name: make application yml  # application.yml 생성
        run: |
          cd ./src/main/resources
          touch ./application.yml
          echo "${{ secrets.APPLICATION_YML }}" > ./application.yml
        shell: bash

      - name: Grant execute permission for gradlew  # gradle 실행 권한 설정
        run: chmod +x ./gradlew

      - name: Build with Gradle  # gradle 설치
        uses: gradle/gradle-build-action@bd5760595778326ba7f1441bcf7e88b49de61a25 # v2.6.0
        with:
          arguments: build

      - name: Get current time  # 배포 버전 설정 
        uses: 1466587594/get-current-time@v2
        id: current-time
        with:
          format: YYYY-MM-DDTHH-mm-ss
          utcOffset: "+09:00"

      - name: Generate deployment package  # jar 압축 파일 생성 
        run: zip -r deploy.zip . -x '*.git*'

      - name: Deploy to EB  # AWS Beanstalk 로 자동 배포 
        uses: einaregilsson/beanstalk-deploy@v21
        with:
          aws_access_key: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws_secret_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          application_name: (applicationName)
          environment_name: (applicationName-env)
          version_label: github-action-${{steps.current-time.outputs.formattedTime}}
          region: ap-northeast-2
          deployment_package: deploy.zip

 

 

Github Action을 사용한 자동 배포 구축(CD)

  • Github Action deploy.yml 파일 작성 후 push
  • 자동 배포 완료
  • master 브랜치로 push되면 자동으로 배포 완료되는지, 최신 애플리케이션으로 변환됬는지 확인
  • 주의할 점
    • yml 파일 push했을 때 Build with Gradle에서 에러
      • 에러 메시지에 test 관련 에러 메시지가 있는지 확인한다. 이는 테스트 코드와 충돌이 있는 경우이다.
      • 테스트 코드 전체 주석처리 하면 정상 작동 
    • Github Action 정상 작동, AWS Beanstalk 압축 파일 정상으로 올라가고 버전 변경도 완료되었지만 배포된 url로 애플리케이션을 들어가니 에러 (502 Bad Gateway)
      • application.yml 파일 안에 AWS 액세스 키가 존재하므로 git 올릴 때 yml 파일은 제외하고 올라가도록 설정되어 있었다.
      • AWS beanstalk에 올라간 압축 파일을 해제해보니 yml 파일이 모두 없었다. (docker-compose.yml, application.yml)
      • docker-compose.yml 파일은 중요한 정보는 없으니까 바로 git에 올렸다.
      • application.yml 파일은 코드 전체를 Github secret에 올리고 Github Action 스크립트 파일(deploy.yml) 안에 application.yml을 생성하는 코드를 작성하여 Github Action이 실행할 때마다 application.yml 파일이 생성되도록 코드를 추가하였다.
        • Github Action 스크립트 파일(deploy.yml) steps 안에 JDK 설치 코드 아래 부분에 yml 파일 생성 코드 추가
        • - name: make application yml  # yml 파일 생성 
                  run: |
                    cd ./src/main/resources
                    touch ./application.yml
                    echo "${{ secrets.APPLICATION_YML }}" > ./application.yml
                  shell: bash
          

 

 

728x90