/
azure-pipelines.yml
202 lines (188 loc) · 8.39 KB
/
azure-pipelines.yml
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
variables:
- group: 'GitHubAuth'
- group: 'ServicePrincipal'
- group: 'SubscriptionMemberGroups'
stages:
- stage: Validate
pool:
vmImage: 'windows-latest'
jobs:
- job: Import
continueOnError: false
steps:
- task: CmdLine@2
name: CloneGraphAPI
displayName: Clone Graph API repo
inputs:
script: 'git clone --branch $(Branch) --single-branch https://github.com/wesley-trust/GraphAPI.git'
workingDirectory: '$(System.ArtifactsDirectory)'
- task: PowerShell@2
name: InvokeWTValidateSubscription
displayName: Invoke-WTValidateSubscription
inputs:
targetType: 'inline'
script: |
# Dot source function
. $(System.ArtifactsDirectory)\GraphAPI\Public\AzureAD\Subscriptions\Pipeline\Invoke-WTValidateSubscription.ps1
# Test if directory exist and execute function as appropriate
$TestPath = Test-Path $(Build.Repository.LocalPath)\AzureAD\Subscriptions\Definitions -PathType Container
if ($TestPath){
$ValidateDefinedSubscriptions = Invoke-WTValidateSubscription `
-Path $(Build.Repository.LocalPath)\AzureAD\Subscriptions\Definitions
}
# Create directory for artifact, if it does not exist
$TestPath = Test-Path $(Pipeline.Workspace)\Output -PathType Container
if (!$TestPath){
New-Item -Path $(Pipeline.Workspace)\Output -ItemType Directory | Out-Null
}
# If there are Subscriptions (as if there are no Subscriptions to import, existing Subscriptions are not removed)
if ($ValidateDefinedSubscriptions){
# Convert to JSON and export
$ValidateDefinedSubscriptions | ConvertTo-Json -Depth 10 | Out-File -Force -FilePath $(Pipeline.Workspace)\Output\Validate.json
}
pwsh: true
workingDirectory: '$(System.ArtifactsDirectory)'
- task: PublishPipelineArtifact@1
inputs:
targetPath: '$(Pipeline.Workspace)\Output'
artifact: 'Import'
publishLocation: 'pipeline'
- stage: Plan
pool:
vmImage: 'windows-latest'
dependsOn: Validate
condition: succeeded()
jobs:
- job: Evaluate
continueOnError: false
steps:
- task: DownloadPipelineArtifact@2
inputs:
buildType: 'current'
targetPath: '$(Pipeline.Workspace)'
- task: CmdLine@2
name: CloneGraphAPI
displayName: Clone Graph API repo
inputs:
script: 'git clone --branch $(Branch) --single-branch https://github.com/wesley-trust/GraphAPI.git'
workingDirectory: '$(System.ArtifactsDirectory)'
- task: CmdLine@2
name: CloneToolKit
displayName: Clone Toolkit repo
inputs:
script: 'git clone --branch $(Branch) --single-branch https://github.com/wesley-trust/ToolKit.git'
workingDirectory: '$(System.ArtifactsDirectory)'
- task: PowerShell@2
name: InvokeWTPlanSubscription
displayName: Invoke-WTPlanSubscription
inputs:
targetType: 'inline'
script: |
# Import and convert Subscriptions from JSON, should they exist
$TestPath = Test-Path $(Pipeline.Workspace)\Import\Validate.json -PathType Leaf
if ($TestPath){
$ValidateDefinedSubscriptions = Get-Content -Raw -Path $(Pipeline.Workspace)\Import\Validate.json | ConvertFrom-Json -Depth 10
}
# Dot source and execute function
. $(System.ArtifactsDirectory)\GraphAPI\Public\AzureAD\Subscriptions\Pipeline\Invoke-WTPlanSubscription.ps1
$PlanDefinedSubscriptions = Invoke-WTPlanSubscription `
-TenantDomain $(TenantDomain) `
-ClientID ${env:CLIENTID} `
-ClientSecret ${env:CLIENTSECRET} `
-DefinedSubscriptions $ValidateDefinedSubscriptions `
-RemoveDefinedSubscriptions `
-Force
# Create directory for artifact, if it does not exist
$TestPath = Test-Path $(Pipeline.Workspace)\Output -PathType Container
if (!$TestPath){
New-Item -Path $(Pipeline.Workspace)\Output -ItemType Directory | Out-Null
}
# If there are Subscriptions
if ($PlanDefinedSubscriptions.RemoveSubscriptions -or $PlanDefinedSubscriptions.CreateSubscriptions){
# Set ShouldRun variable to true, for apply stage
echo "##vso[task.setvariable variable=ShouldRun;isOutput=true]true"
# Convert to JSON and export
$PlanDefinedSubscriptions | ConvertTo-Json -Depth 10 | Out-File -Force -FilePath $(Pipeline.Workspace)\Output\Plan.json
}
pwsh: true
workingDirectory: '$(System.ArtifactsDirectory)'
env:
CLIENTID: $(ClientID)
CLIENTSECRET: $(ClientSecret)
- task: PublishPipelineArtifact@1
inputs:
targetPath: '$(Pipeline.Workspace)\Output'
artifact: 'Evaluate'
publishLocation: 'pipeline'
- stage: Apply
pool:
vmImage: 'windows-latest'
dependsOn: Plan
condition: and(succeeded(), eq(dependencies.Plan.outputs['Evaluate.InvokeWTPlanSubscription.ShouldRun'], 'true'))
jobs:
- deployment: Deploy
continueOnError: false
environment: $(Environment)
strategy:
runOnce:
deploy:
steps:
- checkout: self
- task: CmdLine@2
name: CloneGraphAPI
displayName: Clone Graph API repo
inputs:
script: 'git clone --branch $(Branch) --single-branch https://github.com/wesley-trust/GraphAPI.git'
workingDirectory: '$(System.ArtifactsDirectory)'
- task: CmdLine@2
name: CloneToolKit
displayName: Clone Toolkit repo
inputs:
script: 'git clone --branch $(Branch) --single-branch https://github.com/wesley-trust/ToolKit.git'
workingDirectory: '$(System.ArtifactsDirectory)'
- task: PowerShell@2
name: InvokeWTApplySubscription
displayName: Invoke-WTApplySubscription
inputs:
targetType: 'inline'
script: |
# Import and convert Subscriptions from JSON, should they exist
$TestPath = Test-Path $(Pipeline.Workspace)\Evaluate\Plan.json -PathType Leaf
if ($TestPath){
$PlanDefinedSubscriptions = Get-Content -Raw -Path $(Pipeline.Workspace)\Evaluate\Plan.json | ConvertFrom-Json -Depth 10
}
# Import service plan dependencies if they exist and convert from JSON
$DependentServicePlansPath = "$(Build.Repository.LocalPath)\AzureAD\Subscriptions\Dependencies"
$PathExists = Test-Path -Path $DependentServicePlansPath
if ($PathExists) {
$DependentServicePlansFilePath = (Get-ChildItem -Path $DependentServicePlansPath -Filter "*.json").FullName
}
if ($DependentServicePlansFilePath) {
$DependentServicePlansImport = foreach ($DependentServicePlanFile in $DependentServicePlansFilePath) {
Get-Content -Raw -Path $DependentServicePlanFile
}
}
if ($DependentServicePlansImport) {
$DependentServicePlans = $DependentServicePlansImport | ConvertFrom-Json -Depth 10
}
# Dot source and execute function
. $(System.ArtifactsDirectory)\GraphAPI\Public\AzureAD\Subscriptions\Pipeline\Invoke-WTApplySubscription.ps1
Invoke-WTApplySubscription `
-TenantDomain $(TenantDomain) `
-ClientID ${env:CLIENTID} `
-ClientSecret ${env:CLIENTSECRET} `
-DefinedSubscriptions $PlanDefinedSubscriptions `
-DependentServicePlans $DependentServicePlans `
-RemoveDefinedSubscriptions `
-Path $(Build.SourcesDirectory)\AzureAD\Subscriptions\Definitions `
-Pipeline
pwsh: true
workingDirectory: '$(System.ArtifactsDirectory)'
env:
CLIENTID: $(ClientID)
CLIENTSECRET: $(ClientSecret)
GITHUBPAT: $(GitHubPAT)
REPOHOME: $(Build.Repository.LocalPath)
BRANCH: $(Branch)
USERGROUPID: $(UserGroupID)
GITHUBCONFIGREPO: $(GitHubConfigRepo)