Skip to content

๐Ÿš‘๏ธ ์ตœ์‹  ์ฝ”๋“œ ๋ฐ˜์˜ ์•ˆ๋˜๋Š” ๋ฌธ์ œ ํ•ด๊ฒฐ #32

๐Ÿš‘๏ธ ์ตœ์‹  ์ฝ”๋“œ ๋ฐ˜์˜ ์•ˆ๋˜๋Š” ๋ฌธ์ œ ํ•ด๊ฒฐ

๐Ÿš‘๏ธ ์ตœ์‹  ์ฝ”๋“œ ๋ฐ˜์˜ ์•ˆ๋˜๋Š” ๋ฌธ์ œ ํ•ด๊ฒฐ #32

name: Build, Push Backend Image to ACR and Trigger Deployment
# ์›Œํฌํ”Œ๋กœ์šฐ ํŠธ๋ฆฌ๊ฑฐ ์„ค์ •
on:
push:
branches: [ dev, main ] # dev์™€ main ๋ธŒ๋žœ์น˜์— push๋  ๋•Œ ์‹คํ–‰
workflow_dispatch: {} # GitHub Actions ํƒญ์—์„œ ์ˆ˜๋™์œผ๋กœ ์‹คํ–‰ ๊ฐ€๋Šฅ
jobs:
build-and-push:
runs-on: ubuntu-latest # ์‹คํ–‰ ํ™˜๊ฒฝ ์ง€์ •
outputs:
commit_sha: ${{ github.sha }}
steps:
# 1. ์†Œ์Šค ์ฝ”๋“œ ์ฒดํฌ์•„์›ƒ
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0 # ๋ชจ๋“  ํžˆ์Šคํ† ๋ฆฌ ๊ฐ€์ ธ์˜ค๊ธฐ
ref: ${{ github.ref }} # ํ˜„์žฌ ๋ธŒ๋žœ์น˜์˜ ์ตœ์‹  ์ปค๋ฐ‹ ์‚ฌ์šฉ
clean: true # ์ž‘์—… ๋””๋ ‰ํ† ๋ฆฌ ์ •๋ฆฌ
# 2. Azure ๋กœ๊ทธ์ธ (Service Principal ์‚ฌ์šฉ)
- name: Log in to Azure
uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
# 3. ACR ๋กœ๊ทธ์ธ (Azure ์ž๊ฒฉ ์ฆ๋ช… ์‚ฌ์šฉ)
- name: Log in to ACR
uses: azure/docker-login@v1
with:
login-server: ${{ secrets.ACR_LOGIN_SERVER }}
username: ${{ secrets.ACR_USERNAME }}
password: ${{ secrets.ACR_PASSWORD }}
# 4. Docker ์ด๋ฏธ์ง€ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ์ถ”์ถœ (ํƒœ๊ทธ ์ƒ์„ฑ ๋“ฑ)
- name: Extract metadata (tags, labels) for Docker
id: meta # ์ด step์˜ ์ถœ๋ ฅ์„ ์ฐธ์กฐํ•˜๊ธฐ ์œ„ํ•œ ID
uses: docker/metadata-action@v5
with:
images: ${{ secrets.ACR_LOGIN_SERVER }}/${{ github.ref == 'refs/heads/dev' && secrets.ACR_REPOSITORY_NAME_TEST || secrets.ACR_REPOSITORY_NAME }}
# ์˜ˆ์‹œ ํƒœ๊ทธ: main ๋ธŒ๋žœ์น˜๋ฉด latest, ๊ทธ ์™ธ์—๋Š” ๋ธŒ๋žœ์น˜๋ช…, ๊ทธ๋ฆฌ๊ณ  ํ•ญ์ƒ Git SHA ํƒœ๊ทธ ์ถ”๊ฐ€
tags: |
type=ref,event=branch
type=ref,event=pr
type=sha
type=raw,value=latest,enable={{is_default_branch}}
# 5. Docker ์ด๋ฏธ์ง€ ๋นŒ๋“œ ๋ฐ ACR์— ํ‘ธ์‹œ
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: . # Dockerfile์ด ์žˆ๋Š” ๊ฒฝ๋กœ
file: ./Dockerfile # Dockerfile ๊ฒฝ๋กœ ๋ช…์‹œ (๊ธฐ๋ณธ๊ฐ’)
push: true # ๋นŒ๋“œ ํ›„ ํ‘ธ์‹œ ์‹คํ–‰
tags: ${{ steps.meta.outputs.tags }} # ์œ„ metadata step์—์„œ ์ƒ์„ฑ๋œ ํƒœ๊ทธ ์‚ฌ์šฉ
labels: ${{ steps.meta.outputs.labels }} # ์œ„ metadata step์—์„œ ์ƒ์„ฑ๋œ ๋ผ๋ฒจ ์‚ฌ์šฉ
no-cache: true # ์บ์‹œ ๋น„ํ™œ์„ฑํ™”
build-args: |
BUILD_DATE=$(date +%Y%m%d%H%M%S) # ๋นŒ๋“œ ์‹œ๊ฐ„์„ ์ธ์ž๋กœ ์ „๋‹ฌ
# 6. ACR์— ํ‘ธ์‹œ๋œ ์ด๋ฏธ์ง€ ํ™•์ธ
- name: Verify pushed image
run: |
echo "Pushed image tags:"
echo "${{ steps.meta.outputs.tags }}"
echo "Checking image in ACR..."
echo "Basic tag information:"
az acr repository show-tags --name ${{ secrets.ACR_LOGIN_SERVER }} --repository ${{ github.ref == 'refs/heads/dev' && secrets.ACR_REPOSITORY_NAME_TEST || secrets.ACR_REPOSITORY_NAME }} --output table
call-deploy-workflow:
name: Trigger ACA Deployment
needs: build-and-push
uses: 9git9git/9git-devops/.github/workflows/deploy_to_aca.yml@main
with:
commit_sha: ${{ needs.build-and-push.outputs.commit_sha }}
target_environment: ${{ (github.ref_name == 'main' && 'production') || 'test' }}
app_type: 'backend'
secrets: inherit
# main ๋˜๋Š” dev ๋ธŒ๋žœ์น˜์— push๋  ๋•Œ๋งŒ ๋ฐฐํฌ ์‹คํ–‰
if: github.event_name == 'push' && (github.ref_name == 'main' || github.ref_name == 'dev')