포스트

[Azure/Spring] Git Action으로 ACR에 push하기

[Azure/Spring] Git Action으로 ACR에 push하기

ACR은 이미 생성했다고 가정…

GitHub → Azure Container Registry

기존 저장소에 ./github 생성

  • ./gitgub/workflows/docker-image.yml 생성

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    
      name: Build and Push Docker Image to ACR
        
      on:
        push:
          branches:
            - main
        workflow_dispatch:   # 수동 실행을 위한 트리거
        
      jobs:
        build-and-push:
          runs-on: ubuntu-latest
        
          steps:
            - name: Checkout code
              uses: actions/checkout@v2
        
            - name: Log in to Azure
              uses: azure/login@v1
              with:
                creds: $  
        
            - name: Log in to Azure Container Registry
              run: az acr login --name $
        
            - name: Set up Docker Buildx
              uses: docker/setup-buildx-action@v1
        
            - name: Build and push Docker image
              uses: docker/build-push-action@v2
              with:
                context: .
                push: true
                tags: $/enjoytrip_springboot:latest
        
    

AZURE_CREDENTIALS이란…

  • Azure에서 보유하는 서비스 프린시플(Service Principal)의 자격 증명을 JSON 형태로 저장한 값
  • Github Action 이나 다른 서비스에서 손 쉽게 Azure 리소스에 접근하게 해주는 인증키의 모음

획득하기 위해서는? (CLI)

1
2
3
az ad sp create-for-rbac --name "<Name>" --role contributor \
    --scopes /subscriptions/YOUR-SUBSCRIPTION-ID/resourceGroups/YOUR-RESOURCE-GROUP \
    --sdk-auth

401 에러 발생

1
2
3
4
ERROR: failed to solve: failed to push ***/enjoytrip_springboot:latest: failed to 
authorize: failed to fetch oauth token: unexpected status from GET request to htt
ps://auth.docker.io/token?scope=repository%3A***%2Fenjoytrip_springboot%3Apull%2Cp
ush&service=registry.docker.io: 401 Unauthorized
  • 기존의 방식에서 401 에러발생
  • az acr login --name $ 방식의 CLI는 사내망에서 동작하지 않음

엑세스 키 - 관리 사용자 방식으로 변경

ACR → 설정 → 엑세스 키 → 관리사용자 체크 방식 → ID / PASSWORD 방식 설정

ID/PASSWORD 방식으로 변경

1
2
3
4
5
6
7
  # ACR에 로그인 (CLI 환경은 사내에서 되지 않음)
- name: Log in to Azure
  uses: azure/docker-login@v1
  with:
    login-server: $
    username: $
    password: $

최종 docker-image.yml

  • tag : 현재 시간으로 설정
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
name: Build and Push Docker Image to ACR

on:
  push:
    branches:
      - main
  workflow_dispatch:   # 수동 실행을 위한 트리거

jobs:
  build-and-push:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

        # ACR에 로그인 (CLI 환경은 사내에서 되지 않음)
      - name: Log in to Azure
        uses: azure/docker-login@v1
        with:
          login-server: $
          username: $
          password: $

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1

        # 현재 시간 기준으로 태그 생성
      - name: Generate tag based on current time
        run: echo "IMAGE_TAG=$(date +'%Y%m%d%H%M%S')" >> $GITHUB_ENV

      - name: Build and push to ACR
        uses: docker/build-push-action@v6
        with:
          context: .
          push: true
          tags: $/jinni:$

##

이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.