Skip to content

Commit

Permalink
Add azure as an deployment option and add azure deployment script
Browse files Browse the repository at this point in the history
  • Loading branch information
kumaranvpl committed Nov 28, 2024
1 parent abd3a70 commit eec1307
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ jobs:
python-version: ["3.12", "3.11", "3.10"]
app-type: ["fastapi+mesop", "mesop", "nats+fastapi+mesop", "fastapi"]
authentication: ["basic", "google", "none"]
deployment: ["fly.io", "azure"]
fail-fast: false
runs-on: ubuntu-latest
timeout-minutes: 15
Expand All @@ -42,7 +43,7 @@ jobs:
run: pip install cookiecutter

- name: Test Cookiecutter
run: ./scripts/test_cookiecutter.sh ${{ matrix.app-type }} ${{ matrix.python-version }} ${{ matrix.authentication }}
run: ./scripts/test_cookiecutter.sh ${{ matrix.app-type }} ${{ matrix.python-version }} ${{ matrix.authentication }} ${{ matrix.deployment }}

# https://github.com/marketplace/actions/alls-green#why
check: # This job does nothing and is only used for the branch protection
Expand Down
3 changes: 2 additions & 1 deletion cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"project_slug": "{{ cookiecutter.project_name.lower().replace(' ', '_').replace('-', '_') }}",
"app_type": ["fastapi+mesop", "mesop", "nats+fastapi+mesop", "fastapi"],
"python_version": ["3.12", "3.11", "3.10"],
"authentication": ["basic", "google", "none"]
"authentication": ["basic", "google", "none"],
"deployment": ["fly.io", "azure"],
}
5 changes: 5 additions & 0 deletions hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
{% if 'nats' not in cookiecutter.app_type %}".devcontainer/nats_server.conf",{% endif %}
{% if cookiecutter.app_type == 'fastapi' %}"{{cookiecutter.project_slug}}/deployment/main_2_mesop.py",{% endif %}
{% if cookiecutter.authentication != 'google' %}"deployment/",{% endif %}
{% if cookiecutter.deployment != 'fly.io' %}"scripts/register_to_fly_io.sh",{% endif %}
{% if cookiecutter.deployment != 'fly.io' %}"scripts/deploy_to_fly_io.sh",{% endif %}
{% if cookiecutter.deployment != 'fly.io' %}"fly.toml",{% endif %}
{% if cookiecutter.deployment != 'fly.io' %}".github/workflows/deploy_to_fly_io.yml",{% endif %}
{% if cookiecutter.deployment != 'azure' %}"scripts/deploy_to_azure.sh",{% endif %}
]

for path in REMOVE_PATHS:
Expand Down
14 changes: 7 additions & 7 deletions scripts/test_cookiecutter.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@
set -o errexit

# Accept two arguments: app-type and python-version
echo -e "\033[32mGenerating project using cookiecutter template with app-type: $1, python-version: $2 and authentication: $3\033[0m"
rm -rf generated/$1-$2-$3
cookiecutter -f --no-input --output-dir generated/$1-$2-$3/ ./ app_type=$1 python_version=$2 authentication=$3
echo -e "\033[32mGenerating project using cookiecutter template with app-type: $1, python-version: $2, authentication: $3 and deployment: $4\033[0m"
rm -rf generated/$1-$2-$3-$4
cookiecutter -f --no-input --output-dir generated/$1-$2-$3-$4/ ./ app_type=$1 python_version=$2 authentication=$3 deployment=$4

# Install generated project's dependencies
echo -e "\033[32mInstalling dependencies for the generated project\033[0m"
cd generated/$1-$2-$3/my_fastagency_app && pip install -e .[dev] && cd ../../..
cd generated/$1-$2-$3-$4/my_fastagency_app && pip install -e .[dev] && cd ../../..

# Initialize git in the generated project(needed for pre-commit)
echo -e "\033[32mInitializing git in the generated project\033[0m"
cd generated/$1-$2-$3/my_fastagency_app && git init && git add . && cd ../../..
cd generated/$1-$2-$3-$4/my_fastagency_app && git init && git add . && cd ../../..
# uncomment this for debugging
# cd generated/$1-$2-$3/my_fastagency_app && git commit -m "init" --no-verify && cd ../../..
# cd generated/$1-$2-$3-$4/my_fastagency_app && git commit -m "init" --no-verify && cd ../../..

