Skip to content

CD 파이프라인 구축

Moly edited this page Jul 22, 2024 · 7 revisions

정의

CD : Continuous Deployment, 지속적 배포


필요한 이유/원하는 것

개발자의 변경 사항을 리포지토리에서 프로덕션으로 릴리스하는 것을 자동화하여 고객이 사용할 수 있도록 한다



CD를 위한 Github Actions 구축 방법 정리

기본 정보는 CI 파이프라인 구축 을 참고


0. Github Actions에 Self-hosted Runner 등록

Self-hosted Runner 란?
Github Actions에서 사용자가 지정하는 로컬 컴퓨팅 자원으로 빌드를 수행하도록 설정하는 기능입니다.


1) Settings - Actions - Runners 이동 및 New self-hosted runner 클릭

runner_step1


2) runner로 등록한 자원에 맞게 환경 설정

runner_step2

우리는 Linux의 armx64 을 사용한다


3) runner로 등록한 자원으로 이동 후 명령어 실행

Download

  • 폴더 생성
mkdir actions-runner && cd actions-runner
  • 가장 최신의 Runner 패키지 다운로드
curl -o actions-runner-linux-arm64-2.317.0.tar.gz -L https://github.com/actions/runner/releases/download/v2.317.0/actions-runner-linux-arm64-2.317.0.tar.gz
  • 다운로드 받은 압축파일 압축해제
tar xzf ./actions-runner-linux-arm64-2.317.0.tar.gz

Configure

  • 저장소 연결
./config.sh --url [repository url] --token [github token]
  • runner 프로그램 실행
nohup ./run.sh &

Using your self-hosted runner

  • job 실행 환경 설정
runs-on: self-hosted

1. Secrets 추가

제목 없음

우리는 secrets.WORK_DIRECTORY 가 필요하니, WORK_DIRECTORY의 값을 설정한다.


2. 최상위 디렉터리에 .github/workflows/backend_cd.yml 파일 생성

build.yml 파일 이름은 가장 적절한 이름으로 네이밍한다.


3. build.yml 파일 작성

1)workflow 이름 설정

name: CI # workflow 이름
  • workflow 이름을 적절하게 네이밍
  • workflow 안에는 여러 개의 job이 존재

2) 실행조건 설정

설정된 브랜치로 push되면 jobs 실행

on: 
  push:
    branshes: # 설정된 브랜치
      - branch1 

설정된 브랜치로 pull-request 되면 jobs 실행

on: 
  pull_request:
    branshes: # 설정된 브랜치
      - branch1 

이 외에도 실행 조건은 다양하다.


3) jobs 설정

jobs:
  build: # jar 파일 빌드해주는 job
    # ...
  deploy: # 서버 배포를 해주는 job
    #...

jar 파일 빌드

job 명 : build

build:  
  runs-on: ubuntu-latest # 작업 환경: 가상 환경 우분투로
  env: # 필요한 환경 변수 제작 
    build-directory: ./backend  
  steps: # 입력된 작업 순서대로 실행
    # 작업 실행

서버 배포

job 명 : deploy

deploy:  
  needs: build # job 명이 build인 job이 성공하면 작동한다
  runs-on: self-hosted # 작업 환경: self-hosted runner 환경에서
  steps: # 입력된 작업 순서대로 실행
    # 작업 실행

문제 해결

port 제한으로 인한 scp 명령 불가능

문제 발생

22번 포트로 접근할 수 있는 ip가 제한되어 있어, github actions에서 scp로 파일 전달이 불가능했다.

해결

self-hosted runner 사용으로 해결



실제 구축된 Github Actions 파일

name: Backend CD  
  
on:  
  push:  
    branches:  
      - develop  
      - main  
  
jobs:  
  build:  
    runs-on: ubuntu-latest  
    env:  
      build-directory: ./backend  
    steps:  
      - name: Checkout  
        uses: actions/checkout@v4  
  
      - name: Setup JDK 17  
        uses: actions/setup-java@v4  
        with:  
          java-version: 17  
          distribution: temurin  
  
      - name: Gradle Caching  
        uses: actions/cache@v3  
        with:  
          path: |  
            ~/.gradle/caches  
            ~/.gradle/wrapper  
          key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}  
          restore-keys: |  
            ${{ runner.os }}-gradle-  
  
      - name: Build BootJar  
        run: ./gradlew bootJar  
        working-directory: ${{ env.build-directory }}  
  
      - name: Upload Artifact  
        uses: actions/upload-artifact@v4  
        with:  
          name: code-zap-jar  
          path: backend/build/libs/*.jar  
  
  deploy:  
    needs: build  
    runs-on: self-hosted  
    steps:  
      - name: Get Artifact Download URL  
        id: get-artifact-url  
        run: |  
          artifacts=$(curl -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \  
                            -H "Accept: application/vnd.github.v3+json" \  
                            https://api.github.com/repos/${{ github.repository }}/actions/artifacts)  
          artifact_id=$(echo "$artifacts" | jq -r '.artifacts[0].id')  
          if [[ -z "$artifact_id" || "$artifact_id" == "null" ]]; then  
            echo "Artifact ID not found or invalid"  
            exit 1  
          fi  
          download_url="https://api.github.com/repos/${{ github.repository }}/actions/artifacts/$artifact_id/zip"  
          echo "DOWNLOAD_URL=${download_url}" >> $GITHUB_ENV  
  
      - name: Download Artifact  
        run: |  
          curl -L -o ${{ secrets.WORK_DIRECTORY }}/code-zap-jar.zip -H \  
          "Authorization: token ${{ secrets.GITHUB_TOKEN }}" ${{ env.DOWNLOAD_URL }}  
  
      - name: Run Deploy Script  
        run: |  
          cd ${{ secrets.WORK_DIRECTORY }}  
          unzip -o code-zap-jar.zip  
          RUNNER_TRACKING_ID="" && ./deploy.sh  
  
      - name: Verify Deploy Succeed  
        run: |  
          sleep 3  
          pgrep -f 'java -jar.code-zap.*\.jar' || echo "Deploy Failed"

⚡️ 코드zap

프로젝트

규칙 및 정책

공통

백엔드

프론트엔드

매뉴얼

백엔드

기술 문서

백엔드

프론트엔드


Clone this wiki locally