Stop environment #3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Stop environment | |
| # Deallocates the VM (stops compute billing) and deletes the public IP, so the | |
| # only ongoing cost while idle is the OS disk. Safe to run anytime; the VM's | |
| # data and containers survive on the disk and return on the next start. | |
| on: | |
| workflow_dispatch: | |
| permissions: | |
| id-token: write | |
| contents: read | |
| env: | |
| RESOURCE_GROUP: my-app-rg | |
| VM_NAME: my-vm | |
| NIC_NAME: my-nic | |
| IP_CONFIG: internal | |
| PIP_NAME: my-vm-pip | |
| jobs: | |
| stop: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Get OIDC token | |
| id: prep | |
| run: | | |
| TOKEN=$(curl -sS \ | |
| -H "Authorization: bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" \ | |
| "$ACTIONS_ID_TOKEN_REQUEST_URL&audience=api://AzureADTokenExchange" \ | |
| | jq -r '.value') | |
| echo "::add-mask::$TOKEN" | |
| echo "token=$TOKEN" >> "$GITHUB_OUTPUT" | |
| - name: Stop the environment | |
| uses: azure/cli@v2 | |
| env: | |
| FED_TOKEN: ${{ steps.prep.outputs.token }} | |
| AZURE_CLIENT_ID: ${{ vars.AZURE_CLIENT_ID }} | |
| AZURE_TENANT_ID: ${{ vars.AZURE_TENANT_ID }} | |
| AZURE_SUBSCRIPTION_ID: ${{ vars.AZURE_SUBSCRIPTION_ID }} | |
| with: | |
| azcliversion: "2.64.0" | |
| inlineScript: | | |
| set -e | |
| az login --service-principal \ | |
| --username "$AZURE_CLIENT_ID" --tenant "$AZURE_TENANT_ID" \ | |
| --federated-token "$FED_TOKEN" --output none | |
| az account set --subscription "$AZURE_SUBSCRIPTION_ID" | |
| az vm deallocate --resource-group "$RESOURCE_GROUP" --name "$VM_NAME" | |
| az network nic ip-config update \ | |
| --resource-group "$RESOURCE_GROUP" --nic-name "$NIC_NAME" \ | |
| --name "$IP_CONFIG" --remove publicIpAddress --only-show-errors || true | |
| az network public-ip delete \ | |
| --resource-group "$RESOURCE_GROUP" --name "$PIP_NAME" --only-show-errors || true | |
| { | |
| echo "### 🛑 Environment stopped" | |
| echo "" | |
| echo "VM deallocated and public IP released." | |
| } >> "$GITHUB_STEP_SUMMARY" |