2020 # Set this to the mainline branch you are using
2121 branches :
2222 - main
23+ pull_request :
24+ # Run when pull requests are opened or updated
25+ branches :
26+ - main
27+
2328# GitHub Actions workflow to deploy to Azure using azd
29+ # Ensure only one deployment runs at a time to prevent conflicts
30+ concurrency :
31+ group : ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
32+ cancel-in-progress : false
2433
2534permissions :
2635 actions : read # Needed for uploading SARIF reports
2736 security-events : write # Needed for uploading SARIF reports
28- id-token : write
37+ id-token : write # Needed for OIDC Authentication
2938 contents : read
3039
3140
3241jobs :
3342 build :
3443 runs-on : ${{ fromJson(vars.ACTIONS_RUNNER_NAME || '["ubuntu-latest"]') }}
3544 env :
36- AZURE_ENV_NAME : ${{ github.event.inputs.azd_environment_name || 'CICD' }}
45+ AZURE_ENV_NAME : ${{ github.event.inputs.azd_environment_name || (github.event_name == 'pull_request' && format('pr-{0}', github.event.pull_request.number)) || 'CICD' }}
3746 AZURE_LOCATION : ${{ github.event.inputs.azure_location || 'eastus' }}
3847
3948 steps :
40- - name : Checkout the branch ${{ github.ref_name }}
41- uses : actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
49+ - name : Checkout code
50+ uses : actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
4251 with :
43- ref : ${{ github.ref_name }}
52+ persist-credentials : false
4453
4554 - name : Install azd
46- uses : Azure/setup-azd@ae0f8b5482eeac61e940f447327d84c73beb8b1e # v2.1.0
55+ uses : Azure/setup-azd@cf638ffd167fc81e1851241a478a723c05fa9cb3 # v2.2.0
56+ with :
57+ version : ' 1.20.0' # Specify your desired azd version here
58+
59+ - name : Setup Node.js
60+ uses : actions/setup-node@2028fbc5c25fe9cf00d9f06a71cc4710d4507903 # v6.0.0
61+ with :
62+ node-version : ' 18.x'
63+
64+ - name : Install Terraform
65+ uses : hashicorp/setup-terraform@b9cd54a3c349d3f38e8881555d616ced269862dd # v3.1.2
4766 with :
48- version : ' 1.18.1 ' # Specify your desired azd version here
67+ terraform_version : 1.13.3
4968
5069 - name : Install TFLint
51- uses : terraform-linters/setup-tflint@90f302c255ef959cbfb4bd10581afecdb7ece3e6 # v4.1.1
70+ uses : terraform-linters/setup-tflint@acd1575d3c037258ce5b2dd01379dc49ce24c6b7 # v6.2.0
5271 with :
5372 tflint_version : v0.58.1
5473 github_token : ${{ secrets.GITHUB_TOKEN }} # Used to avoid rate
7695 echo "GitLeaks scan completed"
7796
7897 - name : Setup .NET SDK
79- uses : actions/setup-dotnet@67a3573c9a986a3f9c594539f4ab511d57bb3ce9 # v4.3.1
80- with :
81- dotnet-version : ' 8.0.x'
98+ shell : bash
99+ run : |
100+ # Install .NET SDK to temp directory for self-hosted runners to avoid permission issues
101+ DOTNET_INSTALL_DIR="${{ runner.temp }}/dotnet"
102+ mkdir -p "$DOTNET_INSTALL_DIR"
103+
104+ # Download and run the dotnet-install script
105+ curl -sSL https://dot.net/v1/dotnet-install.sh -o dotnet-install.sh
106+ chmod +x dotnet-install.sh
107+ ./dotnet-install.sh --channel 9.0 --install-dir "$DOTNET_INSTALL_DIR"
108+ rm dotnet-install.sh
109+
110+ # Add to PATH for subsequent steps
111+ echo "$DOTNET_INSTALL_DIR" >> $GITHUB_PATH
112+ echo "DOTNET_ROOT=$DOTNET_INSTALL_DIR" >> $GITHUB_ENV
82113
83114 - name : Install Power Platform Tools
84115 uses : microsoft/powerplatform-actions/actions-install@6c7b538671a040d11afd8ab94d77bfe3b3ed87e6 # v1.9.1
90121 pac help
91122
92123 - name : Set Up Python
93- uses : actions/setup-python@a26af69be951a213d495a4c3e4e4022e16d87065 # 5.6 .0
124+ uses : actions/setup-python@e797f83bcb11b83ae66e0230d6156d7c80228e7c # 6.0 .0
94125 with :
95126 python-version : " 3.x"
96127
@@ -123,6 +154,7 @@ jobs:
123154 RS_RESOURCE_GROUP : ${{ vars.RS_RESOURCE_GROUP }}
124155
125156 RESOURCE_SHARE_USER : ${{ vars.RESOURCE_SHARE_USER }}
157+ RESOURCE_TAGS : ${{ vars.RESOURCE_TAGS }}
126158
127159 GITHUB_PAT : ${{ secrets.MCS_RUNNER }}
128160 GITHUB_REPO_OWNER : ${{ github.repository_owner }}
@@ -135,6 +167,7 @@ jobs:
135167 azd config set auth.useAzCliAuth "true"
136168 azd env new "$AZURE_ENV_NAME" --location "$AZURE_LOCATION" --no-prompt
137169 azd env set RESOURCE_SHARE_USER "$RESOURCE_SHARE_USER"
170+ azd env set RESOURCE_TAGS "$RESOURCE_TAGS"
138171
139172 azd env set RS_STORAGE_ACCOUNT "$RS_STORAGE_ACCOUNT"
140173 azd env set RS_CONTAINER_NAME "$RS_CONTAINER_NAME"
@@ -168,8 +201,8 @@ jobs:
168201 with :
169202 sarif_file : ./checkov-results.sarif/results_sarif.sarif
170203
171- - name : Azd down
172- if : ${{ github.event.inputs.run_azd_down == 'true' }}
204+ - name : Destroy Infrastructure
205+ if : ${{ github.event.inputs.run_azd_down == 'true' || github.event_name == 'pull_request' }}
173206 env :
174207 POWER_PLATFORM_CLIENT_ID : ${{ vars.AZURE_CLIENT_ID }}
175208 POWER_PLATFORM_TENANT_ID : ${{ vars.AZURE_TENANT_ID }}
@@ -187,13 +220,37 @@ jobs:
187220 RS_CONTAINER_NAME : ${{ vars.RS_CONTAINER_NAME }}
188221 RS_RESOURCE_GROUP : ${{ vars.RS_RESOURCE_GROUP }}
189222 RESOURCE_SHARE_USER : ${{ vars.RESOURCE_SHARE_USER }}
223+ RESOURCE_TAGS : ${{ vars.RESOURCE_TAGS }}
190224
191225 shell : bash
192226 run : |
193227 azd env set RS_STORAGE_ACCOUNT "$RS_STORAGE_ACCOUNT"
194228 azd env set RS_CONTAINER_NAME "$RS_CONTAINER_NAME"
195229 azd env set RS_RESOURCE_GROUP "$RS_RESOURCE_GROUP"
196230 azd env set RESOURCE_SHARE_USER "$RESOURCE_SHARE_USER"
231+ azd env set RESOURCE_TAGS "$RESOURCE_TAGS"
197232
198233 azd env select "$AZURE_ENV_NAME"
199- azd down --no-prompt --force --purge
234+ azd down --no-prompt --force --purge
235+
236+ - name : Purge Soft-Deleted Azure OpenAI Resources
237+ if : ${{ github.event.inputs.run_azd_down == 'true' || github.event_name == 'pull_request' }}
238+ shell : bash
239+ run : |
240+ # Get the OpenAI resource name and location from environment outputs
241+ OPENAI_RESOURCE_NAME=$(azd env get-values --output json | jq -r '.openai_resource_name // empty')
242+ AZURE_REGION=$(azd env get-values --output json | jq -r '.primary_azure_region // empty')
243+ RESOURCE_GROUP=$(azd env get-values --output json | jq -r '.resource_group_name // empty')
244+
245+ # Only attempt to purge if we have the required information
246+ if [[ -n "$OPENAI_RESOURCE_NAME" && -n "$AZURE_REGION" ]]; then
247+ echo "Attempting to purge soft-deleted Azure OpenAI resource: $OPENAI_RESOURCE_NAME in $AZURE_REGION"
248+
249+ # Purge the soft-deleted Cognitive Services account (continue on error if resource not found)
250+ az cognitiveservices account purge \
251+ --location "$AZURE_REGION" \
252+ --resource-group "$RESOURCE_GROUP" \
253+ --name "$OPENAI_RESOURCE_NAME" || echo "Resource may not be in soft-delete state or already purged"
254+ else
255+ echo "OpenAI resource information not found in environment outputs. Skipping purge."
256+ fi
0 commit comments