-
Notifications
You must be signed in to change notification settings - Fork 0
164 lines (146 loc) · 5.32 KB
/
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
name: CI
on:
# push:
# branches: [ "main" ]
# paths:
# - app/**
# pull_request:
# branches: [ "main" ]
# paths:
# - app/**
workflow_dispatch:
inputs:
runner-type:
required: true
type: choice
options:
- general-cpu
- gpu
env:
REGISTRY: devghactionacr001.azurecr.io
IMAGE_NAME: demo-fastapi-webapp
STORAGE_NAME: devghactionsac001
STORAGE_CONTAINER_NAME: test
DOWNLOAD_FILENAME: test.txt
BUILDX_CACHE_DIR: /tmp/buildx_cache_dir/
CONTAINER_NAME: my-container
jobs:
continuous-integration:
runs-on: [self-hosted, "${{ inputs.runner-type }}"]
steps:
# 1. Checkout Repository
- name: 1. Checkout Repository
uses: actions/checkout@v4
# 2-1. Setup Python
- name: 2-1. Setup Python
uses: actions/setup-python@v5.2.0
with:
python-version: '3.11.7'
# 2-2. Install Requirements Package and Excute PyTest
- name: 2-2. Install Requirements and Test Application
run: |
pip install -r requirements.txt
pytest app/test_rest.py | tee ${{ github.workspace }}/pytest-results-${{ github.sha }}.txt
# 3-1. Azure Login
- name: 3-1. Azure Login
uses: azure/login@v2
with:
auth-type: IDENTITY # VM System Assigned Identity
tenant-id: ${{ secrets.AZURE_TENANT_ID }}
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }}
# 3-2. Upload PyTest Result to Blob
- name: 3-2. Upload PyTest Result to Blob (AzureCLI)
uses: azure/cli@v2
with:
azcliversion: latest
inlineScript: |
az storage blob upload \
--account-name ${{ env.STORAGE_NAME }} \
--container-name ${{ env.STORAGE_CONTAINER_NAME }} \
--name pytest-results-${{ github.sha }}.txt \
--file ${{ github.workspace }}/pytest-results-${{ github.sha }}.txt \
--auth-mode login \
--overwrite
# 3-3. Upload PyTest Result to GithubActions
- name: 3-3. Upload PyTest Result to GithubActions
uses: actions/upload-artifact@v4
with:
name: pytest-result
path: ${{ github.workspace }}/pytest-results-${{ github.sha }}.txt
# Use always() to always run this step to publish test results when there are test failures
if: ${{ always() }}
# 3-4. Download File From Blob
- name: 3-4. Download File From Blob (AzureCLI)
uses: azure/cli@v2
with:
azcliversion: latest
inlineScript: |
az storage blob download \
--account-name ${{ env.STORAGE_NAME }} \
--container-name ${{ env.STORAGE_CONTAINER_NAME }} \
--name ${{ env.DOWNLOAD_FILENAME }} \
--file "${{ github.workspace }}/${{ env.DOWNLOAD_FILENAME }}" \
--auth-mode login
# 4-1. Set up Docker Buildx
- name: 4-1. Set up Docker Buildx
uses: docker/setup-buildx-action@v3
# 4-2. Docker build (Save image to local) / Use Buildx Local Cache / Save Image to local
- name: 4-2. Docker build (Save image to local)
uses: docker/build-push-action@v6.9.0
with:
context: .
cache-from: type=local,src=${{ env.BUILDX_CACHE_DIR }}
cache-to: type=local,dest=${{ env.BUILDX_CACHE_DIR }},mode=max
platforms: linux/amd64
push: false
outputs: type=docker
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.sha }}
# 5. Docker Run (bind mount)
- name: 5-1. Docker Run (bind mount)
run: |
docker run -d -p 8080:8080 \
--name ${{ env.CONTAINER_NAME }} \
-v "${{ github.workspace }}/${{ env.DOWNLOAD_FILENAME }}":/home/python/${{ env.DOWNLOAD_FILENAME }} \
-e FILEPATH="/home/python/${{ env.DOWNLOAD_FILENAME }}" \
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.sha }}
# 6-1. Curl Test (Container)
- name: 6. Curl Test (Container)
run: |
sleep 5s
max=10;for (( i=1; i <= $max; ++i ));do curl localhost:8080/text >> curl-test-${{ github.sha }}.txt; done
# 6-2. Upload Curl Test Result to Blob
- name: 6-1. Upload Test Result to Blob (AzureCLI)
uses: azure/cli@v2
with:
azcliversion: latest
inlineScript: |
az storage blob upload \
--account-name ${{ env.STORAGE_NAME }} \
--container-name ${{ env.STORAGE_CONTAINER_NAME }} \
--name curl-test-${{ github.sha }}.txt \
--file ${{ github.workspace }}/curl-test-${{ github.sha }}.txt \
--auth-mode login \
--overwrite
# 7. Approve or Deny
exit-continuous-integration:
runs-on: [self-hosted, "${{ inputs.runner-type }}"]
environment: need-approvals
needs: continuous-integration
if: success()
steps:
# 8-1. Login to ACR
- name: 8-1. Docker Login to ACR
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ secrets.AZURE_CLIENT_ID }}
password: ${{ secrets.AZURE_CLIENT_SECRET }}
# 8-2. Push Image to ACR
- name: 8-2. Push Image to ACR
run: |
docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.sha }}
# 9. Stop and Remove Container
- name: 9. Delete Container
run: |
docker stop ${{ env.CONTAINER_NAME }}
docker rm ${{ env.CONTAINER_NAME }}