-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathazure-pipelines-automate-pr-workitems-v1.0.yml
262 lines (250 loc) · 10.1 KB
/
azure-pipelines-automate-pr-workitems-v1.0.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
#####################################################
# TRIGGER CONDITION CAN BE ALTERED LIKE BELOW :-
#####################################################
# trigger:
# branches:
# include:
# - feature/*
# paths:
# include:
# - Automate-PR-and-Associate-WorkItems/*
#####################################################
#######################
# TRIGGER CONDITION :-
#######################
trigger: none
########################################################################
#DECLARE VARIABLES:-
# ONLY VARIABLE VALUES NEEDS TO BE ALTERED TO MAKE THE PIPELINE WORK.
########################################################################
variables:
PlanFilename: tfplan
TfvarFilename: usrmid.tfvars
KV-Name: ampockv
ServiceConnection: amcloud-cicd-service-connection
ResourceGroup: tfpipeline-rg
StorageAccount: tfpipelinesa
Container: terraform
TfstateFile: PR/createprworkitem.tfstate
BuildAgent: ubuntu-latest
PipelineEnv: NonProd
Terraform_Version: 1.2.3
WorkingDir: $(System.DefaultWorkingDirectory)/Automate-PR-and-Associate-WorkItems
Target: $(build.artifactstagingdirectory)/AMTF
Artifact: AM
anyTfChanges: false
DevOpsOrganisation: https://dev.azure.com/ArindamMitra0251
DevOpsProjName: AMCLOUD
DevOpsRepoName: PR
DevOpsDestinationBranch: main
######################
#DECLARE BUILD AGENT:-
######################
pool:
vmImage: $(BuildAgent)
###################
#DECLARE STAGES:-
###################
#################
# STAGE: BUILD
#################
stages:
- stage: BUILD
jobs:
- job: BUILD
displayName: BUILD
steps:
# Install Terraform Installer in the Build Agent:-
- task: ms-devlabs.custom-terraform-tasks.custom-terraform-installer-task.TerraformInstaller@0
displayName: INSTALL LATEST TERRAFORM VERSION
inputs:
terraformVersion: '$(Terraform_Version)'
# Terraform Init:-
- task: TerraformTaskV2@2
displayName: TERRAFORM INIT
inputs:
command: 'init'
provider: 'azurerm'
workingDirectory: '$(WorkingDir)'
backendServiceArm: '$(ServiceConnection)'
backendAzureRmResourceGroupName: '$(ResourceGroup)'
backendAzureRmStorageAccountName: '$(StorageAccount)'
backendAzureRmContainerName: '$(Container)'
backendAzureRmKey: '$(TfstateFile)'
# Terraform Validate:-
- task: TerraformTaskV2@2
displayName: TERRAFORM VALIDATE
inputs:
command: 'validate'
provider: 'azurerm'
workingDirectory: '$(WorkingDir)'
environmentServiceNameAzureRM: '$(ServiceConnection)'
# Terraform Plan:-
- task: TerraformTaskV2@2
displayName: TERRAFORM PLAN
inputs:
command: 'plan'
provider: 'azurerm'
workingDirectory: '$(WorkingDir)'
commandOptions: '--var-file=$(TfvarFilename) --out=$(PlanFilename)'
environmentServiceNameAzureRM: '$(ServiceConnection)'
# Detect Terraform Changes:-
- task: PowerShell@2
name: DetectTFChanges
displayName: DETECT TERRAFORM CHANGES
inputs:
workingDirectory: '$(workingDir)'
targetType: 'inline'
script: |
Write-Host "#######################################################"
Write-Host "Intial value of variable: $(anyTfChanges)"
Write-Host "#######################################################"
$plan = $(terraform show -json tfplan | ConvertFrom-Json)
$count = $plan.resource_changes.change.actions.length
$actions = ($plan.resource_changes | where { 'no-op' -notcontains $_.change.actions }).length -ne 0
Write-Host "##vso[task.setvariable variable=anyTfChanges;isOutput=true]$actions"
Write-Host "#######################################################"
Write-Host "Are there Changes in Infrastruture: $actions"
Write-Host "#######################################################"
Write-Host "TOTAL NO OF CHANGES: $count"
Write-Host "#######################################################"
# Copy Files to Artifacts Staging Directory:-
- task: CopyFiles@2
displayName: COPY FILES ARTIFACTS STAGING DIRECTORY
inputs:
SourceFolder: '$(WorkingDir)'
Contents: |
**/*.tf
**/*.tfvars
**/*$(PlanFilename)*
TargetFolder: '$(Target)'
# Publish Artifacts:-
- task: PublishBuildArtifacts@1
displayName: PUBLISH ARTIFACTS
inputs:
targetPath: '$(Target)'
artifactName: '$(Artifact)'
#################
# STAGE: DEPLOY
#################
- stage: DEPLOY
condition: |
and(succeeded(),
ne(variables['Build.SourceBranch'], 'refs/heads/main'),
eq(dependencies.BUILD.outputs['build.DetectTFChanges.anyTfChanges'], 'true')
)
jobs:
- deployment:
displayName: Deploy
environment: '$(PipelineEnv)'
pool:
vmImage: '$(BuildAgent)'
strategy:
runOnce:
deploy:
steps:
# Download Artifacts:-
- task: DownloadBuildArtifacts@0
displayName: DOWNLOAD ARTIFACTS
inputs:
buildType: 'current'
downloadType: 'single'
artifactName: '$(Artifact)'
downloadPath: '$(System.ArtifactsDirectory)'
# Install Terraform Installer in the Build Agent:-
- task: ms-devlabs.custom-terraform-tasks.custom-terraform-installer-task.TerraformInstaller@0
displayName: INSTALL LATEST TERRAFORM VERSION
inputs:
terraformVersion: '$(Terraform_Version)'
# Terraform Init:-
- task: TerraformTaskV2@2
displayName: TERRAFORM INIT
inputs:
provider: 'azurerm'
command: 'init'
workingDirectory: '$(System.ArtifactsDirectory)/$(Artifact)/AMTF/'
backendServiceArm: '$(ServiceConnection)'
backendAzureRmResourceGroupName: '$(ResourceGroup)'
backendAzureRmStorageAccountName: '$(StorageAccount)'
backendAzureRmContainerName: '$(Container)'
backendAzureRmKey: '$(TfstateFile)'
# Terraform Apply:-
- task: TerraformTaskV2@2
displayName: TERRAFORM APPLY
inputs:
provider: 'azurerm'
command: 'apply'
workingDirectory: '$(System.ArtifactsDirectory)/$(Artifact)/AMTF'
commandOptions: '--var-file=$(TfvarFilename)'
environmentServiceNameAzureRM: '$(ServiceConnection)'
##################################################################################################
# STAGE: CREATE PR
# CREATE AND COMPLETE PULL REQUEST BY ASSOCIATING WORKITEMS AND DELETING SOURCE BRANCH
#################################################################################################
- stage: PULL_REQUEST_ASSOCIATE_WORKITEMS
condition: |
and(succeeded(),
ne(variables['Build.SourceBranch'], 'refs/heads/main')
)
dependsOn: DEPLOY
jobs:
- job: PULL_REQUEST_WORKITEMS
displayName: CREATE PR | ASSOCIATE WORKITEMS | COMPLETE
steps:
# Download Keyvault Secrets:-
- task: AzureKeyVault@2
inputs:
azureSubscription: '$(ServiceConnection)'
KeyVaultName: '$(KV-Name)'
SecretsFilter: '*'
RunAsPreJob: false
# Install Az DevOps CLI Extension in the Build Agent:-
- task: AzureCLI@1
displayName: INSTALL DEVOPS CLI EXTENSION
inputs:
azureSubscription: '$(ServiceConnection)'
scriptType: ps
scriptLocation: inlineScript
inlineScript: |
az extension add --name azure-devops
az extension show --name azure-devops --output table
# Validate Az DevOps CLI Extension in the Build Agent:-
- task: PowerShell@2
displayName: VALIDATE AZ DEVOPS CLI
inputs:
targetType: 'inline'
script: |
az devops -h
# Set Default DevOps Organization and Project:-
- task: PowerShell@2
displayName: DEVOPS LOGIN + SET DEFAULT DEVOPS ORG & PROJECT
inputs:
targetType: 'inline'
script: |
echo "$(PAT)" | az devops login
az devops configure --defaults organization=$(DevOpsOrganisation) project=$(DevOpsProjName)
# Create Workitem + Create PR + Associate Workitem with PR + Complete the PR + Delete Source Branch:-
- task: PowerShell@2
displayName: CREATE & COMPLETE PULL REQUEST + WORKITEMS + DELETE SOURCE BRANCH
inputs:
targetType: 'inline'
script: |
Write-Host "#######################################################"
Write-Host "NAME OF THE SOURCE BRANCH: $(Build.SourceBranchName)"
Write-Host "#######################################################"
$i="PR-"
$j=Get-Random -Maximum 1000
Write-Host "###################################################"
Write-Host "WORKITEM NUMBER GENERATED IN DEVOPS BOARD: $i$j"
Write-Host "###################################################"
$wid = az boards work-item create --title $i$j --type "Issue" --query "id"
Write-Host "#######################################################"
Write-Host "WORKITEM ID is: $wid"
Write-Host "#######################################################"
$prid = az repos pr create --repository $(DevOpsRepoName) --source-branch $(Build.SourceBranchName) --target-branch $(DevOpsDestinationBranch) --work-items $wid --transition-work-items true --query "pullRequestId"
Write-Host "#######################################################"
Write-Host "PULL REQUEST ID is: $prid"
Write-Host "#######################################################"
Write-Host "##### TO BE MERGED FROM $(Build.SourceBranchName) TO Main #####"
az repos pr update --id $prid --auto-complete true --squash true --status completed --delete-source-branch true
Write-Host "##### MERGE SUCCESSFULL #####"