Skip to content

Commit 9e1347d

Browse files
committed
GitHub action tidy up, and moved az cli inline script out of the GitHub actions yaml file and into a proper shell script for better dev experience
1 parent a432c63 commit 9e1347d

File tree

4 files changed

+215
-163
lines changed

4 files changed

+215
-163
lines changed

.github/workflows/deploy.sh

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
echo "========================================================================================================================================================================================================"
2+
echo "Azure CLI Version: $(az version | jq '."azure-cli"')"
3+
echo
4+
echo "Connection info:"
5+
az account show | jq '. | {tenantId: .tenantId, subscriptionName: .name, userName: .user.name, userType: .user.type}'
6+
echo
7+
8+
echo "Configuring variables for secrets:"
9+
echo "========================================================================================================================================================================================================"
10+
echo
11+
12+
# ls -a
13+
14+
15+
# # Can we run a shell script to tidy this up?
16+
# . ./.github/workflows/deploy_dev_test_lab.sh -?
17+
18+
# echo "========================================================================================================================================================================================================"
19+
20+
21+
ARTIFACT_SOURCE_NAME=$(az lab artifact-source list --resource-group $RESOURCE_GROUP \
22+
--lab-name $LAB_NAME \
23+
| jq --raw-output \
24+
'.[] | select( .uri == "https://github.com/sqlcollaborative/AzureDataPipelineTools.git" ) | .name' \
25+
)
26+
27+
echo "Artifact Source Name: $ARTIFACT_SOURCE_NAME"
28+
29+
BRANCH_NAME=${GITHUB_REF#*refs/heads/}
30+
echo "Branch Name: $BRANCH_NAME"
31+
32+
# We need the object id of the Enterprise Application created from the App Registration in order to set permissions in the ARM template. This is **not** the same as the app/client id
33+
echo "Retriving service principal id for the logged in user..."
34+
SERVICEPRINCIPALAPPID=$(az account show | jq --raw-output '.user.name')
35+
echo "Service Principal App/Client Id: $SERVICEPRINCIPALAPPID"
36+
SERVICEPRINCIPALID=$( az ad sp list --filter "appId eq '$SERVICEPRINCIPALAPPID' and servicePrincipalType eq 'Application'" --query [0].objectId --output tsv)
37+
echo "Service Principal Object Id: $SERVICEPRINCIPALID"
38+
39+
40+
# Build a JSON snippet with the client/app id, object id and client secret for the devops SPN. This is used by the ARM template to grant permissions on resources so that the devops SPN
41+
# can deploy code into them. The ARM template generates the required .runsettings file for the integration tests as an output, which reuses the devops SPN to access resources to test.
42+
SERVICEPRINCIPALINFO=$( echo $SERVICEPRINCIPALCREDENTIALS | jq '{ tenantId, clientId, clientSecret, $clientObjectId }' --arg 'clientObjectId' $SERVICEPRINCIPALID -c )
43+
44+
echo "Service Principal Info: $SERVICEPRINCIPALINFO"
45+
46+
echo "Building parameters file for ARM deployment..."
47+
PARAMETERS_FILE="$(pwd)/azuredeploy.parameters.json"
48+
echo $'[ { "name":"branch", "value":"'$BRANCH_NAME'" },' \
49+
' { "name":"commit", "value":"'$GITHUB_SHA'" },' \
50+
' { "name":"location", "value":"UK South" },' \
51+
' { "name":"devopsServicePrincipalCredentials", "value":' $SERVICEPRINCIPALINFO ' }' \
52+
']' \
53+
| jq '.' > "$PARAMETERS_FILE"
54+
#cat $PARAMETERS_FILE
55+
56+
ENVIRONMENT_INSTANCE_NAME='CI_Build___'"${BRANCH_NAME////__}"'___'"${GITHUB_SHA:0:8}"''
57+
echo "Environment Instance Name: $ENVIRONMENT_INSTANCE_NAME"
58+
59+
echo "::set-output name=ENVIRONMENT_INSTANCE_NAME::$ENVIRONMENT_INSTANCE_NAME"
60+
61+
ENVIRONMENT_CREATE_OUTPUT=$(az lab environment create --resource-group $RESOURCE_GROUP \
62+
--lab-name $LAB_NAME \
63+
--name $ENVIRONMENT_INSTANCE_NAME \
64+
--artifact-source-name $ARTIFACT_SOURCE_NAME \
65+
--arm-template $ARM_TEMPLATE_NAME \
66+
--parameter "@$PARAMETERS_FILE" \
67+
--verbose \
68+
| jq '.'
69+
)
70+
71+
echo "Output from 'az lab environment create'"
72+
echo $ENVIRONMENT_CREATE_OUTPUT
73+
74+
PROVISIONING_STATE=$(echo $ENVIRONMENT_CREATE_OUTPUT | jq --raw-output '.provisioningState')
75+
echo "Provisioning State: $PROVISIONING_STATE"
76+
77+
ENVIRONMENT_INSTANCE_RESOURCE_GROUP_NAME=$(echo $ENVIRONMENT_CREATE_OUTPUT | jq --raw-output '.resourceGroupId' | xargs basename)
78+
echo "Resource Group Id: $ENVIRONMENT_INSTANCE_RESOURCE_GROUP_NAME"
79+
80+
echo "::set-output name=ENVIRONMENT_INSTANCE_RESOURCE_GROUP_NAME::$ENVIRONMENT_INSTANCE_RESOURCE_GROUP_NAME"
81+
82+
if [ $PROVISIONING_STATE != "Succeeded" ]; then
83+
echo "::error Error provisioning lab environment"
84+
exit 1
85+
fi
86+
87+
echo "========================================================================================================================================================================================================"
88+
DEPLOYMENTOUTPUT=$(az deployment group list --resource-group $ENVIRONMENT_INSTANCE_RESOURCE_GROUP_NAME --query '[0].properties.outputs')
89+
90+
# DEBUG: Use this to get the full deployment output JSON. If the ARM template outputs a full reference to a resource, we can find the bits we need easily.
91+
# echo "::set-output name=DEPLOYMENTOUTPUT::$DEPLOYMENTOUTPUT"
92+
93+
echo "Deployment Outputs"
94+
#echo "::set-output name=STORAGE_ACCOUNTCONNECTION_STRING::$(echo $DEPLOYMENTOUTPUT | jq --raw-output '.storageAccountConnectionString.value')"
95+
echo "::set-output name=STORAGE_ACCOUNT_NAME::$(echo $DEPLOYMENTOUTPUT | jq --raw-output '.storageAccountName.value')"
96+
echo "::set-output name=STORAGE_CONTAINER_NAME::$(echo $DEPLOYMENTOUTPUT | jq --raw-output '.storageContainerName.value')"
97+
echo "::set-output name=FUNCTIONS_APP_NAME::$(echo $DEPLOYMENTOUTPUT | jq --raw-output '.functionsAppName.value')"
98+
echo "::set-output name=FUNCTIONS_APP_URI::$(echo $DEPLOYMENTOUTPUT | jq --raw-output '.functionsAppUri.value')"
99+
echo "::set-output name=KEY_VAULT_NAME::$(echo $DEPLOYMENTOUTPUT | jq --raw-output '.keyVaultName.value')"
100+
#echo "::set-output name=FUNCTIONS_APP_KEY::$(echo $DEPLOYMENTOUTPUT | jq --raw-output '.functionsAppKey.value')"
101+
echo "::set-output name=RUN_SETTINGS::$(echo $DEPLOYMENTOUTPUT | jq --raw-output '.runSettings.value')"
102+
103+
echo "========================================================================================================================================================================================================"
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/bin/bash
2+
3+
RED='\033[0;31m'
4+
NC='\033[0m' # No Color
5+
6+
helpFunction()
7+
{
8+
printf "${NC}Description:"
9+
echo " This script uses AZ CLI to connect to a Azure Dev Test Labs instance and deploy a lab using an ARM template"
10+
echo -e ""
11+
echo "Usage:"
12+
echo " . deploy_dev_test_lab.sh --resource_group AzureDataPipelineTools_CI \\"
13+
echo " --lab AzureDataPipelineTools \\"
14+
echo " --arm_template sqlcollaborative_AzureDataPipelineTools \\"
15+
echo " --arm_template_params \$servicePrincipalInfoJson"
16+
echo -e ""
17+
echo "Parameters:"
18+
echo -e " --resource_group"
19+
echo -e " The Resource group name"
20+
echo -e ""
21+
echo -e " --lab"
22+
echo -e " The Azure Dev Test Labs name"
23+
echo -e ""
24+
echo -e " --arm_template"
25+
echo -e " The name of the ARM template. This must be in a git repository already registred with the lab as an artifact source"
26+
echo -e ""
27+
echo -e " --arm_template_params"
28+
echo -e " JSON params to pass to the ARM template"
29+
echo -e " Example;"
30+
echo -e " {"
31+
echo -e " \"clientId\": \"<GUID>\","
32+
echo -e " \"clientSecret\": \"<GUID>\","
33+
echo -e " \"subscriptionId\": \"<GUID>\","
34+
echo -e " \"tenantId\": \"<GUID>\","
35+
echo -e " }"
36+
}
37+
38+
39+
#================================================================================================================================================================
40+
# Parse input
41+
#================================================================================================================================================================
42+
while [ $# -gt 0 ]; do
43+
44+
if [[ $1 == "--help" ]] || [[ $1 == "-?" ]] || [[ $1 == "--?" ]]; then
45+
helpFunction
46+
return
47+
elif [[ $1 == *"--"* ]]; then
48+
param="${1/--/}"
49+
declare $param="$2"
50+
# echo $1 $2 // Optional to see the parameter:value result
51+
fi
52+
53+
shift
54+
done
55+
56+
if [ -z "$resource_group" ]; then
57+
printf "${RED}Parameter --resource_group is required.\n"
58+
fi
59+
60+
if [ -z "$lab" ]; then
61+
printf "${RED}Parameter --lab is required.\n"
62+
fi
63+
64+
if [ -z "$arm_template" ]; then
65+
printf "${RED}Parameter --arm_template is required.\n"
66+
fi
67+
68+
if [ -z "$arm_template_params" ]; then
69+
printf "${RED}Parameter --arm_template_params is required.\n"
70+
fi
71+
72+
echo ""
73+
74+
if [ -z "$resource_group" ] || [ -z "$lab" ] || [ -z "$arm_template" ] || [ -z "$arm_template_params" ]; then
75+
helpFunction
76+
return
77+
fi
78+
79+
80+
#================================================================================================================================================================
81+
# Do some stuff
82+
#================================================================================================================================================================
83+
84+
85+
# If all is good, do the work
86+
echo "Helo world from deploy_dev_test_lab.sh"
87+

0 commit comments

Comments
 (0)