# Run pre-commit
echo -e "\033[32mRunning pre-commit\033[0m"
cd generated/$1-$2-$3/my_fastagency_app && pre-commit run --show-diff-on-failure --color=always --all-files && cd ../../..
cd generated/$1-$2-$3-$4/my_fastagency_app && pre-commit run --show-diff-on-failure --color=always --all-files && cd ../../..
77 changes: 77 additions & 0 deletions {{cookiecutter.project_slug}}/scripts/deploy_to_azure.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/bin/bash

# Script to deploy to Azure Container Apps using Azure CLI

# Variables
RESOURCE_GROUP="{{ cookiecutter.project_slug.replace("_", "-") }}-rg"
CONTAINER_APP_NAME="{{ cookiecutter.project_slug.replace("_", "-") }}"
LOCATION="westeurope"
ACR_NAME="{{ cookiecutter.project_slug.replace("_", "-") }}-acr"


echo -e "\033[0;32mChecking if already logged into Azure\033[0m"
if ! az account show > /dev/null 2>&1; then
echo -e "\033[0;32mLogging into Azure\033[0m"
az login
else
echo -e "\033[0;32mAlready logged into Azure\033[0m"
fi

# az extension add --name containerapp --upgrade
# az provider register --namespace Microsoft.App
# az provider register --namespace Microsoft.OperationalInsights

echo -e "\033[0;32mCreating resource group if it doesn't exists already\033[0m"
az group create --name $RESOURCE_GROUP --location $LOCATION

echo -e "\033[0;32mCreating azure container registry if it doesn't exists already\033[0m"
az acr create --resource-group $RESOURCE_GROUP --name $ACR_NAME --sku Basic --admin-enabled true
rm ~/.docker/config.json
az acr login --name $ACR_NAME

echo -e "\033[0;32mBuilding and pushing docker image to azure container registry\033[0m"
docker build -t $ACR_NAME.azurecr.io/${CONTAINER_APP_NAME}:latest -f docker/Dockerfile .
docker push $ACR_NAME.azurecr.io/${CONTAINER_APP_NAME}:latest

echo -e "\033[0;32mChecking if container app environment exists\033[0m"
if ! az containerapp env show --name "$CONTAINER_APP_NAME-env" --resource-group $RESOURCE_GROUP > /dev/null 2>&1; then
echo -e "\033[0;32mCreating container app environment\033[0m"
az containerapp env create \
--name "$CONTAINER_APP_NAME-env" \
--resource-group $RESOURCE_GROUP \
--location $LOCATION
else
echo -e "\033[0;32mContainer app environment already exists\033[0m"
fi

echo -e "\033[0;32mCreating container app\033[0m"
az containerapp create \
--name $CONTAINER_APP_NAME \
--resource-group $RESOURCE_GROUP \
--environment "$CONTAINER_APP_NAME-env" \
--image $ACR_NAME.azurecr.io/${CONTAINER_APP_NAME}:latest \
--target-port {% if cookiecutter.app_type == 'fastapi' %}8008{% else %}8888{% endif %} \
--ingress 'external' \
--query properties.configuration.ingress.fqdn \
--registry-server $ACR_NAME.azurecr.io \
--cpu 0.5 \
--memory 1Gi \
--min-replicas 0 \
--max-replicas 1 \
--env-vars OPENAI_API_KEY=$OPENAI_API_KEY
{% if "nats" in cookiecutter.app_type %}
# echo -e "\033[0;32mUpdating nats port in container app\033[0m"
# az containerapp update \
# --name $CONTAINER_APP_NAME \
# --resource-group $RESOURCE_GROUP \
# --add-ports 8000{%- endif %}{% if "fastapi" in cookiecutter.app_type %}
# echo -e "\033[0;32mUpdating fastapi port in container app\033[0m"
# az containerapp update \
# --name $CONTAINER_APP_NAME \
# --resource-group $RESOURCE_GROUP \
# --add-ports 8008{%- endif %}{% if "mesop" in cookiecutter.app_type %}
# echo -e "\033[0;32mUpdating fastapi port in container app\033[0m"
# az containerapp update \
# --name $CONTAINER_APP_NAME \
# --resource-group $RESOURCE_GROUP \
# --add-ports 8888{%- endif %}

0 comments on commit eec1307

Please sign in to comment.