Skip to content

Commit 34367ed

Browse files
authored
Merge pull request #28 from 9git9git/SCRUM-126-DO-백엔드-GitHub-Actions-작성
Scrum 126 do 백엔드 GitHub actions 작성
2 parents a6e0a7a + b06ee88 commit 34367ed

File tree

4 files changed

+126
-0
lines changed

4 files changed

+126
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
name: Build and Push Backend Image to ACR
2+
3+
# 워크플로우 트리거 설정
4+
on:
5+
push:
6+
branches: [ dev, main ] # dev와 main 브랜치에 push될 때 실행
7+
workflow_dispatch: {} # GitHub Actions 탭에서 수동으로 실행 가능
8+
9+
jobs:
10+
build-and-push:
11+
runs-on: ubuntu-latest # 실행 환경 지정
12+
steps:
13+
# 1. 소스 코드 체크아웃
14+
- name: Checkout repository
15+
uses: actions/checkout@v4
16+
17+
# 2. Azure 로그인 (Service Principal 사용)
18+
- name: Log in to Azure
19+
uses: azure/login@v1
20+
with:
21+
creds: ${{ secrets.AZURE_CREDENTIALS }}
22+
23+
# 3. ACR 로그인 (Azure 자격 증명 사용)
24+
- name: Log in to ACR
25+
uses: azure/docker-login@v1
26+
with:
27+
login-server: ${{ secrets.ACR_LOGIN_SERVER }}
28+
username: ${{ secrets.ACR_USERNAME }}
29+
password: ${{ secrets.ACR_PASSWORD }}
30+
31+
# 4. Docker 이미지 메타데이터 추출 (태그 생성 등)
32+
- name: Extract metadata (tags, labels) for Docker
33+
id: meta # 이 step의 출력을 참조하기 위한 ID
34+
uses: docker/metadata-action@v5
35+
with:
36+
images: ${{ secrets.ACR_LOGIN_SERVER }}/${{ github.ref == 'refs/heads/dev' && secrets.ACR_REPOSITORY_NAME_TEST || secrets.ACR_REPOSITORY_NAME }}
37+
# 예시 태그: main 브랜치면 latest, 그 외에는 브랜치명, 그리고 항상 Git SHA 태그 추가
38+
tags: |
39+
type=ref,event=branch
40+
type=ref,event=pr
41+
type=sha
42+
type=raw,value=latest,enable={{is_default_branch}}
43+
44+
# 5. Docker 이미지 빌드 및 ACR에 푸시
45+
- name: Build and push Docker image
46+
uses: docker/build-push-action@v5
47+
with:
48+
context: . # Dockerfile이 있는 경로
49+
file: ./Dockerfile # Dockerfile 경로 명시 (기본값)
50+
push: true # 빌드 후 푸시 실행
51+
tags: ${{ steps.meta.outputs.tags }} # 위 metadata step에서 생성된 태그 사용
52+
labels: ${{ steps.meta.outputs.labels }} # 위 metadata step에서 생성된 라벨 사용
53+
54+
# 6. ACR에 푸시된 이미지 확인
55+
- name: Verify pushed image
56+
run: |
57+
echo "Pushed image tags:"
58+
echo "${{ steps.meta.outputs.tags }}"
59+
echo "Checking image in ACR..."
60+
echo "Basic tag information:"
61+
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
92.6 KB
Loading
18.9 KB
Loading
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# GitHub Actions로 Azure Container Registry에 이미지 업로드하기
2+
3+
## 1. 준비물
4+
5+
- Azure Service Principal에 대한 정보
6+
7+
## 2. GitHub 저장소 Secrets 설정
8+
9+
> 원래 Secrets에 저장되는 값들이라 공개되면 안되지만 추후 학습을 위해 공개합니다. 중요한 값은 설명만 작성하였습니다.
10+
11+
### 2.1. Azure Service Principal 정보
12+
13+
- GitHub 저장소의 `Settings` > `Secrets and variables` > `Actions` 메뉴로 이동하여 다음 Secrets를 추가합니다.
14+
- `New repository secret` 버튼을 클릭하여 Secrets를 추가할 수 있습니다.
15+
16+
![GitHub Actions Secrets 설정](./images/make-github-actions-for-docker-image-upload/01.png)
17+
18+
![GitHub Actions Secrets 설정](./images/make-github-actions-for-docker-image-upload/02.png)
19+
20+
- `AZURE_CREDENTIALS`
21+
22+
```json
23+
{
24+
"clientId": "<Application (client) ID>",
25+
"clientSecret": "<서비스 주체를 생성할 때 얻은 비밀값>",
26+
"subscriptionId": "<Azure 구독 ID>",
27+
"tenantId": "<Directory (tenant) ID>"
28+
}
29+
```
30+
31+
### 2.2. Azure Container Registry 정보
32+
33+
> Azure Portal에서 ACR 리소스에 들어가면 `설정` > `액세스 키` 메뉴로 이동하여 다음 정보를 확인할 수 있습니다.
34+
35+
- `ACR_LOGIN_SERVER`
36+
37+
```
38+
gugitcontainerregistry.azurecr.io
39+
```
40+
41+
- `ACR_USERNAME`
42+
43+
```
44+
gugitContainerRegistry
45+
```
46+
47+
- `ACR_PASSWORD`
48+
49+
```
50+
# password 사용하면 된다.
51+
```
52+
53+
### 2.3. 이미지 정보
54+
55+
- `ACR_REPOSITORY_NAME_TEST`
56+
57+
```
58+
gugit-backend-test
59+
```
60+
61+
- `ACR_REPOSITORY_NAME`
62+
63+
```
64+
gugit-backend
65+
```

0 commit comments

Comments
 (0)