-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathBuild.ps1
64 lines (55 loc) · 1.93 KB
/
Build.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
[CmdletBinding()]
Param(
$Location = $env:Location,
$PackerFile = $env:Packerfile,
$ClientId = $env:ClientId,
$ClientSecret = $env:ClientSecret,
$TenantId = $env:TenantId,
$SubscriptionId = $env:SubscriptionId,
$ObjectId = $env:ObjectId,
$ManagedImageResourceGroupName = $env:ManagedImageResourceGroupName,
$ManagedImageName = $env:ManagedImageName,
[switch]$InstallPrerequisites
)
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
if ($InstallPrerequisites) {
"Installing prerequisites"
Set-ExecutionPolicy Bypass -Scope Process -Force
Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
"Install Packer"
choco install packer -y
"Install Git"
choco install git -y
"Install AzureRM PowerShell commands"
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
Install-Module AzureRM -AllowClobber -Force
Import-Module AzureRM
}
Get-AzureRmResourceGroup -Name $ManagedImageResourceGroupName -ErrorVariable notPresent -ErrorAction SilentlyContinue
if ( -Not $notPresent) {
"Cleaning up previous image versions"
Remove-AzureRmImage -ResourceGroupName $ManagedImageResourceGroupName -ImageName $ManagedImageName -Force
}
"Build Image"
if ($env:BUILD_REPOSITORY_LOCALPATH) {
Set-Location $env:BUILD_REPOSITORY_LOCALPATH
}
$commitId = $(git log --pretty=format:'%H' -n 1)
"CommitId: $commitId"
packer build `
-var "commit_id=$commitId" `
-var "client_id=$ClientId" `
-var "client_secret=$ClientSecret" `
-var "tenant_id=$TenantId" `
-var "subscription_id=$SubscriptionId" `
-var "object_id=$ObjectId" `
-var "location=$Location" `
-var "managed_image_resource_group_name=$ManagedImageResourceGroupName" `
-var "managed_image_name=$ManagedImageName" `
-on-error=abort `
$PackerFile
if ($LASTEXITCODE -eq 1){
Write-Error "Packer build faild"
exit 1
}