mirror of
https://github.com/actions/runner-images.git
synced 2025-12-12 20:26:49 +00:00
Remove Storage Account and VHD refs (#8581)
This commit is contained in:
committed by
GitHub
parent
e78225c349
commit
e9fc71683f
@@ -74,9 +74,6 @@ jobs:
|
||||
$ManagedImageName = "${{ parameters.image_type }}-$(Build.BuildId)"
|
||||
Write-Host "##vso[task.setvariable variable=ManagedImageName;]$ManagedImageName"
|
||||
|
||||
$VhdName = "$ManagedImageName.vhd"
|
||||
Write-Host "##vso[task.setvariable variable=VhdName;]$VhdName"
|
||||
|
||||
$TempResourceGroupName = "packer-temp-$ManagedImageName"
|
||||
Write-Host "##vso[task.setvariable variable=TempResourceGroupName;]$TempResourceGroupName"
|
||||
|
||||
@@ -146,26 +143,6 @@ jobs:
|
||||
-PrefixToPathTrim "$(TemplateDirectoryPath)" `
|
||||
-PrintTopNLongest 25
|
||||
|
||||
- task: PowerShell@2
|
||||
displayName: 'Convert managed image to VHD'
|
||||
inputs:
|
||||
targetType: filePath
|
||||
filePath: ./images.CI/linux-and-win/convert-to-vhd.ps1
|
||||
arguments: -SubscriptionId $(AZURE_SUBSCRIPTION) `
|
||||
-Location $(AZURE_LOCATION) `
|
||||
-ResourceGroupName $(AZURE_RESOURCE_GROUP) `
|
||||
-ManagedImageName "$(ManagedImageName)" `
|
||||
-GalleryName "github_imagegeneration_convert_to_vhd" `
|
||||
-GalleryImageSku "${{ parameters.image_type }}" `
|
||||
-GalleryImageVersion "0.0.$(Build.BuildId)" `
|
||||
-StorageAccountName $(AZURE_STORAGE_ACCOUNT) `
|
||||
-StorageAccountContainerName "images" `
|
||||
-VhdName "$(VhdName)" `
|
||||
-ClientId $(CLIENT_ID) `
|
||||
-ClientSecret $(CLIENT_SECRET) `
|
||||
-TenantId $(AZURE_TENANT) `
|
||||
-RemoveManagedImage
|
||||
|
||||
- ${{ if eq(parameters.create_release, true) }}:
|
||||
- task: PowerShell@2
|
||||
displayName: 'Create release for VM deployment'
|
||||
@@ -176,9 +153,8 @@ jobs:
|
||||
-Organization $(RELEASE_TARGET_ORGANIZATION) `
|
||||
-DefinitionId $(RELEASE_TARGET_DEFINITION_ID) `
|
||||
-Project $(RELEASE_TARGET_PROJECT) `
|
||||
-ImageName "${{ parameters.image_type }}" `
|
||||
-StorageAccountContainerName "images" `
|
||||
-VhdName "$(VhdName)" `
|
||||
-ImageType "${{ parameters.image_type }}" `
|
||||
-ManagedImageName "$(ManagedImageName)" `
|
||||
-AccessToken $(RELEASE_TARGET_TOKEN)
|
||||
|
||||
- task: PowerShell@2
|
||||
@@ -188,7 +164,6 @@ jobs:
|
||||
targetType: filePath
|
||||
filePath: ./images.CI/linux-and-win/cleanup.ps1
|
||||
arguments: -TempResourceGroupName "$(TempResourceGroupName)" `
|
||||
-StorageAccount $(AZURE_STORAGE_ACCOUNT) `
|
||||
-SubscriptionId $(AZURE_SUBSCRIPTION) `
|
||||
-ClientId $(CLIENT_ID) `
|
||||
-ClientSecret $(CLIENT_SECRET) `
|
||||
|
||||
@@ -1,23 +1,19 @@
|
||||
param(
|
||||
[String] [Parameter (Mandatory=$true)] $TempResourceGroupName,
|
||||
[String] [Parameter (Mandatory=$true)] $StorageAccount,
|
||||
[String] [Parameter (Mandatory=$true)] $SubscriptionId,
|
||||
[String] [Parameter (Mandatory=$true)] $ClientId,
|
||||
[String] [Parameter (Mandatory=$true)] $ClientSecret,
|
||||
[String] [Parameter (Mandatory=$true)] $TenantId
|
||||
[Parameter (Mandatory=$true)] [string] $TempResourceGroupName,
|
||||
[Parameter (Mandatory=$true)] [string] $SubscriptionId,
|
||||
[Parameter (Mandatory=$true)] [string] $ClientId,
|
||||
[Parameter (Mandatory=$true)] [string] $ClientSecret,
|
||||
[Parameter (Mandatory=$true)] [string] $TenantId
|
||||
)
|
||||
|
||||
az login --service-principal --username $ClientId --password $ClientSecret --tenant $TenantId | Out-Null
|
||||
az account set --subscription $SubscriptionId | Out-Null
|
||||
|
||||
$groupExist = az group exists --name $TempResourceGroupName --subscription $SubscriptionId
|
||||
$groupExist = az group exists --name $TempResourceGroupName
|
||||
if ($groupExist -eq "true") {
|
||||
$osDiskName = az deployment group list --resource-group $TempResourceGroupName --query "[].properties.parameters.osDiskName.value" -o tsv
|
||||
Write-Host "Found a match, deleting temporary files"
|
||||
az group delete --name $TempResourceGroupName --subscription $SubscriptionId --yes | Out-Null
|
||||
az group delete --name $TempResourceGroupName --yes | Out-Null
|
||||
Write-Host "Temporary group was deleted successfully"
|
||||
Write-Host "Deleting OS disk"
|
||||
az storage remove --account-name $StorageAccount -c "images" -n "$osDiskName.vhd" --only-show-errors | Out-Null
|
||||
Write-Host "OS disk deleted"
|
||||
} else {
|
||||
Write-Host "No temporary groups found"
|
||||
}
|
||||
@@ -1,185 +0,0 @@
|
||||
param (
|
||||
[String] [Parameter (Mandatory = $true)] $SubscriptionId,
|
||||
[String] [Parameter (Mandatory = $true)] $Location,
|
||||
[String] [Parameter (Mandatory = $true)] $ResourceGroupName,
|
||||
|
||||
[String] [Parameter (Mandatory = $true)] $ManagedImageName,
|
||||
|
||||
[String] [Parameter (Mandatory = $true)] $GalleryName,
|
||||
[String] [Parameter (Mandatory = $true)] $GalleryImageSku,
|
||||
[String] [Parameter (Mandatory = $true)] $GalleryImageVersion,
|
||||
|
||||
[String] [Parameter (Mandatory = $true)] $StorageAccountName,
|
||||
[String] [Parameter (Mandatory = $true)] $StorageAccountContainerName,
|
||||
[String] [Parameter (Mandatory = $true)] $VhdName,
|
||||
|
||||
[Switch] [Parameter (Mandatory = $false)] $RemoveManagedImage,
|
||||
|
||||
[String] [Parameter (Mandatory = $true)] $ClientId,
|
||||
[String] [Parameter (Mandatory = $true)] $ClientSecret,
|
||||
[String] [Parameter (Mandatory = $true)] $TenantId
|
||||
)
|
||||
|
||||
$ErrorActionPreference = "Stop"
|
||||
|
||||
# Login to Azure
|
||||
az login --service-principal --username $ClientId --password $ClientSecret --tenant $TenantId | Out-Null
|
||||
az account set --subscription $SubscriptionId | Out-Null
|
||||
|
||||
# Create Compute Image Gallery if it doesn't exist
|
||||
Write-Host "Creating Compute Image Gallery '$GalleryName'..."
|
||||
$galleryExists = az sig list --resource-group $ResourceGroupName --query "[?name=='$GalleryName']" -o tsv
|
||||
if ($null -eq $galleryExists) {
|
||||
az sig create --resource-group $ResourceGroupName --gallery-name $GalleryName --location $Location
|
||||
}
|
||||
|
||||
# Create Image Definition if it doesn't exist
|
||||
Write-Host "Creating Image Definition '$ManagedImageName'..."
|
||||
$imageDefinitionName = "RunnerImage-$GalleryImageSku"
|
||||
if ($ManagedImageName -like "*windows*") {
|
||||
$imageOsType = "Windows"
|
||||
} elseif ($ManagedImageName -like "*ubuntu*") {
|
||||
$imageOsType = "Linux"
|
||||
} else {
|
||||
throw "Unknown OS type for image '$ManagedImageName'"
|
||||
}
|
||||
|
||||
$galleryImageExists = az sig image-definition list --resource-group $ResourceGroupName --gallery-name $GalleryName --query "[?name=='$imageDefinitionName']" -o tsv
|
||||
if ($null -eq $galleryImageExists) {
|
||||
az sig image-definition create `
|
||||
--resource-group $ResourceGroupName `
|
||||
--gallery-name $GalleryName `
|
||||
--gallery-image-definition $imageDefinitionName `
|
||||
--publisher "GuthubImagesGeneration" `
|
||||
--offer "RunnerImage" `
|
||||
--sku $GalleryImageSku `
|
||||
--os-type $imageOsType `
|
||||
--location $Location
|
||||
}
|
||||
|
||||
# Create Image Version from existing Managed Image
|
||||
Write-Host "Creating Image Version '$GalleryImageVersion' from Managed Image '$ManagedImageName'..."
|
||||
az sig image-version create `
|
||||
--resource-group $ResourceGroupName `
|
||||
--gallery-name $GalleryName `
|
||||
--gallery-image-definition $imageDefinitionName `
|
||||
--gallery-image-version $GalleryImageVersion `
|
||||
--managed-image "/subscriptions/$SubscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.Compute/images/$ManagedImageName" `
|
||||
--target-regions $Location `
|
||||
--replica-count 1 `
|
||||
--location $Location
|
||||
|
||||
# Create Azure Managed Disk from Shared Image Gallery
|
||||
Write-Host "Creating Azure Managed Disk '$ManagedImageName' from Shared Image Gallery..."
|
||||
az disk create `
|
||||
--resource-group $ResourceGroupName `
|
||||
--name $ManagedImageName `
|
||||
--location $Location `
|
||||
--gallery-image-reference "/subscriptions/$SubscriptionId/resourceGroups/$ResourceGroupName/providers/Microsoft.Compute/galleries/$GalleryName/images/$imageDefinitionName/versions/$GalleryImageVersion"
|
||||
|
||||
# Generate SAS URL for the Managed Disk
|
||||
Write-Host "Generating SAS URL for the Managed Disk '$ManagedImageName'..."
|
||||
$sourceDiskUri = az disk grant-access `
|
||||
--resource-group $ResourceGroupName `
|
||||
--name $ManagedImageName `
|
||||
--duration-in-seconds 86400 `
|
||||
--access-level Read `
|
||||
--query [accessSas] -o tsv
|
||||
|
||||
Write-Host "Querying key for the storage account '$StorageAccountName'..."
|
||||
$targetKey = az storage account keys list `
|
||||
--resource-group $ResourceGroupName `
|
||||
--account-name $StorageAccountName `
|
||||
--query "[0].value" -o tsv
|
||||
|
||||
Write-Host ("Copying VHD blob from '{0}' to 'https://{1}.blob.core.windows.net/{2}/{3}'..." `
|
||||
-f $sourceDiskUri.Split('?')[0], $StorageAccountName, $StorageAccountContainerName, $VhdName)
|
||||
|
||||
az storage blob copy start `
|
||||
--source-uri $sourceDiskUri `
|
||||
--destination-blob $VhdName `
|
||||
--destination-container $StorageAccountContainerName `
|
||||
--account-name $StorageAccountName `
|
||||
--account-key $targetKey `
|
||||
--only-show-errors
|
||||
|
||||
Write-Host "Waiting for the copy to complete..."
|
||||
while ($true) {
|
||||
$status = az storage blob show `
|
||||
--container-name $StorageAccountContainerName `
|
||||
--name $VhdName `
|
||||
--account-name $StorageAccountName `
|
||||
--account-key $targetKey `
|
||||
--query "properties.copy.status" -o tsv
|
||||
|
||||
if ($status -eq "success") {
|
||||
Write-Host "Copy completed successfully."
|
||||
break
|
||||
} elseif ($status -ne "pending") {
|
||||
Write-Host "Copy failed with status '$status', see blob information below:"
|
||||
az storage blob show `
|
||||
--container-name $StorageAccountContainerName `
|
||||
--name $VhdName `
|
||||
--account-name $StorageAccountName `
|
||||
--account-key $targetKey
|
||||
throw "Copy failed with status '$status'"
|
||||
}
|
||||
|
||||
$progress = az storage blob show `
|
||||
--container-name $StorageAccountContainerName `
|
||||
--name $VhdName `
|
||||
--account-name $StorageAccountName `
|
||||
--account-key $targetKey `
|
||||
--query "properties.copy.progress" -o tsv
|
||||
|
||||
Write-Host "Progress: $(($progress.Split("/")[0] / $progress.Split("/")[1]).ToString("P"))"
|
||||
Start-Sleep -Seconds 15
|
||||
}
|
||||
|
||||
Write-Host "Successfully converted '$ManagedImageName' to '$VhdName' in '$StorageAccountName' storage account."
|
||||
|
||||
# Remove Managed Image if requested
|
||||
if ($RemoveManagedImage) {
|
||||
Write-Host "Removing Managed Image '$ManagedImageName'..."
|
||||
az image delete `
|
||||
--resource-group $ResourceGroupName `
|
||||
--name $ManagedImageName `
|
||||
--only-show-errors
|
||||
if ($LastExitCode) {
|
||||
Write-Host "Warning: Failed to delete the Managed Image '$ManagedImageName'."
|
||||
}
|
||||
}
|
||||
|
||||
Write-Host "Cleaning up..."
|
||||
|
||||
# Revoke SAS URL for the Managed Disk
|
||||
az disk revoke-access `
|
||||
--resource-group $ResourceGroupName `
|
||||
--name $ManagedImageName `
|
||||
--only-show-errors
|
||||
if ($LastExitCode) {
|
||||
Write-Host "Warning: Failed to revoke access to the Managed Disk '$ManagedImageName'."
|
||||
}
|
||||
|
||||
# Delete Azure Managed Disk from Shared Image Gallery
|
||||
az disk delete `
|
||||
--resource-group $ResourceGroupName `
|
||||
--name $ManagedImageName `
|
||||
--only-show-errors `
|
||||
--yes
|
||||
if ($LastExitCode) {
|
||||
Write-Host "Warning: Failed to delete the Managed Disk '$ManagedImageName'."
|
||||
}
|
||||
|
||||
# Delete Image Version from Shared Image Gallery
|
||||
az sig image-version delete `
|
||||
--resource-group $ResourceGroupName `
|
||||
--gallery-name $GalleryName `
|
||||
--gallery-image-definition $imageDefinitionName `
|
||||
--gallery-image-version $GalleryImageVersion `
|
||||
--only-show-errors
|
||||
if ($LastExitCode) {
|
||||
Write-Host "Warning: Failed to delete the Image Version '$GalleryImageVersion'."
|
||||
}
|
||||
|
||||
Write-Host "Done."
|
||||
@@ -1,12 +1,11 @@
|
||||
param(
|
||||
[UInt32] [Parameter (Mandatory)] $BuildId,
|
||||
[String] [Parameter (Mandatory)] $Organization,
|
||||
[String] [Parameter (Mandatory)] $Project,
|
||||
[String] [Parameter (Mandatory)] $ImageName,
|
||||
[String] [Parameter (Mandatory)] $StorageAccountContainerName,
|
||||
[String] [Parameter (Mandatory)] $VhdName,
|
||||
[String] [Parameter (Mandatory)] $DefinitionId,
|
||||
[String] [Parameter (Mandatory)] $AccessToken
|
||||
[Parameter (Mandatory)] [UInt32] $BuildId,
|
||||
[Parameter (Mandatory)] [string] $Organization,
|
||||
[Parameter (Mandatory)] [string] $Project,
|
||||
[Parameter (Mandatory)] [string] $ImageType,
|
||||
[Parameter (Mandatory)] [string] $ManagedImageName,
|
||||
[Parameter (Mandatory)] [string] $DefinitionId,
|
||||
[Parameter (Mandatory)] [string] $AccessToken
|
||||
)
|
||||
|
||||
$Body = @{
|
||||
@@ -15,14 +14,11 @@ $Body = @{
|
||||
ImageBuildId = @{
|
||||
value = $BuildId
|
||||
}
|
||||
ImageName = @{
|
||||
value = $ImageName
|
||||
ImageType = @{
|
||||
value = $ImageType
|
||||
}
|
||||
ImageStorageContainerName = @{
|
||||
value = $StorageAccountContainerName
|
||||
}
|
||||
ImageBlobPath = @{
|
||||
value = $VhdName
|
||||
ManagedImageName = @{
|
||||
value = $ManagedImageName
|
||||
}
|
||||
}
|
||||
isDraft = "false"
|
||||
|
||||
Reference in New Issue
Block a user