Skip to content

Commit

Permalink
Move duplicated logic to scripts (#3371)
Browse files Browse the repository at this point in the history
* move construction of tre_url to script and source it

* Use Azure environment in local deployment

* move conversion of azure_env to arm_env to script and source it

* remove bash options

* Update devops/scripts/load_and_validate_env.sh

Co-authored-by: Tamir Kamara <26870601+tamirkamara@users.noreply.github.com>

* Update devops/scripts/load_and_validate_env.sh

Co-authored-by: Tamir Kamara <26870601+tamirkamara@users.noreply.github.com>

---------

Co-authored-by: LizaShak <iliza@outlook.com>
Co-authored-by: Tamir Kamara <26870601+tamirkamara@users.noreply.github.com>
  • Loading branch information
3 people committed Apr 2, 2023
1 parent 3444a10 commit 923272c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 19 deletions.
24 changes: 13 additions & 11 deletions .github/actions/devcontainer_run_command/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,18 @@ inputs:
runs:
using: composite
steps:
- name: Construct AZURE_ENVIRONMENT
shell: bash
run: |
azure_env="${{ (inputs.AZURE_ENVIRONMENT != '' && inputs.AZURE_ENVIRONMENT) || 'AzureCloud' }}"
echo "AZURE_ENVIRONMENT=$azure_env" >> $GITHUB_ENV
- name: Azure Login
uses: azure/login@v1
if: contains(inputs.COMMAND, 'bootstrap') != true
with:
creds: ${{ inputs.AZURE_CREDENTIALS }}
environment: ${{ (inputs.AZURE_ENVIRONMENT != '' && inputs.AZURE_ENVIRONMENT) || 'AzureCloud' }}
environment: "${{ env.AZURE_ENVIRONMENT }}"

- name: ACR Login
shell: bash
Expand All @@ -141,20 +147,16 @@ runs:
- name: Construct TRE_URL
shell: bash
run: |
declare -A cloudapp_endpoint_suffixes=( ["AzureCloud"]="cloudapp.azure.com" ["AzureUSGovernment"]="cloudapp.usgovcloudapi.net")
azure_env="${{ (inputs.AZURE_ENVIRONMENT != '' && inputs.AZURE_ENVIRONMENT) || 'AzureCloud' }}"
domain=${cloudapp_endpoint_suffixes[${azure_env}]}
tre_id="${{ inputs.TRE_ID }}"
location="${{ inputs.LOCATION }}"
tre_url=${TRE_URL:-https://${tre_id}.${location}.${domain}}
source devops/scripts/construct_tre_url.sh
tre_url=$(construct_tre_url "${{ inputs.TRE_ID }}" "${{ inputs.LOCATION }}" "${{ env.AZURE_ENVIRONMENT }}")
echo "TRE_URL=$tre_url" >> $GITHUB_ENV
- name: Construct ARM_ENVIRONMENT
shell: bash
run: |
declare -A arm_envs=( ["AzureCloud"]="public" ["AzureUSGovernment"]="usgovernment")
azure_env="${{ (inputs.AZURE_ENVIRONMENT != '' && inputs.AZURE_ENVIRONMENT) || 'AzureCloud' }}"
echo "ARM_ENVIRONMENT=${arm_envs[${azure_env}]}" >> $GITHUB_ENV
source devops/scripts/convert_azure_env_to_arm_env.sh
arm_environment=$(convert_azure_env_to_arm_env "${{ env.AZURE_ENVIRONMENT }}")
echo "ARM_ENVIRONMENT=$arm_environment" >> $GITHUB_ENV
- name: Construct ACR Domain Suffix
shell: bash
Expand All @@ -176,7 +178,7 @@ runs:
-e WORKSPACE_SERVICE_NAME="${{ inputs.WORKSPACE_SERVICE_NAME }}" \
-e ARM_ENVIRONMENT="${{ env.ARM_ENVIRONMENT }}" \
-e TF_VAR_arm_environment="${{ env.ARM_ENVIRONMENT }}" \
-e AZURE_ENVIRONMENT="${{ (inputs.AZURE_ENVIRONMENT != '' && inputs.AZURE_ENVIRONMENT) || 'AzureCloud' }}" \
-e AZURE_ENVIRONMENT="${{ env.AZURE_ENVIRONMENT }}" \
-e LOCATION="${{ inputs.LOCATION }}" \
-e TF_VAR_location="${{ inputs.LOCATION }}" \
-e RESOURCE_LOCATION="${{ inputs.LOCATION }}" \
Expand Down
15 changes: 15 additions & 0 deletions devops/scripts/construct_tre_url.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

# This script is designed to be `source`d to create reusable helper functions

function construct_tre_url()
{
tre_id=$1
location=$2
azure_environment=$3

declare -A cloudapp_endpoint_suffixes=( ["AzureCloud"]="cloudapp.azure.com" ["AzureUSGovernment"]="cloudapp.usgovcloudapi.net" )
domain=${cloudapp_endpoint_suffixes[${azure_environment}]}

echo https://"${tre_id}"."${location}"."${domain}"
}
10 changes: 10 additions & 0 deletions devops/scripts/convert_azure_env_to_arm_env.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

# This script is designed to be `source`d to create reusable helper functions

function convert_azure_env_to_arm_env()
{
azure_environment=$1
declare -A arm_envs=( ["AzureCloud"]="public" ["AzureUSGovernment"]="usgovernment")
echo "${arm_envs[${azure_environment}]}"
}
20 changes: 12 additions & 8 deletions devops/scripts/load_and_validate_env.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ set -o nounset
# load_and_validate_env.sh
#

# shellcheck disable=SC1091
source "${DIR}"/construct_tre_url.sh
# shellcheck disable=SC1091
source "${DIR}"/convert_azure_env_to_arm_env.sh

if [ ! -f "config.yaml" ]; then
if [ -z "${USE_ENV_VARS_NOT_FILES:-}" ]; then
echo -e "\e[31m»»» 💥 Unable to find config.yaml file, please create file and try again!\e[0m"
Expand Down Expand Up @@ -43,18 +48,17 @@ else
# shellcheck disable=SC2046
export $(yq e "$GET_LEAF_KEYS|$TF_KEYS| $FORMAT_FOR_ENV_EXPORT" config.yaml)

# Source AZURE_ENVIRONMENT and setup the ARM_ENVIRONMENT based on it
# Source AZURE_ENVIRONMENT and setup the ARM_ENVIRONMENT based on it
AZURE_ENVIRONMENT=$(az cloud show --query name --output tsv)
export AZURE_ENVIRONMENT

declare -A arm_environments=( ["AzureCloud"]="public" ["AzureUSGovernment"]="usgovernment")
declare -A cloudapp_endpoint_suffix=( ["AzureCloud"]="cloudapp.azure.com" ["AzureUSGovernment"]="cloudapp.usgovcloudapi.net")

# The ARM Environment is required by terrafform to indicate the destination cloud.
export ARM_ENVIRONMENT="${arm_environments[${AZURE_ENVIRONMENT}]}"
export TF_VAR_arm_environment="${arm_environments[${AZURE_ENVIRONMENT}]}"
# The ARM Environment is required by terraform to indicate the destination cloud.
ARM_ENVIRONMENT=$(convert_azure_env_to_arm_env "${AZURE_ENVIRONMENT}")
export ARM_ENVIRONMENT
export TF_VAR_arm_environment="${ARM_ENVIRONMENT}"

export TRE_URL=${TRE_URL:-https://${TRE_ID}.${LOCATION}.${cloudapp_endpoint_suffix[${AZURE_ENVIRONMENT}]}}
TRE_URL=$(construct_tre_url "${TRE_ID}" "${LOCATION}" "${AZURE_ENVIRONMENT}")
export TRE_URL
fi

set +o nounset

0 comments on commit 923272c

Please sign in to comment.