diff --git a/e2e_samples/parking_sensors/.devcontainer/.envtemplate b/e2e_samples/parking_sensors/.devcontainer/.envtemplate index 1afbe808b..35df8517a 100644 --- a/e2e_samples/parking_sensors/.devcontainer/.envtemplate +++ b/e2e_samples/parking_sensors/.devcontainer/.envtemplate @@ -26,6 +26,9 @@ AZDO_ORGANIZATION_URL= # OPTIONAL. Environment variables for deployment script. # Uncomment to set values. +# Define if Dev(example: #ENV_DEPLOY=1) or Dev and Stage (example: #ENV_DEPLOY=2) or or Dev, Stage and Prod(example: #ENV_DEPLOY=3) will be deployed. +# ENV_DEPLOY= + # Azure location to deploy resources. Default: westus # AZURE_LOCATION= diff --git a/e2e_samples/parking_sensors/README.md b/e2e_samples/parking_sensors/README.md index 56a0101a0..352f7b996 100644 --- a/e2e_samples/parking_sensors/README.md +++ b/e2e_samples/parking_sensors/README.md @@ -277,6 +277,17 @@ Set up the environment variables as specified, fork the GitHub repository, and l Optionally, set the following environment variables: + - **ENV_DEPLOY**- Specifies the number of environments to deploy. + + - If the variable is set as shown in the optional configuration below, the prompt will be skipped. + - If not configured, a prompt will display the following options. + + - The options are: + + - **Option 1**: Deploy only to the development environment (Dev). Optional configuration for environment variable set #ENV_DEPLOY=1. + - **Option 2**: Deploy to both development (Dev) and staging (Stage) environments. Optional configuration for environment variable #ENV_DEPLOY=2. + - **Option 3** : Deploy to development (Dev), staging (Stage), and production (Prod) environments. Optional configuration for environment variable #ENV_DEPLOY=3 + - **AZURE_LOCATION** - Azure location to deploy resources. *Default*: `westus`. - **DEPLOYMENT_ID** - string appended to all resource names. This is to ensure uniqueness of azure resource names. *Default*: random five character string. - **AZDO_PIPELINES_BRANCH_NAME** - git branch where Azure DevOps pipelines definitions are retrieved from. *Default*: main. @@ -298,7 +309,11 @@ Set up the environment variables as specified, fork the GitHub repository, and l 2. **Deploy Azure resources** - `cd` into the `e2e_samples/parking_sensors` folder of the repo. - Run `./deploy.sh`. - - The login process for deployment is interactive. When you run the script **deploy.sh**, a browser window will be open, prompting you to log in to Azure. If there is an open session from a previous deployment, it may log you out and request you to log in again. + - The login process for deployment is interactive. When you run the script **deploy.sh**, a browser window will be open, prompting you to log in to Azure. If there is an open session from a previous deployment, it may log you out and request you to log in again- . + - During deployment, you will be presented with three options if the optional environment variable #ENV_DEPLOY for deployment is not set. Answer the prompt by typing 1,2 or 3 from the keyboard according to the options shown. Keep in mind that deployments are standalone and cannot be performed incrementally: + - **Option 1**: Deploy only to the development environment (Dev). Note that this option does not deploy release pipelines, as a minimum of two environments is required for pipeline releases. + - **Option 2**: Deploy to both development (Dev) and staging (Stage) environments. + - **Option 3** (Or Press Enter): Deploy to development (Dev), staging (Stage), and production (Prod) environments. - This may take around **~30mins or more** to run end to end. So grab yourself a cup of coffee... ☕ But before you do so keep the following in mind: - You might encounter deployment issues if the script attempts to create a Key Vault that conflicts with a previously soft-deleted Key Vault. In such cases, the deployment script may prompt you to confirm the purge of the previously deleted Key Vault. - There are 3 points in time where you will need to authenticate to the databricks workspace, before the script continues to run. You will find the following message for the deployment of the dev, stage and production environments. Click the link highlighted in green, consent to authenticate to the databricks workspace and when the workspace opens successfully, return to the deployment windows and press Enter to continue: ![image](docs/images/databricks_ws.png) diff --git a/e2e_samples/parking_sensors/deploy.sh b/e2e_samples/parking_sensors/deploy.sh index fcaf95430..0daefc4ec 100755 --- a/e2e_samples/parking_sensors/deploy.sh +++ b/e2e_samples/parking_sensors/deploy.sh @@ -24,27 +24,29 @@ set -o nounset . ./scripts/verify_prerequisites.sh - -project=mdwdops # CONSTANT - this is prefixes to all resources of the Parking Sensor sample +# CONSTANT - this is prefixes to all resources of the Parking Sensor sample +project=mdwdops github_repo_url="https://github.com/$GITHUB_REPO" +#Ask the user the following options: +#### +## 1) Only Dev +## 2) Dev and Stage +## 3) All +#### -################### -# DEPLOY ALL FOR EACH ENVIRONMENT +if [ -z "$ENV_DEPLOY" ]; then + read -r -p "Do you wish to deploy:"$'\n'" 1) Dev Environment Only?"$'\n'" 2) Dev and Stage Environments?"$'\n'" 3) Dev, Stage and Prod (Or Press Enter)?"$'\n'" Choose 1, 2 or 3: " ENV_DEPLOY + log "Option Selected: $ENV_DEPLOY" "info" +fi -for env_name in dev stg prod; do # dev stg prod - PROJECT=$project \ - DEPLOYMENT_ID=$DEPLOYMENT_ID \ - ENV_NAME=$env_name \ - AZURE_LOCATION=$AZURE_LOCATION \ - AZURE_SUBSCRIPTION_ID=$AZURE_SUBSCRIPTION_ID \ - AZURESQL_SERVER_PASSWORD=$AZURESQL_SERVER_PASSWORD \ - bash -c "./scripts/deploy_infrastructure.sh" # includes AzDevOps Azure Service Connections and Variable Groups -done +# Call the deploy function +deploy_infrastructure_environment "$ENV_DEPLOY" "$project" ################### # Deploy AzDevOps Pipelines +################### # Create AzDo Github Service Connection -- required only once for the entire deployment PROJECT=$project \ diff --git a/e2e_samples/parking_sensors/scripts/common.sh b/e2e_samples/parking_sensors/scripts/common.sh index 7a235d81d..ad5195560 100755 --- a/e2e_samples/parking_sensors/scripts/common.sh +++ b/e2e_samples/parking_sensors/scripts/common.sh @@ -148,3 +148,49 @@ delete_azdo_service_connection_principal(){ log "Deleted Service Principal: $spnAppObjId" || log "Failed to delete Service Principal: $spnAppObjId" } + +deploy_infrastructure_environment() { + ##function to allow user deploy enviromnents + ## 1) Only Dev + ## 2) Dev and Stage + ## 3) Dev, Stage and Prod + ##Default is option 3. + ENV_DEPLOY=${1:-3} + project=${2:-mdwdops} + case $ENV_DEPLOY in + 1) + log "Deploying Dev Environment only..." + env_names="dev" + ;; + 2) + log "Deploying Dev and Stage Environments..." + env_names="dev stg" + ;; + 3) + log "Full Deploy: Dev, Stage and Prod Environments..." + env_names="dev stg prod" + ;; + *) + log "Invalid choice. Exiting..." "warning" + exit + ;; + esac + + # Loop through the environments and deploy + for env_name in $env_names; do + echo "Currently deploying to the environment: $env_name" + export PROJECT=$project + export DEPLOYMENT_ID=$DEPLOYMENT_ID + export ENV_NAME=$env_name + export AZURE_LOCATION=$AZURE_LOCATION + export AZURE_SUBSCRIPTION_ID=$AZURE_SUBSCRIPTION_ID + export AZURESQL_SERVER_PASSWORD=$AZURESQL_SERVER_PASSWORD + bash -c "./scripts/deploy_infrastructure.sh" || { + echo "Deployment failed for $env_name" + exit 1 + } + export ENV_DEPLOY=$ENV_DEPLOY + + done + +} \ No newline at end of file diff --git a/e2e_samples/parking_sensors/scripts/deploy_azdo_pipelines.sh b/e2e_samples/parking_sensors/scripts/deploy_azdo_pipelines.sh index a0f49cce3..55be09644 100755 --- a/e2e_samples/parking_sensors/scripts/deploy_azdo_pipelines.sh +++ b/e2e_samples/parking_sensors/scripts/deploy_azdo_pipelines.sh @@ -38,7 +38,7 @@ set -o nounset # AZDO_PIPELINES_BRANCH_NAME # DEV_DATAFACTORY_NAME -source ./scripts/common.sh +. ./scripts/init_environment.sh # Retrieve Github Service Connection Id github_sc_name="${PROJECT}-github" @@ -57,12 +57,16 @@ create_azdo_pipeline "ci-qa-sql" "This pipeline builds the sql dacpac" create_azdo_pipeline "ci-artifacts" "This pipeline publishes build artifacts" +################### # Release Pipelines -cd_release_pipeline_id=$(create_azdo_pipeline "cd-release" "This pipeline releases across environments") - - -az pipelines variable create \ - --name devAdfName \ - --pipeline-id "$cd_release_pipeline_id" \ - --value "$DEV_DATAFACTORY_NAME" \ - -o none +################### +# Release Pipelines - only if it has at least 2 environments +if [ "$ENV_DEPLOY" -eq 2 ] || [ "$ENV_DEPLOY" -eq 3 ]; then + log " Release Pipeline are been created - option selected: $ENV_DEPLOY" + cd_release_pipeline_id=$(create_azdo_pipeline "cd-release" "This pipeline releases across environments") + az pipelines variable create \ + --name devAdfName \ + --pipeline-id "$cd_release_pipeline_id" \ + --value "$DEV_DATAFACTORY_NAME" \ + -o none +fi \ No newline at end of file diff --git a/e2e_samples/parking_sensors/scripts/init_environment.sh b/e2e_samples/parking_sensors/scripts/init_environment.sh index bc6adbe23..4f1a4c383 100755 --- a/e2e_samples/parking_sensors/scripts/init_environment.sh +++ b/e2e_samples/parking_sensors/scripts/init_environment.sh @@ -28,7 +28,6 @@ az config set core.login_experience_v2=on az account set -s $AZURE_SUBSCRIPTION_ID -o none az devops configure --defaults organization=$AZDO_ORGANIZATION_URL project=$AZDO_PROJECT -o none - # check required variables are specified. if [ -z "$GITHUB_REPO" ] @@ -94,4 +93,10 @@ then # set purge protection variable to true if the env variable has not been set export ENABLE_KEYVAULT_PURGE_PROTECTION=${ENABLE_KEYVAULT_PURGE_PROTECTION:-true} log "No ENABLE_KEYVAULT_PURGE specified. Defaulting to $ENABLE_KEYVAULT_PURGE_PROTECTION" "info" +fi + +ENV_DEPLOY=${ENV_DEPLOY:-} +if [ -z "$ENV_DEPLOY" ]; then + export ENV_DEPLOY + log "No Deployment option was specified in the configuration" "info" fi \ No newline at end of file