diff --git a/images.CI/azure-pipelines/image-generation.yml b/images.CI/azure-pipelines/image-generation.yml new file mode 100644 index 00000000..2f9840b6 --- /dev/null +++ b/images.CI/azure-pipelines/image-generation.yml @@ -0,0 +1,39 @@ +jobs: +- job: + pool: ci-agent-pool + timeoutInMinutes: 600 + variables: + - group: Image Generation Variables + + steps: + - task: PowerShell@2 + displayName: 'Build VM' + inputs: + targetType: filePath + filePath: ./images.CI/build-image.ps1 + arguments: -ResourcesNamePrefix $(Build.BuildNumber) ` + -ClientId $(CLIENT_ID) ` + -ClientSecret $(CLIENT_SECRET) ` + -Image ${{ parameters.image_type }} ` + -ResourceGroup $(AZURE_RESOURCE_GROUP) ` + -StorageAccount $(AZURE_STORAGE_ACCOUNT) ` + -SubscriptionId $(AZURE_SUBSCRIPTION) ` + -TenantId $(AZURE_TENANT) ` + -Location $(AZURE_LOCATION) ` + -VirtualNetworkName $(BUILD_AGENT_VNET_NAME) ` + -VirtualNetworkRG $(BUILD_AGENT_VNET_RESOURCE_GROUP) ` + -VirtualNetworkSubnet $(BUILD_AGENT_SUBNET_NAME) ` + -GitHubFeedToken $(GITHUB_TOKEN) + + - task: PowerShell@2 + displayName: 'Clean up resources' + condition: always() + inputs: + targetType: filePath + filePath: ./images.CI/cleanup.ps1 + arguments: -ResourcesNamePrefix $(Build.BuildNumber) ` + -ClientId $(CLIENT_ID) ` + -ClientSecret $(CLIENT_SECRET) ` + -Image ${{ parameters.image_type }} ` + -SubscriptionId $(AZURE_SUBSCRIPTION) ` + -TenantId $(AZURE_TENANT) \ No newline at end of file diff --git a/images.CI/azure-pipelines/ubuntu1604.yml b/images.CI/azure-pipelines/ubuntu1604.yml new file mode 100644 index 00000000..b19e79a1 --- /dev/null +++ b/images.CI/azure-pipelines/ubuntu1604.yml @@ -0,0 +1,19 @@ +# schedules: +# - cron: "0 0 * * *" +# displayName: Daily +# branches: +# include: +# - master +# always: true + +trigger: none +pr: + autoCancel: true + branches: + include: + - master + +jobs: +- template: image-generation.yml + parameters: + image_type: ubuntu1604 \ No newline at end of file diff --git a/images.CI/azure-pipelines/ubuntu1804.yml b/images.CI/azure-pipelines/ubuntu1804.yml new file mode 100644 index 00000000..db8acfd9 --- /dev/null +++ b/images.CI/azure-pipelines/ubuntu1804.yml @@ -0,0 +1,19 @@ +# schedules: +# - cron: "0 0 * * *" +# displayName: Daily +# branches: +# include: +# - master +# always: true + +trigger: none +pr: + autoCancel: true + branches: + include: + - master + +jobs: +- template: image-generation.yml + parameters: + image_type: ubuntu1804 \ No newline at end of file diff --git a/images.CI/azure-pipelines/windows2016.yml b/images.CI/azure-pipelines/windows2016.yml new file mode 100644 index 00000000..eaac2d1b --- /dev/null +++ b/images.CI/azure-pipelines/windows2016.yml @@ -0,0 +1,19 @@ +# schedules: +# - cron: "0 0 * * *" +# displayName: Daily +# branches: +# include: +# - master +# always: true + +trigger: none +pr: + autoCancel: true + branches: + include: + - master + +jobs: +- template: image-generation.yml + parameters: + image_type: Windows2016-Azure \ No newline at end of file diff --git a/images.CI/azure-pipelines/windows2019.yml b/images.CI/azure-pipelines/windows2019.yml new file mode 100644 index 00000000..ebbf68e7 --- /dev/null +++ b/images.CI/azure-pipelines/windows2019.yml @@ -0,0 +1,19 @@ +# schedules: +# - cron: "0 0 * * *" +# displayName: Daily +# branches: +# include: +# - master +# always: true + +trigger: none +pr: + autoCancel: true + branches: + include: + - master + +jobs: +- template: image-generation.yml + parameters: + image_type: Windows2019-Azure \ No newline at end of file diff --git a/images.CI/build-image.ps1 b/images.CI/build-image.ps1 new file mode 100644 index 00000000..6b75ec28 --- /dev/null +++ b/images.CI/build-image.ps1 @@ -0,0 +1,44 @@ +param( + [String] [Parameter (Mandatory=$true)] $Image, + [String] [Parameter (Mandatory=$true)] $ClientId, + [String] [Parameter (Mandatory=$true)] $ClientSecret, + [String] [Parameter (Mandatory=$true)] $GitHubFeedToken, + [String] [Parameter (Mandatory=$true)] $ResourcesNamePrefix, + [String] [Parameter (Mandatory=$true)] $Location, + [String] [Parameter (Mandatory=$true)] $ResourceGroup, + [String] [Parameter (Mandatory=$true)] $StorageAccount, + [String] [Parameter (Mandatory=$true)] $SubscriptionId, + [String] [Parameter (Mandatory=$true)] $TenantId, + [String] [Parameter (Mandatory=$true)] $VirtualNetworkName, + [String] [Parameter (Mandatory=$true)] $VirtualNetworkRG, + [String] [Parameter (Mandatory=$true)] $VirtualNetworkSubnet +) + +$TemplatePath = (Get-ChildItem -Path "images" -Include "$Image.json" -Recurse -Depth 2).FullName +if (-not $TemplatePath) +{ + Write-Error "'-Image' parameter is not valid. You have to specify correct image type." + exit 1 +} + +$TempResourceGroupName = "${ResourcesNamePrefix}_${Image}" +$InstallPassword = [System.GUID]::NewGuid().ToString().ToUpper() + +packer validate -syntax-only $TemplatePath + +Write-Host "Build $Image VM" +packer build -var "capture_name_prefix=$ResourcesNamePrefix" ` + -var "client_id=$ClientId" ` + -var "client_secret=$ClientSecret" ` + -var "install_password=$InstallPassword" ` + -var "github_feed_token=$GitHubFeedToken" ` + -var "location=$Location" ` + -var "resource_group=$ResourceGroup" ` + -var "storage_account=$StorageAccount" ` + -var "subscription_id=$SubscriptionId" ` + -var "temp_resource_group_name=$TempResourceGroupName" ` + -var "tenant_id=$TenantId" ` + -var "virtual_network_name=$VirtualNetworkName" ` + -var "virtual_network_resource_group_name=$VirtualNetworkRG" ` + -var "virtual_network_subnet_name=$VirtualNetworkSubnet" ` + $TemplatePath \ No newline at end of file diff --git a/images.CI/cleanup.ps1 b/images.CI/cleanup.ps1 new file mode 100644 index 00000000..00050d55 --- /dev/null +++ b/images.CI/cleanup.ps1 @@ -0,0 +1,21 @@ +param( + [String] [Parameter (Mandatory=$true)] $Image, + [String] [Parameter (Mandatory=$true)] $ResourcesNamePrefix, + [String] [Parameter (Mandatory=$true)] $ClientId, + [String] [Parameter (Mandatory=$true)] $ClientSecret, + [String] [Parameter (Mandatory=$true)] $SubscriptionId, + [String] [Parameter (Mandatory=$true)] $TenantId +) + +az login --service-principal --username $ClientId --password $ClientSecret --tenant $TenantId | Out-Null + +$TempResourceGroupName = "${ResourcesNamePrefix}_${Image}" + +$groupExist = az group exists --name $TempResourceGroupName --subscription $SubscriptionId | Out-Null +if ($groupExist -eq "true") { + Write-Host "Found a match, deleting temporary files" + az group delete --name $TempResourceGroupName --subscription $SubscriptionId --yes | Out-Null + Write-Host "Temporary group was deleted succesfully" -ForegroundColor Green +} else { + Write-Host "No temporary groups found" +} \ No newline at end of file