Skip to content

Update main_dj237-api.yml #33

Update main_dj237-api.yml

Update main_dj237-api.yml #33

name: Build and Deploy Django App to ACR
on:
push:
branches:
- main
env:
AZURE_CONTAINER_REGISTRY_LOGIN_SERVER: ${{ secrets.AZURE_CONTAINER_REGISTRY_LOGIN_SERVER }}
AZURE_CONTAINER_REGISTRY_USERNAME: ${{ secrets.AZURE_CONTAINER_REGISTRY_USERNAME }}
AZURE_CONTAINER_REGISTRY_PASSWORD: ${{ secrets.AZURE_CONTAINER_REGISTRY_PASSWORD }}
APP_NAME: ${{ secrets.APP_NAME }}
IMAGE_TAG: ${{ github.sha }}
jobs:
build-and-push:
runs-on: ubuntu-latest
outputs:
image_tag: ${{ steps.get-image-tag.outputs.image_tag }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3.6.1
- name: Get short commit SHA
id: get-image-tag
run: echo "image_tag=$(git rev-parse --short HEAD)" >> $GITHUB_ENV
- name: Log in to Azure Container Registry
uses: azure/docker-login@v2
with:
login-server: ${{ env.AZURE_CONTAINER_REGISTRY_LOGIN_SERVER }}
username: ${{ env.AZURE_CONTAINER_REGISTRY_USERNAME }}
password: ${{ env.AZURE_CONTAINER_REGISTRY_PASSWORD }}
- name: Create .env file from secret
run: echo "${{ secrets.PROJECT_SECRET }}" > .env
- name: Build and tag Docker image
run: |
docker build -f config/docker/Dockerfile -t ${{ env.AZURE_CONTAINER_REGISTRY_LOGIN_SERVER }}/${{ env.APP_NAME }}:latest .
docker tag ${{ env.AZURE_CONTAINER_REGISTRY_LOGIN_SERVER }}/${{ env.APP_NAME }}:latest ${{ env.AZURE_CONTAINER_REGISTRY_LOGIN_SERVER }}/${{ env.APP_NAME }}:${{ env.IMAGE_TAG }}
- name: Push Docker image to ACR
run: |
docker push ${{ env.AZURE_CONTAINER_REGISTRY_LOGIN_SERVER }}/${{ env.APP_NAME }}:latest
docker push ${{ env.AZURE_CONTAINER_REGISTRY_LOGIN_SERVER }}/${{ env.APP_NAME }}:${{ env.IMAGE_TAG }}
deploy:
runs-on: ubuntu-latest
needs: build-and-push
steps:
- name: Log in to Azure Container Registry
uses: azure/docker-login@v2
with:
login-server: ${{ env.AZURE_CONTAINER_REGISTRY_LOGIN_SERVER }}
username: ${{ env.AZURE_CONTAINER_REGISTRY_USERNAME }}
password: ${{ env.AZURE_CONTAINER_REGISTRY_PASSWORD }}
- name: Pull and deploy the pinned Docker image
run: |
docker pull ${{ env.AZURE_CONTAINER_REGISTRY_LOGIN_SERVER }}/${{ env.APP_NAME }}:latest
docker run -d --name ${{ env.APP_NAME }} --restart always -p 8000:8000 ${{ env.AZURE_CONTAINER_REGISTRY_LOGIN_SERVER }}/${{ env.APP_NAME }}:latest