Azure/CICD

[AKS] GitHub Actions를 사용하여 AKS에 컨테이너 빌드, 테스트 및 배포

bbiyak2da 2025. 2. 25. 14:22

GitHub Action

GitHub Actions를 사용하면 자동화된 소프트웨어 개발 수명 주기 워크플로를 유연성 있게 빌드할 수 있습니다.

여러 Kubernetes 작업을 사용하여 GitHub Actions를 통해 ACR(Azure Container Registry)에서 AKS(Azure Kubernetes Service)로 컨테이너에 배포할 수 있습니다.

 

 

GitHub Actions documentation - GitHub Docs

Automate, customize, and execute your software development workflows right in your repository with GitHub Actions. You can discover, create, and share actions to perform any job you'd like, including CI/CD, and combine actions in a completely customized wo

docs.github.com

 

사전 작업

 

# AKS 자격증명 생성

az aks get-credentials --resource-group <리소스 그룹명> --name <aks 명>

 

# ACR 연동

az aks update --name <aks 명> --resource-group <리소스 그룹명> --attach-acr <acr 명>

 

 

내 GitHub Repository에 예제 코드 포크

 

# Repositroy 포크 및 업데이트

 

 

https://github.com/Azure-Samples/azure-voting-app-redis

 

GitHub - Azure-Samples/azure-voting-app-redis: Azure voting app used in docs.

Azure voting app used in docs. Contribute to Azure-Samples/azure-voting-app-redis development by creating an account on GitHub.

github.com

 

# azure-vote-all-in-one-redis.yaml 파일 업데이트

...
      containers:
      - name: azure-vote-front
        image: <registryName>.azurecr.io/azuredocs/azure-vote-front:v1
...

 

registryName에 내 acr 명으로 바꿔준다.

 

Secret 생성

 

# Service Principal 생성

az ad sp create-for-rbac --name "ghActionAzureVote" --scope /subscriptions/<SUBSCRIPTION_ID>/resourceGroups/<RESOURCE_GROUP> --role Contributor --json-auth

 

az ad sp create-for-rbac 명령을 사용하여 Contributor 역할로 리소스 그룹에 액세스할 서비스 주체를 만듭니다.

<SUBSCRIPTION_ID>를 Azure 계정의 구독 ID로 바꾸고,

<RESOURCE_GROUP>을 ACR이 포함된 리소스 그룹의 이름으로 바꿉니다.

{
  "clientId": <clientId>,
  "clientSecret": <clientSecret>,
  "subscriptionId": <subscriptionId>,
  "tenantId": <tenantId>,
  ...
}

 

출력은 다음 예제 출력과 비슷하게 됩니다.

 

# Secret 생성

 

fork한 리포지토리 > Settings > Secrets and Variables 내 Actions 클릭

 

아래 표를 참조해 Secret들을 생성합니다.

 

AZURE_CREDENTIALS az ad sp create-for-rbac 명령의 전체 JSON 출력
service_principal clientId 값
service_principal_password clientSecret 값
subscription subscriptionId
tenant tenantId
registry Azure Container Registry 명
repository azuredocs
resource_group 리소스 그룹명
cluster_name AKS 명

 

작업 파일 생성

 

# .github/workflows/main.yml 생성

name: build_deploy_aks
on:
  push:
    paths:
      - "azure-vote/**"
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout source code 
        uses: actions/checkout@v3
      - name: ACR build
        id: build-push-acr
        uses: azure/acr-build@v1
        with:
          service_principal: ${{ secrets.service_principal }}
          service_principal_password: ${{ secrets.service_principal_password }}
          tenant: ${{ secrets.tenant }}
          registry: ${{ secrets.registry }}
          repository: ${{ secrets.repository }}
          image:  azure-vote-front
          folder: azure-vote
          branch: master
          tag: ${{ github.sha }}
      - name: Azure login
        id: login
        uses: azure/login@v1.4.3
        with:
          creds: ${{ secrets.AZURE_CREDENTIALS }}
      - name: Set AKS context
        id: set-context
        uses: azure/aks-set-context@v3
        with:
          resource-group: '${{ secrets.resource_group }}' 
          cluster-name: '${{ secrets.cluster_name }}'
      - name: Setup kubectl
        id: install-kubectl
        uses: azure/setup-kubectl@v3
      - name: Deploy to AKS
        id: deploy-aks
        uses: Azure/k8s-deploy@v4
        with:
          namespace: 'default'
          manifests: |
             azure-vote-all-in-one-redis.yaml
          images: '${{ secrets.registry }}.azurecr.io/${{ secrets.repository }}/azure-vote-front:${{ github.sha }}'
          pull-images: false

 

리포지토리에서 .github/workflows/main.yml을 만들고 다음 콘텐츠를 붙여넣습니다.

 

  • on 섹션에는 작업을 트리거하는 이벤트가 포함됩니다. 파일 예에서는 변경 내용이 azure-vote 디렉터리에 푸시될 때 작업이 트리거됩니다.
  • steps 섹션에는 각각의 고유한 작업이 포함되어 있습니다.

# .github/workflows/main.yml 파일을 리포지토리에 커밋

 

# 작업이 작동하는지 확인하려면 다음 콘텐츠로 azure-vote/azure-vote/config_file.cfg를 업데이트 후 저장

# UI Configurations
TITLE = 'Azure Voting App'
VOTE1VALUE = 'Fish'
VOTE2VALUE = 'Dogs'
SHOWHOST = 'false'

 

# 업데이트된 azure-vote/azure-vote/config_file.cfg을 리포지토리에 커밋

 

# 리포지토리에서 작업을 선택하고 워크플로가 실행 중인지 확인

 

 

# 애플리케이션 접근

LoadBalancer의 External IP로 접근한다.


[참고 문서]

https://learn.microsoft.com/ko-kr/azure/aks/kubernetes-action