CI #52
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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-on-cpu: | |
runs-on: [self-hosted, "${{ inputs.runner-type }}"] | |
steps: | |
# 1. Checkout Repo | |
- uses: actions/checkout@v4 | |
# 2. Setup Python | |
- name: Setup Python | |
uses: actions/setup-python@v5.2.0 | |
with: | |
python-version: '3.11.7' | |
# 3. Install Requirements and Test Application | |
- name: Install Requirements and Test Application | |
run: | | |
pip install -r requirements.txt | |
pytest app/test_rest.py | tee ${{ github.workspace }}/test-results-${{ github.sha }}.txt | |
# 4. Upload Test Result to Blob | |
- name: Upload Test Result to Blob - 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 }} | |
- name: Upload Test Result to Blob - Azure CLI script | |
uses: azure/cli@v2 | |
with: | |
azcliversion: latest | |
inlineScript: | | |
az storage blob upload \ | |
--account-name ${{ env.STORAGE_NAME }} \ | |
--container-name ${{ env.STORAGE_CONTAINER_NAME }} \ | |
--name test-results-${{ github.sha }}.txt \ | |
--file ${{ github.workspace }}/test-results-${{ github.sha }}.txt \ | |
--auth-mode login \ | |
--overwrite | |
# 4. Upload Test Reulst as Artifact to GithubActions | |
- name: Upload pytest test results - to GithubActions | |
uses: actions/upload-artifact@v4 | |
with: | |
name: pytest-result | |
path: ${{ github.workspace }}/test-results-${{ github.sha }}.txt | |
# Use always() to always run this step to publish test results when there are test failures | |
if: ${{ always() }} | |
# 5. Docker build and push (push to ACR) - Setup Docker | |
- name: Docker build and push (push to ACR) - Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
# 5. Docker build and push (push to ACR) - Login to ACR | |
- name: Docker build and push (push to ACR) - Docker Login to ACR | |
uses: docker/login-action@v3 | |
with: | |
registry: ${{ env.REGISTRY }} | |
username: ${{ secrets.AZURE_CLIENT_ID }} | |
password: ${{ secrets.AZURE_CLIENT_SECRET }} | |
# 5. Docker build and push (push to ACR) / Use Buildx Local Cache | |
- name: Docker build and push (push to ACR) - Build and Push Docker image to ACR | |
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 | |
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.sha }} | |
# 6. Download Test Tool From Blob | |
- name: Download Test Tool From Blob - Azure CLI script | |
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 | |
# 7. Docker run with volume (with download file) (image from ACR) | |
- name: Docker run with volume (with download file) (image from ACR) | |
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 }} | |
# 8. Test Application | |
- name: Docker run with volume (with download file) (image from ACR) | |
run: | | |
sleep 5s | |
max=10;for (( i=1; i <= $max; ++i ));do curl localhost:8080/text >> curl-test-${{ github.sha }}.txt; done | |
# 9. Approve or Deny / Notify | |
exit-continuous-integration-on-cpu: | |
runs-on: [self-hosted, "${{ inputs.runner-type }}"] | |
environment: need-approvals | |
needs: continuous-integration-on-cpu | |
if: success() | |
steps: | |
# 10. Stop and Remove Container | |
- name: Push Image to ACR | |
run: | | |
docker push ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.sha }} | |
# 11. Stop and Remove Container | |
- name: Delete Container | |
run: | | |
docker stop ${{ env.CONTAINER_NAME }} | |
docker rm ${{ env.CONTAINER_NAME }} |