Skip to content

Commit 1cd3744

Browse files
committed
Improving lab resource naming to allow re-runs and manual runs for dev environments
1 parent a353fec commit 1cd3744

File tree

3 files changed

+145
-80
lines changed

3 files changed

+145
-80
lines changed

.github/workflows/deploy.sh

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ echo
44
echo "Connection info:"
55
az account show | jq '. | {tenantId: .tenantId, subscriptionName: .name, userName: .user.name, userType: .user.type}'
66
echo
7+
RUNTIMESTAMP=$(date +"%Y%m%d%H%M")
78

89
echo "========================================================================================================================================================================================================"
910

@@ -20,32 +21,60 @@ echo "Artifact Source Name: $ARTIFACT_SOURCE_NAME"
2021
BRANCH_NAME=${GITHUB_REF#*refs/heads/}
2122
echo "Branch Name: $BRANCH_NAME"
2223

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+
2348
# 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
2449
echo "Retriving service principal id for the logged in user..."
2550
SERVICEPRINCIPALAPPID=$(az account show | jq --raw-output '.user.name')
26-
echo "Service Principal App/Client Id: $SERVICEPRINCIPALAPPID"
51+
#echo "Service Principal App/Client Id: $SERVICEPRINCIPALAPPID"
2752
SERVICEPRINCIPALID=$( az ad sp list --filter "appId eq '$SERVICEPRINCIPALAPPID' and servicePrincipalType eq 'Application'" --query [0].objectId --output tsv)
28-
echo "Service Principal Object Id: $SERVICEPRINCIPALID"
53+
#echo "Service Principal Object Id: $SERVICEPRINCIPALID"
2954

3055

3156
# 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
3257
# 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.
3358
SERVICE_PRINCIPAL_INFO=$( echo $SERVICE_PRINCIPAL_CREDENTIALS | jq '{ tenantId, clientId, clientSecret, $clientObjectId }' --arg 'clientObjectId' $SERVICEPRINCIPALID -c )
34-
echo "Service Principal Info: $SERVICE_PRINCIPAL_INFO"
59+
#echo "Service Principal Info: $SERVICE_PRINCIPAL_INFO"
3560

3661
echo "Building parameters file for ARM deployment..."
3762
PARAMETERS_FILE="$(pwd)/azuredeploy.parameters.json"
38-
echo $'[ { "name":"branch", "value":"'$BRANCH_NAME'" },' \
39-
' { "name":"commit", "value":"'$GITHUB_SHA'" },' \
63+
echo $'[' \
64+
' { "name":"branch", "value":"'$BRANCH_NAME'" },' \
65+
' { "name":"environment", "value":"'$ENVIRONMENT'" },' \
66+
' { "name":"environmentUser", "value":"'$ACTOR_NAME'" },' \
67+
' { "name":"gitSha", "value":"'$GITHUB_SHA'" },' \
68+
' { "name":"gitShaShort", "value":"'${GITHUB_SHA:0:7}'" },' \
69+
' { "name":"githubPullRequest", "value":"PR: '${GITHUB_PR_NUMBER}': '$GITHUB_PR_TITLE'" },' \
70+
' { "name":"resourceNameSuffix", "value":"'$RESOURCE_NAME_SUFFIX'" },' \
4071
' { "name":"location", "value":"UK South" },' \
4172
' { "name":"devopsServicePrincipalCredentials", "value":' $SERVICE_PRINCIPAL_INFO ' },' \
4273
' { "name":"additionalPrincipals", "value":' "${ADDITIONAL_PRINCIPALS:=[]}" ' }' \
4374
']' \
4475
| jq '.' > "$PARAMETERS_FILE"
4576
#cat $PARAMETERS_FILE
4677

47-
ENVIRONMENT_INSTANCE_NAME='CI_Build___'"${BRANCH_NAME////__}"'___'"${GITHUB_SHA:0:8}"''
48-
echo "Environment Instance Name: $ENVIRONMENT_INSTANCE_NAME"
4978

5079
echo "::set-output name=ENVIRONMENT_INSTANCE_NAME::$ENVIRONMENT_INSTANCE_NAME"
5180

@@ -83,7 +112,6 @@ echo "==========================================================================
83112

84113

85114
# These don't show in the output, but we can view then in a yaml step as below
86-
# echo "ENVIRONMENT_INSTANCE_NAME: ${{ steps.create-devtest-labs-environment.outputs.ENVIRONMENT_INSTANCE_NAME }}"
87115

88116
# 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.
89117
#echo "::set-output name=DEPLOYMENTOUTPUT::$DEPLOYMENTOUTPUT"

.github/workflows/test_azure_devtest_labs_integration.yml

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ name: Build and Run Integration Tests on Azure DevTest Labs
55
# Controls when the action will run.
66
on:
77
# Triggers the workflow on push or pull request events but only for the main branch
8-
push:
8+
pull_request:
99
branches:
1010
- main
11-
- feature/*
12-
pull_request:
11+
push:
1312
branches:
1413
- main
14+
- feature/*
1515
# Allows you to run this workflow manually from the Actions tab
1616
workflow_dispatch:
1717

@@ -64,28 +64,30 @@ jobs:
6464
with:
6565
creds: '${{ secrets.AZURE_DEV_TEST_LABS_CREDENTIALS }}'
6666

67-
67+
# Find the PR details
68+
- uses: jwalton/gh-find-current-pr@v1
69+
id: findPr
6870

6971
# --raw-output is requiored on all jq commands that select strings, otherwise they come out with the quotes, which is interpreted as a command, and it likely won't exist!
7072
- name: Create DevTest Labs Environment
7173
id: create-devtest-labs-environment
7274
uses: azure/CLI@v1
75+
env:
76+
GITHUB_PR_NUMBER: ${{ steps.findPr.outputs.pr }}
77+
GITHUB_PR_TITLE: ${{ steps.findPr.outputs.title }}
7378
with:
7479
# azcliversion: 2.24.0 # Versions prior to this have a bug creating environments
7580
# This runs in a container, so all output is passed back using 'echo "::set-output name=OUTPUT_VAR_NAME::value here"', as files created won't persist.
81+
# Note: Environment variables passed in (as env:... above) break if they contain quotes and newlines, eg multiline JSON. The export commands below work correctly as long as the bash
82+
# quoting rules are followed.
7683
inlineScript: |
77-
# We need the secrets in an environment variable so they get passed to our shell script that does the work
7884
export SERVICE_PRINCIPAL_CREDENTIALS=$'${{ secrets.AZURE_DEV_TEST_LABS_CREDENTIALS }}'
7985
export ADDITIONAL_PRINCIPALS=$'${{ secrets.ADDITIONAL_PRINCIPALS }}'
80-
8186
# Execute the script to create the dev test lab
8287
chmod +x ./.github/workflows/deploy.sh
8388
./.github/workflows/deploy.sh
8489
8590
86-
87-
88-
8991
# Runs a set of commands using the runners shell
9092
- name: Show create environment outputs
9193
run: |
@@ -139,10 +141,35 @@ jobs:
139141
run: dotnet test --no-build --configuration ${{ env.BUILD_CONFIGURATION }} --filter TestCategory=IntegrationTest --output ./output --results-directory ./test-results --verbosity normal --logger "trx;LogFileName=integration-test-results.trx" --settings ${{ env.RUN_SETTINGS_FILENAME }}
140142

141143

144+
# --raw-output is requiored on all jq commands that select strings, otherwise they come out with the quotes, which is interpreted as a command, and it likely won't exist!
145+
- name: Teardown DevTest Labs Environment
146+
id: teardown-devtest-labs-environment
147+
if: ${{ (github.event_name != 'workflow_dispatch') && (success() || failure()) }} # run this step even if previous step failed
148+
uses: azure/CLI@v1
149+
env:
150+
ENVIRONMENT_INSTANCE_NAME: ${{ steps.create-devtest-labs-environment.outputs.ENVIRONMENT_INSTANCE_NAME }}
151+
ENVIRONMENT_INSTANCE_RESOURCE_GROUP_NAME: ${{ steps.create-devtest-labs-environment.outputs.ENVIRONMENT_INSTANCE_RESOURCE_GROUP_NAME }}
152+
with:
153+
# This runs in a container, so all output is passed back using 'echo "::set-output name=OUTPUT_VAR_NAME::value here"', as files created won't persist.
154+
# Note: Environment variables passed in (as env:... above) break if they contain quotes and newlines, eg multiline JSON. The export commands below work correctly as long as the bash
155+
# quoting rules are followed.
156+
inlineScript: |
157+
# Get the prvisioing state for the RG created for the lab
158+
echo "Lab Name: $ENVIRONMENT_INSTANCE_NAME"
159+
echo "Checking provisioning state of lab resource group $ENVIRONMENT_INSTANCE_RESOURCE_GROUP_NAME"
160+
PROVISIONING_STATE=$(az deployment group list --resource-group $ENVIRONMENT_INSTANCE_RESOURCE_GROUP_NAME --query 'sort_by([*], &properties.timestamp)[0].properties.provisioningState' | sed -e 's/^"//' -e 's/"$//')
161+
162+
echo "Povisioning State: $PROVISIONING_STATE"
163+
164+
if [ ! -z "$PROVISIONING_STATE" ]; then
165+
echo "Deleting lab $ENVIRONMENT_INSTANCE_NAME"
166+
az lab environment delete --resource-group "$RESOURCE_GROUP" --lab-name "$LAB_NAME" --name "$ENVIRONMENT_INSTANCE_NAME"
167+
fi
168+
142169
143170
- name: Publish Test Report
144171
uses: dorny/test-reporter@v1
145-
if: success() || failure() # run this step even if previous step failed
172+
if: success() || failure() # run this step even if previous step failed
146173
with:
147174
name: Tests # Name of the check run which will be created
148175
path: ./test-results/*.trx # Path to test results

0 commit comments

Comments
 (0)