Github Actions로 Azure Container Apps 자동 배포

Eugene·2025년 4월 16일

Azure Container App & github action workflow 예시 코드

unit test -> e2e test -> deploy

name: Trigger auto deployment for my-project

# When this action will be executed
on:
  # Automatically trigger it when detected changes in repo
  push:
    branches: [dev]
    paths:
      - '**'
      - '.github/workflows/my-project-AutoDeployTrigger.yml'

  # Allow manual trigger
  workflow_dispatch:

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    permissions:
      id-token: write #This is required for requesting the OIDC JWT Token
      contents: read #Required when GH token is used to authenticate with private repo

		# e2e 테스트용 mysql과 mongoDB 도커 컨테이너 실행
    services:
      mysql:
        image: mysql:8.0.27
        options: >-
          --health-cmd "mysqladmin ping --silent" 
          --health-interval 10s
          --health-timeout 5s
          --health-retries 3
        ports:
          - 3306:3306
        env:
          MYSQL_ROOT_PASSWORD: password
          MYSQL_DATABASE: main
      mongo:
        image: mongo
        ports:
          - 27017:27017

    steps:
		    # 브렌치 체크아웃
      - name: Checkout to the branch
        uses: actions/checkout@v2   
				# 노드 셋업
      - name: Set up Node.js
        uses: actions/setup-node@v2
        with:
          node-version: '20.17.0'
      - name: Debug .npmrc file
        run: cat $GITHUB_WORKSPACE/.npmrc
				# npm 의존성 설치
      - name: Install dependencies
        run: npm install
				# .env.jest-unit 파일 생성
      - name: create .env.jest-unit file
        run: |
          echo -e "process.env.NODE_ENV='local'\nprocess.env.TEST_SN='test@123' \nprocess.env.OTHER_TEST_SN='otherTest@123' \nprocess.env.TEST_NAME='test_name' \nprocess.env.TEST_EMAIL='test@test.com' \nprocess.env.TEST_PHONE='+821000000000'" > $GITHUB_WORKSPACE/.env.jest-unit
				
      - name: Debug .env.jest-unit file
        run: cat $GITHUB_WORKSPACE/.env.jest-unit
				# test 코드 실행
      - name: Run Jest tests
        run: npm run test
        continue-on-error: false
				# mysql 실행 대기
      - name: Wait for MySQL to be ready
        run: |
          until mysqladmin ping -h"127.0.0.1" --silent; do
            echo "Waiting for MySQL..."
            sleep 5
          done
				# mysql 리셋
      - name: Reset database
        run: npm run typeorm:reset
				# .env 파일 생성
      - name: create .env file
        run: |
          echo -e "MONGO_URL=${{ secrets.MONGO_URL }}\nTYPE_ORM_HOST=${{ secrets.TYPE_ORM_HOST }}\nTYPE_ORM_PORT=${{ secrets.TYPE_ORM_PORT }}\nTYPE_ORM_USERNAME=${{ secrets.TYPE_ORM_USERNAME }}\nTYPE_ORM_PASSWORD=${{ secrets.TYPE_ORM_PASSWORD }}\nTYPE_ORM_DATABASE=${{ secrets.TYPE_ORM_DATABASE }}\nJWT_SECRET=${{ secrets.JWT_SECRET }}" > $GITHUB_WORKSPACE/.env

      - name: Debug .env file
        run: cat $GITHUB_WORKSPACE/.env
				# e2e 테스트 실행
      - name: Run Jest e2e test
        run: npm run test:e2e
        continue-on-error: false
				# 애저 로그인
      - name: Azure Login
        uses: azure/login@v1
        with:
          client-id: ${{ secrets.AZURE_CLIENT_ID }}
          tenant-id: ${{ secrets.AZURE_TENANT_ID }}
          subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
				# 최신 이미지 태그 가져오기
      - name: Get latest image tag from ACR
        id: get_latest_image
        run: |
          LATEST_TAG=$(az acr repository show-tags --name myproject --repository server --orderby time_desc --top 1 --output tsv)
          echo "LATEST_TAG=$LATEST_TAG"
          if [ -z "$LATEST_TAG" ]; then
            NEW_TAG=1
          else
            if [ "$LATEST_TAG" == "latest" ]; then
              NEW_TAG=1
            else
              NEW_TAG=$((LATEST_TAG + 1))
            fi
          fi
          echo "::set-output name=NEW_TAG::$NEW_TAG"
        shell: bash
				# 배포: 애저 레지스트리에 이미지 빌드 & 푸쉬
      - name: Build and push container image to registry
        uses: azure/container-apps-deploy-action@v2
        with:
          appSourcePath: ${{ github.workspace }}
          # _dockerfilePathKey_ 프로퍼티 키/벨류 수정 필요.
          _dockerfilePathKey_: _dockerfilePath_
          registryUrl: myproject.azurecr.io
          registryUsername: ${{ secrets.REGISTRY_USERNAME }}
          registryPassword: ${{ secrets.REGISTRY_PASSWORD }}
          containerAppName: my-project
          resourceGroup: Dev
          imageToBuild: myproject.azurecr.io/server:${{ steps.get_latest_image.outputs.NEW_TAG }}
          _buildArgumentsKey_: |
            _buildArgumentsValues_
profile
서버 개발자

0개의 댓글