-
Notifications
You must be signed in to change notification settings - Fork 1
/
New-AzPipeline.ps1
122 lines (109 loc) · 4.93 KB
/
New-AzPipeline.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
param (
[Parameter(Mandatory, HelpMessage = "Azure DevOps Organization: <OrganizationName>")][string]$OrganizationName,
[Parameter(Mandatory, HelpMessage = "Azure DevOps Project: <ProjectName>")][string]$ProjectName,
[Parameter(Mandatory, HelpMessage = "Azure DevOps Repository: RepositoryName>")][string]$RepositoryName,
[Parameter(HelpMessage = "Example: Pipelines")][string]$FolderPath = "Pipelines",
[Parameter(HelpMessage = "Example: 2020-04-11 or 2020-05-*")][string]$Version,
[Parameter(HelpMessage = "Example: C:\Modules\")][string]$PipelinePath = ".",
[Parameter()][switch]$Latest,
[Parameter()][switch]$All
)
$AzContext = Get-AzContext
if (!$AzContext) {
Write-Output "No Azure context found - Please make sure to login with the help of Connect-AzAccount or az login."
exit
}
$AzurePipelines = Get-ChildItem -Path $PipelinePath -Recurse | Where-Object { $_.Name -like "pipeline.yml" } | Sort-Object FullName
$PipelinesArray = @()
$OldPipelinesArray = @()
foreach ($Pipeline in $azurePipelines) {
$PipeObj = New-Object -TypeName PSCustomObject
$YmlPath = $Pipeline.fullname.replace("\", "/")
$PathSplit = $YmlPath.Split("/")
$YmlPath = $PathSplit[-6] + "/" + $PathSplit[-5] + "/" + $PathSplit[-4] + "/" + $PathSplit[-3] + "/" + $PathSplit[-2] + "/" + $PathSplit[-1]
$ModuleName = $PathSplit[-4]
$PipelineName = $PathSplit[-4] + "-" + $PathSplit[-3]
$PipelineVersion = $PathSplit[-3]
$PipeObj | Add-Member -MemberType NoteProperty -Name ProjectName -Value $ProjectName
$PipeObj | Add-Member -MemberType NoteProperty -Name RepositoryName -Value $RepositoryName
$PipeObj | Add-Member -MemberType NoteProperty -Name FolderPath -Value $FolderPath
$PipeObj | Add-Member -MemberType NoteProperty -Name YmlPath -Value $YmlPath
$PipeObj | Add-Member -MemberType NoteProperty -Name ModuleName -Value $ModuleName
$PipeObj | Add-Member -MemberType NoteProperty -Name PipelineName -Value $PipelineName
$PipeObj | Add-Member -MemberType NoteProperty -Name PipelineVersion -Value $PipelineVersion
if ($Version -lt $PipelineVersion) {
$PipelinesArray += $PipeObj
}
elseif ($All -and !$Latest) {
$PipelinesArray += $PipeObj
}
else {
$OldPipelinesArray += $PipeObj
}
}
if ($PipelinesArray.Count -gt 0) {
Write-Output "$($PipelinesArray.Count) Pipelines have been identified."
} else {
Write-Output "No Pipelines have been identified. Exiting."
Start-Sleep 1
exit
}
if ($OldPipelinesArray) {
Write-Output "$($OldPipelinesArray.Count) Modules are older than $Version and will be skipped."
}
if (!$All -and $Latest) {
$Modules = ($PipelinesArray).ModuleName | Select-Object -Unique
$LatestModules = @()
foreach ($Module in $Modules) {
$LatestModules += $PipelinesArray | Where-Object { $_.ModuleName -eq $Module } | Sort-Object PipelineVersion | Select-Object -Last 1
}
$PipelinesArray = @()
$PipelinesArray = $LatestModules
Write-Output "$($PipelinesArray.Count) Azure Pipelines (latest) will be created."
}
if ($Version -ne "") {
Write-Output "$($PipelinesArray.Count) Azure Pipeline(s) prior to $Version version will be created."
}
if ($All -and !$Latest) {
$Modules = ($PipelinesArray).ModuleName | Select-Object -Unique
$LatestModules = @()
foreach ($Module in $Modules) {
$LatestModules += $PipelinesArray | Where-Object { $_.ModuleName -eq $Module } | Sort-Object PipelineVersion | Select-Object -Last 1
}
$PipelinesArray = @()
$PipelinesArray = $LatestModules
Write-Output "$($PipelinesArray.Count) Azure Pipelines will be created."
}
$PipelinesSkipped = @()
$PipelinesCreated = @()
$ExistingPipelines = (az pipelines list --organization ("https://dev.azure.com/" + $OrganizationName) --project $ProjectName | ConvertFrom-Json).name
foreach ($Pipeline in $PipelinesArray) {
if ($Pipeline.PipelineName -notin $ExistingPipelines) {
Write-Output "Creating Azure Pipeline $($Pipeline.PipelineName) ... "
$Job = Start-Job -Name $Pipeline.PipelineName -ScriptBlock {
param($Pipeline)
$ErrorActionPreference = SilentlyContinue
az pipelines create --project "$($Pipeline.ProjectName)" `
--repository "$($Pipeline.RepositoryName)" `
--repository-type tfsgit --branch master `
--folder-path "$($Pipeline.FolderPath)" `
--name "$($Pipeline.PipelineName)" `
--yml-path "$($Pipeline.YmlPath)" `
--skip-run
} -ArgumentList $Pipeline
$PipelinesCreated += $Pipeline
}
else {
Write-Output "Azure Pipeline $($Pipeline.PipelineName) exists already and will be skipped."
$PipelinesSkipped += $Pipeline
}
}
Write-Host "Please wait " -NoNewline
while (Get-Job -State Running) {
Start-Sleep 5
Write-Host ". " -NoNewline
}
Write-Output "$($PipelinesCreated.Count) Azure Pipeline(s) have been created!"
Write-Output "$($PipelinesSkipped.Count) Azure Pipeline(s) have been skipped!"
$Url = "https://dev.azure.com/" + $OrganizationName + "/" + $adoProject + "/" + "_build?view=folders"
Write-Output "Please check your Azure Pipelines here: $Url"