-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathpull.yml
283 lines (236 loc) · 6.95 KB
/
pull.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
---
name: "AzOps - Pull"
#
# Triggers
# Automated triggers are configured via Branch Policies
# within Azure Repos. It's also recommended to manually
# disable CI triggers with overrides.
#
trigger: none
#
# Schedule
# This is an optional trigger to pull the latest Azure
# hierarchy into the Git repository in a recurring
# manner.
#
# Default: Every 6 hours
#
schedules:
- cron: "0 */6 * * *"
branches:
include:
- main
always: true
#
# Pipelines
# Triggers this pipeline upon the completion of
# the Push pipeline.
#
resources:
pipelines:
- pipeline: 'Pull'
source: 'AzOps - Push'
trigger:
branches:
include:
- main
variables:
#
# Shared variables
# Include shared variables from the 'vars.yml' file
# to not have to repeat them in every pipeline.
#
- template: .templates/vars.yml
#
# Folder Name
# By default we generate the hierachy within the
# 'azops' folder within the root of the repository.
# If this property is modified, the config value within
# the settings.json file - Core.State will also need
# to be changed.
#
# Default: root
#
- name: folder
value: "root"
#
# Branch Name
# As part of the Pull workflow we check a temporary branch
# this branch was previously know as system, this value can
# be changed if this name is already reserved for other systems
# within the repository.
#
# Default: automated
#
- name: branch
value: "automated"
#
# Commit Message
# During the Pull workflow, the changes are commited to the
# temporary branch, the message which is applied within the
# Git history can be changed as needed.
#
# Default: Automated commit
#
- name: commit_message
value: "Automated commit"
#
# Pull Request
# The generated Pull Request for the Pull workflow can be
# modified to help indicate when changes we're merged in the
# Git history.
#
# Default: Automated state
#
- name: pull_request
value: "Automated State"
jobs:
- job: pull
#
# Pull
#
displayName: "Pull"
pool:
vmImage: "ubuntu-20.04"
steps:
#
# Shared steps
# Include shared steps from the 'shared.yml' file
# to not have to repeat them in every pipeline.
#
- template: .templates/sharedSteps.yml
#
# Configure
# Set global options
#
- task: Bash@3
displayName: "Configure"
inputs:
targetType: "inline"
script: |
git config user.name "Azure DevOps"
git config user.email "azuredevops@microsoft.com"
#
# Checkout
# Switch branches
#
- task: Bash@3
displayName: "Checkout"
inputs:
targetType: "inline"
script: |
git checkout -b $(branch)
#
# Verify that the Management resource provider is registered in the subscription where AzOps will initialize.
#
- task: PowerShell@2
displayName: "Pre-initialize"
inputs:
targetType: "inline"
script: |
$Env:PSModulePath = $Env:PSModulePath, '$(modulesFolder)' -join [IO.Path]::PathSeparator
$ManagementResourceProvider = Get-AzResourceProvider -ProviderNamespace 'microsoft.management' | Where-object {$_.ResourceTypes.ResourceTypeName -eq 'managementGroups'}
$InitSub = $((Get-AzContext).Subscription.Id)
if ($ManagementResourceProvider.RegistrationState -ne 'Registered') {
Write-verbose "The Azure Resource Provider 'Microsoft.Management' is not currently registered in the subscription '$InitSub'" -Verbose
try {
Write-verbose "Attempting to register the 'Microsoft.Management' Resource provider." -Verbose
Register-AzResourceProvider -ProviderNamespace 'Microsoft.Management'
} catch {
Write-Warning "Unable to register the 'Microsoft.Management' resource provider for subscription '$InitSub'"
throw
}
}
#
# Initialize
# Generate new state data
#
- task: PowerShell@2
displayName: "Initialize"
inputs:
targetType: "inline"
script: |
$Env:PSModulePath = $Env:PSModulePath, '$(modulesFolder)' -join [IO.Path]::PathSeparator
Import-PSFConfig -Path settings.json -Schema MetaJson -EnableException
Invoke-AzOpsPull -Rebuild
Get-Job | Remove-Job -Force
#
# Status
# Check for data changes
#
- task: Bash@3
displayName: "Status"
inputs:
targetType: "inline"
script: |
STATUS=$(git status --short $(folder))
echo $STATUS
if [ -z "$STATUS" ]
then
echo "##vso[task.setvariable variable=state]stop"
else
echo "##vso[task.setvariable variable=state]continue"
fi
#
# Add
# Add file content to index
#
- task: Bash@3
displayName: "Add"
condition: contains(variables['state'], 'continue')
inputs:
targetType: "inline"
script: |
git add ./$(folder)
#
# Commit
# Record changes to the repository
#
- task: Bash@3
displayName: "Commit"
condition: contains(variables['state'], 'continue')
inputs:
targetType: "inline"
script: |
git commit -m "$(commit_message)"
#
# Push
# Update remote refs along with associated objects
#
- task: Bash@3
displayName: "Push"
condition: contains(variables['state'], 'continue')
inputs:
targetType: "inline"
script: |
git push origin $(branch) -f
#
# Merge
# Update remote refs along with associated objects
#
- task: Bash@3
displayName: "Merge"
condition: contains(variables['state'], 'continue')
inputs:
targetType: "inline"
script: |
# Open new PR
PROut=$(
az repos pr create \
--title "$(pull_request)" \
--source-branch "$(branch)" \
--target-branch "main" \
--squash true \
--delete-source-branch true \
--auto-complete true \
);
# Get PR ID and check status
PRid=$(echo $PROut | jq -r '.pullRequestId');
PRStatus=$(az repos pr show --id $PRid | jq .status);
# If PR is not completed, then complete it bypassing policy
if [ $PRStatus == "\"active\"" ]; then
echo "Completing PR bypassing branch policy"
az repos pr update --status completed --id $PRid --bypass-policy true --bypass-policy-reason "Automated pull request" > /dev/null 2>&1
fi;
env:
AZURE_DEVOPS_EXT_PAT: $(System.AccessToken)