forked from dsccommunity/DscWorkshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path31 New Release Pipeline DscConfig.Demo.ps1
176 lines (144 loc) · 7.13 KB
/
31 New Release Pipeline DscConfig.Demo.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
if (-not (Get-Lab -ErrorAction SilentlyContinue).Name -eq 'DscWorkshop')
{
Import-Lab -Name DscWorkshop -NoValidation -ErrorAction Stop
}
$projectGitUrl = 'https://github.com/raandree/DscConfig.Demo'
$projectName = $projectGitUrl.Substring($projectGitUrl.LastIndexOf('/') + 1)
$collectionName = 'AutomatedLab'
$gitVersion = @{
Publisher = 'GitTools'
Extension = 'GitVersion'
Version = '5.0.1.3'
VsixPath = 'C:\gittools.gitversion-5.0.1.3.vsix'
}
$lab = Get-Lab
$devOpsServer = Get-LabVM -Role AzDevOps
$devOpsWorker = Get-LabVM -Role HyperV
$devOpsHostName = if ($lab.DefaultVirtualizationEngine -eq 'Azure')
{
$devOpsServer.AzureConnectionInfo.DnsName
}
else
{
$devOpsServer.FQDN
}
$nugetServer = Get-LabVM -Role AzDevOps
$nugetFeed = Get-LabTfsFeed -ComputerName $nugetServer -FeedName PowerShell
$devOpsRole = $devOpsServer.Roles | Where-Object Name -EQ AzDevOps
$devOpsCred = $devOpsServer.GetCredential($lab)
$devOpsPort = $originalPort = 8080
if ($devOpsRole.Properties.ContainsKey('Port'))
{
$devOpsPort = $devOpsRole.Properties['Port']
}
if ($lab.DefaultVirtualizationEngine -eq 'Azure')
{
$devOpsPort = (Get-LabAzureLoadBalancedPort -DestinationPort $devOpsPort -ComputerName $devOpsServer).Port
}
# Which will make use of Azure DevOps, clone the stuff, add the necessary build step, publish the test results and so on
# You will see two remotes, Origin (Our code on GitHub) and Azure DevOps (Our code pushed to your lab)
Write-ScreenInfo 'Creating Azure DevOps project and cloning from GitHub...' -NoNewLine
Copy-LabFileItem -Path $PSScriptRoot\LabData\gittools.gitversion-5.0.1.3.vsix -ComputerName $devOpsServer
Invoke-LabCommand -ActivityName "Uploading 'GitVersion' extension" -ComputerName $devOpsServer -ScriptBlock {
$param = @{
Uri = "https://$($devOpsHostName):$devOpsPort/_apis/gallery/extensions?api-version=3.0-preview.1"
Credential = $devOpsCred
Body = '{{"extensionManifest": "{0}"}}' -f ([Convert]::ToBase64String([IO.File]::ReadAllBytes($gitVersion.VsixPath)))
Method = 'POST'
ContentType = 'application/json'
}
if ($PSVersionTable.PSEdition -eq 'Core')
{
$param.Add('SkipCertificateCheck', $true)
}
$null = (Invoke-RestMethod @param)
} -Variable (Get-Variable -Name devOpsCred, devOpsHostName, devOpsPort, collectionName, gitVersion)
New-LabReleasePipeline -ProjectName $projectName -SourceRepository $projectGitUrl -CodeUploadMethod FileCopy
Invoke-LabCommand -ActivityName 'Bootstrap NuGet.exe' -ComputerName $devOpsServer, $devOpsWorker -ScriptBlock {
$nugetPath = 'C:\NuGet'
$nugetPathAllUsers = "$([System.Environment]::GetFolderPath('CommonApplicationData'))\Microsoft\Windows\PowerShell\PowerShellGet\NuGet.exe"
$nugetPathCurrentUser = "$([System.Environment]::GetFolderPath('LocalApplicationData'))\Microsoft\Windows\PowerShell\PowerShellGet\NuGet.exe"
if (-not (Test-Path -Path $nugetPath))
{
mkdir -Path $nugetPath
}
$hasNuget = if (Test-Path -Path $nugetPathAllUsers)
{
$nugetExe = Get-Item -Path $nugetPathAllUsers
Write-Host "'nuget.exe' exist in '$nugetPathAllUsers' with version '$($nugetExe.VersionInfo.FileVersionRaw)'"
if ($nugetExe.VersionInfo.FileVersionRaw -gt '5.11')
{
$true
}
}
if (-not $hasNuget)
{
if (Test-Path -Path $nugetPathCurrentUser)
{
$nugetExe = Get-Item -Path $nugetPathCurrentUser
Write-Host "'nuget.exe' exist in '$nugetPathCurrentUser' with version '$($nugetExe.VersionInfo.FileVersionRaw)'"
if ($nugetExe.VersionInfo.FileVersionRaw -gt '5.11')
{
$hasNuget = $true
}
}
}
if (-not $hasNuget)
{
Write-Host "'Nuget.exe' does not exist in ProgramData nor the local users profile, downloading into the users profile..."
Invoke-WebRequest -Uri 'https://aka.ms/psget-nugetexe' -OutFile $nugetPathCurrentUser -ErrorAction Stop
if (Test-Path -Path $nugetPathCurrentUser)
{
$nugetExe = Get-Item -Path $nugetPathCurrentUser -ErrorAction SilentlyContinue
Write-Host "'nuget.exe' exist in '$nugetPathCurrentUser' with version '$($nugetExe.VersionInfo.FileVersionRaw)'"
if ($nugetExe.VersionInfo.FileVersionRaw -lt '5.11')
{
Write-Host "'nuget.exe' has the version '$($nugetExe.VersionInfo.FileVersionRaw)' and needs to be updated."
Invoke-WebRequest -Uri 'https://aka.ms/psget-nugetexe' -OutFile $nugetPathCurrentUser -ErrorAction Stop
}
}
else
{
Write-Host "'nuget.exe' does not exist in the local profile and will be downloaded."
Invoke-WebRequest -Uri 'https://aka.ms/psget-nugetexe' -OutFile $nugetPathCurrentUser -ErrorAction Stop
}
}
else
{
Write-Host "OK: NuGet version $($nugetExe.VersionInfo.FileVersionRaw) in directory '$($nugetExe.Directory)' works"
}
Copy-Item -Path $nugetExe -Destination $nugetPath
[System.Environment]::SetEnvironmentVariable('Path', $env:Path + ';C:\NuGet', 'Machine')
}
Invoke-LabCommand -ActivityName "Installing 'GitVersion' extension" -ComputerName $devOpsServer -ScriptBlock {
Start-Sleep -Seconds 10
$param = @{
Uri = "https://$($devOpsHostName):$devOpsPort/$collectionName/_apis/extensionmanagement/installedextensionsbyname/$($gitVersion.Publisher)/$($gitVersion.Extension)/$($gitVersion.Version)?api-version=5.0-preview.1"
Credential = $devOpsCred
Method = 'POST'
ContentType = 'application/json'
}
if ($PSVersionTable.PSEdition -eq 'Core')
{
$param.Add('SkipCertificateCheck', $true)
}
$null = (Invoke-RestMethod @param)
} -Variable (Get-Variable -Name devOpsCred, devOpsHostName, devOpsPort, collectionName, gitVersion)
Invoke-LabCommand -ActivityName 'Set Repository and create Build Pipeline' -ScriptBlock {
Set-Location -Path C:\Git\DscConfig.Demo
git checkout main *>$null
Remove-Item -Path '.\azure-pipelines.yml'
(Get-Content -Path '.\azure-pipelines On-Prem.yml' -Raw) -replace 'RepositoryUri_WillBeChanged', $nugetFeed.NugetV2Url | Set-Content -Path .\azure-pipelines.yml
(Get-Content -Path .\Resolve-Dependency.psd1 -Raw) -replace 'PSGallery', 'PowerShell' | Set-Content -Path .\Resolve-Dependency.psd1
$content = [System.Collections.ArrayList](Get-Content -Path .\Resolve-Dependency.psd1)
$content.Insert(3, ' AllowOldPowerShellGetModule = $true # this is still required because of dependencies not following semantic versioning')
$content | Set-Content -Path .\Resolve-Dependency.psd1
(Get-Content -Path .\RequiredModules.psd1 -Raw) -replace 'PSGallery', 'PowerShell' | Set-Content -Path .\RequiredModules.psd1
git add .
git commit -m 'Set RepositoryUri and create Build Pipeline'
git push 2>$null
} -ComputerName $devOpsServer -Variable (Get-Variable -Name nugetFeed)
Write-Host 'Restarting Azure DevOps Server and worker machine...' -NoNewline
Restart-LabVM -ComputerName (Get-LabVM -Role AzDevOps, HyperV) -Wait -NoDisplay
Write-Host 'done'
Write-ScreenInfo done