Skip to content

Commit c4b2e6c

Browse files
authored
Merge pull request #4 from sqlcollaborative/feature/azure-devtest-labs-arm-CICD
Feature/azure devtest labs arm cicd
2 parents e25100d + 96c04a6 commit c4b2e6c

File tree

62 files changed

+4755
-287
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+4755
-287
lines changed

.github/workflows/build_and_run_unit_tests.yaml

-28
This file was deleted.

.github/workflows/deploy.sh

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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+
RUNTIMESTAMP=$(date +"%Y%m%d%H%M")
8+
9+
echo "========================================================================================================================================================================================================"
10+
11+
echo "Configuring variables for secrets:"
12+
13+
ARTIFACT_SOURCE_NAME=$(az lab artifact-source list --resource-group $RESOURCE_GROUP \
14+
--lab-name $LAB_NAME \
15+
| jq --raw-output \
16+
'.[] | select( .uri == "https://github.com/sqlcollaborative/AzureDataPipelineTools.git" ) | .name' \
17+
)
18+
19+
echo "Artifact Source Name: $ARTIFACT_SOURCE_NAME"
20+
21+
BRANCH_NAME=${GITHUB_REF#*refs/heads/}
22+
echo "Branch Name: $BRANCH_NAME"
23+
24+
# Build some id's for the names of resources
25+
if [ "$GITHUB_EVENT_NAME" == "workflow_dispatch" ]; then
26+
ACTOR_NAME='DEV_'"$(echo $GITHUB_ACTOR | sed "s/[^[:alpha:][:digit:]]//g")"
27+
ACTOR_SHORT=$(echo ${GITHUB_ACTOR} | sed "s/[^[:alpha:][:digit:]]//g" | cut -c -9)
28+
ENVIRONMENT=DEV
29+
else
30+
ACTOR_NAME=GITHUB_CI_BUILD
31+
ACTOR_SHORT=CI
32+
ENVIRONMENT=CICD
33+
fi
34+
35+
36+
echo "GitHub Workflow URL: ${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
37+
38+
39+
# The name of the lab. Allows sorting by owner, timestamp, while showing the branch and commit it was built from
40+
#ENVIRONMENT_INSTANCE_NAME='CI_Build___'"${BRANCH_NAME////__}"'___'"${GITHUB_SHA:0:8}"''
41+
ENVIRONMENT_INSTANCE_NAME="${ACTOR_NAME}"'__'"${RUNTIMESTAMP}"'__'"${BRANCH_NAME////__}"'___'"${GITHUB_SHA:0:7}"''
42+
echo "Environment Instance Name: $ENVIRONMENT_INSTANCE_NAME"
43+
44+
# Used for resource names, eg adlsnlangley1602c96a22d or adlsci09498d47f45. This should give enough uiniqueness to allow parallel environments, while showing the build reason and owner
45+
RESOURCE_NAME_SUFFIX="_${ACTOR_SHORT}_${RUNTIMESTAMP:8:12}_${GITHUB_SHA:0:7}"
46+
echo "Resource Name Suffix: $RESOURCE_NAME_SUFFIX"
47+
48+
# 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
49+
echo "Retriving service principal info for the logged in user..."
50+
TENANTID=$(az account show | jq --raw-output '.homeTenantId')
51+
SERVICEPRINCIPALAPPID=$(az account show | jq --raw-output '.user.name')
52+
#echo "Service Principal App/Client Id: $SERVICEPRINCIPALAPPID"
53+
SERVICEPRINCIPALID=$( az ad sp list --filter "appId eq '$SERVICEPRINCIPALAPPID' and servicePrincipalType eq 'Application'" --query [0].objectId --output tsv)
54+
#echo "Service Principal Object Id: $SERVICEPRINCIPALID"
55+
56+
57+
# 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
58+
# 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.
59+
SERVICE_PRINCIPAL_INFO=$( echo $SERVICE_PRINCIPAL_CREDENTIALS | jq '{ tenantId, clientId, clientSecret, $clientObjectId }' --arg 'clientObjectId' $SERVICEPRINCIPALID -c )
60+
#echo "Service Principal Info: $SERVICE_PRINCIPAL_INFO"
61+
62+
echo "Building parameters file for ARM deployment..."
63+
PARAMETERS_FILE="$(pwd)/azuredeploy.parameters.json"
64+
echo $'[' \
65+
' { "name":"branch", "value":"'$BRANCH_NAME'" },' \
66+
' { "name":"environment", "value":"'$ENVIRONMENT'" },' \
67+
' { "name":"environmentUser", "value":"'$ACTOR_NAME'" },' \
68+
' { "name":"gitSha", "value":"'$GITHUB_SHA'" },' \
69+
' { "name":"gitShaShort", "value":"'${GITHUB_SHA:0:7}'" },' \
70+
' { "name":"githubPullRequest", "value":"PR: '${GITHUB_PR_NUMBER}': '$GITHUB_PR_TITLE'" },' \
71+
' { "name":"resourceNameSuffix", "value":"'$RESOURCE_NAME_SUFFIX'" },' \
72+
' { "name":"location", "value":"UK South" },' \
73+
' { "name":"devopsServicePrincipalCredentials", "value":' $SERVICE_PRINCIPAL_INFO ' },' \
74+
' { "name":"additionalPrincipals", "value":' "${ADDITIONAL_PRINCIPALS:=[]}" ' }' \
75+
']' \
76+
| jq '.' > "$PARAMETERS_FILE"
77+
#cat $PARAMETERS_FILE
78+
79+
80+
echo "::set-output name=ENVIRONMENT_INSTANCE_NAME::$ENVIRONMENT_INSTANCE_NAME"
81+
82+
ENVIRONMENT_CREATE_OUTPUT=$(az lab environment create --resource-group $RESOURCE_GROUP \
83+
--lab-name $LAB_NAME \
84+
--name $ENVIRONMENT_INSTANCE_NAME \
85+
--artifact-source-name $ARTIFACT_SOURCE_NAME \
86+
--arm-template $ARM_TEMPLATE_NAME \
87+
--parameter "@$PARAMETERS_FILE" \
88+
--verbose \
89+
| jq '.'
90+
)
91+
92+
echo "Output from 'az lab environment create'"
93+
echo $ENVIRONMENT_CREATE_OUTPUT
94+
95+
PROVISIONING_STATE=$(echo $ENVIRONMENT_CREATE_OUTPUT | jq --raw-output '.provisioningState')
96+
echo "Provisioning State: $PROVISIONING_STATE"
97+
98+
if [ "$PROVISIONING_STATE" != "Succeeded" ]; then
99+
echo "::error Error provisioning lab environment"
100+
exit 1
101+
fi
102+
103+
ENVIRONMENT_INSTANCE_RESOURCE_GROUP_NAME=$(echo $ENVIRONMENT_CREATE_OUTPUT | jq --raw-output '.resourceGroupId' | xargs basename)
104+
echo "Resource Group Id: $ENVIRONMENT_INSTANCE_RESOURCE_GROUP_NAME"
105+
106+
echo "::set-output name=ENVIRONMENT_INSTANCE_RESOURCE_GROUP_NAME::$ENVIRONMENT_INSTANCE_RESOURCE_GROUP_NAME"
107+
108+
109+
DEPLOYMENTOUTPUT=$(az deployment group list --resource-group $ENVIRONMENT_INSTANCE_RESOURCE_GROUP_NAME --query '[0].properties.outputs')
110+
111+
echo "Setting Job Outputs"
112+
echo "========================================================================================================================================================================================================"
113+
114+
115+
# These don't show in the output, but we can view then in a yaml step as below
116+
117+
# 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.
118+
#echo "::set-output name=DEPLOYMENTOUTPUT::$DEPLOYMENTOUTPUT"
119+
120+
echo "::set-output name=TENANTID::$TENANTID"
121+
echo "::set-output name=STORAGE_ACCOUNT_NAME::$(echo $DEPLOYMENTOUTPUT | jq --raw-output '.storageAccountName.value')"
122+
echo "::set-output name=STORAGE_CONTAINER_NAME::$(echo $DEPLOYMENTOUTPUT | jq --raw-output '.storageContainerName.value')"
123+
echo "::set-output name=FUNCTIONS_APP_NAME::$(echo $DEPLOYMENTOUTPUT | jq --raw-output '.functionsAppName.value')"
124+
echo "::set-output name=FUNCTIONS_APP_URI::$(echo $DEPLOYMENTOUTPUT | jq --raw-output '.functionsAppUri.value')"
125+
echo "::set-output name=KEY_VAULT_NAME::$(echo $DEPLOYMENTOUTPUT | jq --raw-output '.keyVaultName.value')"
126+
echo "::set-output name=RUN_SETTINGS::$(echo $DEPLOYMENTOUTPUT | jq --raw-output '.runSettings.value')"
127+
+87
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)