Create an Azure VM #5
This file contains 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: Create an Azure VM | |
defaults: | |
run: | |
shell: bash | |
on: | |
workflow_dispatch: | |
inputs: | |
action: | |
description: Action | |
required: true | |
type: choice | |
options: | |
- start | |
- terminate all | |
size: | |
description: Instance | |
required: true | |
type: choice | |
# All numbers are for spot instances in East US as of 2023-06-03. | |
# Standard_HB120rs_v3: | |
# - https://learn.microsoft.com/en-us/azure/virtual-machines/hbv3-series | |
# - 120 AMD EPYC™ 7V73X (Milan-X) CPU cores | |
# - 448 GB of RAM | |
# - 2 * 960 GB SSD | |
# - $0.37/hour | |
# Standard_NC8as_T4_v3: | |
# - https://learn.microsoft.com/en-us/azure/virtual-machines/nct4-v3-series | |
# - 1x NVIDIA Tesla T4 GPU with 16 GB of RAM | |
# - 8 AMD EPYC 7V12 (Rome) CPU cores | |
# - 56 GB of RAM | |
# - 1 * 360 GB SSD | |
# - $0.14/hour | |
# Standard_NV36ads_A10_v5: | |
# - https://learn.microsoft.com/en-us/azure/virtual-machines/nva10v5-series | |
# - 1x NVIDIA A10 GPU with 24 GB of RAM | |
# - 36 AMD EPYC 74F3V(Milan) CPU cores | |
# - 440 GB of RAM | |
# - 1 * 1440 GB SSD | |
# - $1.28/hour | |
options: | |
- Standard_HB120rs_v3 | |
# - Standard_NC8as_T4_v3 | |
# - Standard_NV36ads_A10_v5 | |
jobs: | |
check-for-running-instance: | |
name: Check for instance | |
runs-on: ubuntu-latest | |
if: ${{ inputs.action == 'start' }} | |
outputs: | |
# NOTE: Returns the ID of the matching runner, or -1 if no matching runner is found. | |
id: ${{ steps.id.outputs.value }} | |
permissions: | |
actions: read | |
contents: read | |
id-token: write | |
steps: | |
- name: Query GitHub for runners | |
id: runners | |
env: | |
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: | | |
gh api \ | |
-H "Accept: application/vnd.github+json" \ | |
-H "X-GitHub-Api-Version: 2022-11-28" \ | |
/repos/ConnorBaker/nix-cuda-test/actions/runners \ | |
| jq -crS '.runners' \ | |
| xargs --null printf "json=%s" \ | |
| tee -a "$GITHUB_OUTPUT" | |
- name: Filter the runners by size and status | |
id: matching-runners | |
run: | | |
jq -crS \ | |
--arg size "${{ inputs.size }}" \ | |
'map(select(.name == $size and .status == "online"))' \ | |
<<< "${{ steps.runners.outputs.json }}" \ | |
| xargs --null printf "json=%s" \ | |
| tee -a "$GITHUB_OUTPUT" | |
- name: Get the ID of the matching runners | |
id: id | |
run: | | |
declare -i length=$(jq -crS 'length' <<< "${{ steps.matching-runners.outputs.json }}") | |
declare -i id=-1 | |
if (( length == 0 )); then | |
echo "No matching runner found." | |
else if (( length == 1 )); then | |
echo "Matching runner found." | |
id=$(jq -crS '.[0].id' <<< "${{ steps.matching-runners.outputs.json }}") | |
else | |
echo "Multiple matching runners found!" | |
exit 1 | |
fi | |
create-running-instance: | |
name: Create a running instance of the specified size | |
runs-on: ubuntu-latest | |
needs: check-for-running-instance | |
if: ${{ inputs.action == 'start' && needs.check-for-running-instance.outputs.id == -1 }} | |
permissions: | |
contents: read | |
id-token: write | |
steps: | |
- name: Log in to Azure | |
uses: Azure/login@v2 | |
with: | |
client-id: ${{ secrets.AZURE_CLIENT_ID }} | |
subscription-id: ${{ secrets.AZURE_SUBSCRIPTION_ID }} | |
tenant-id: ${{ secrets.AZURE_TENANT_ID }} | |
- name: Azure CLI script | |
uses: azure/CLI@v1 | |
with: | |
azcliversion: latest | |
inlineScript: | | |
az account show |