forked from teach-for-america-archive/ssiscicd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathazure-pipelines.yml
231 lines (210 loc) · 11.4 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
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
# Starter pipeline
# Start with a minimal pipeline that you can customize to build and deploy your code.
# Add steps that build, run tests, deploy, and more:
# https://aka.ms/yaml
name: $(TeamProject)-$(Date:yyyyMMdd)$(Rev:-r)
variables:
projectName: AdventureWorksExample
trigger:
- master
# Using the default pool, because it is the one with the self-hosted agent, required by builds using Sql Server Data Tools.
pool: default
steps:
- checkout: self # self represents the repo where the initial Pipelines YAML file was found
clean: true
path: source
- task: PowerShell@1
displayName: Build SSIS and Database Unit Test Projects
inputs:
scriptType: 'inlineScript'
inlineScript: |
& "C:\Program Files (x86)\Microsoft Visual Studio\2017\SQL\Common7\IDE\devenv.com" $(Agent.BuildDirectory)\source\SLN_$(projectName)\SLN_$(projectName).sln /Build Development /project $(Agent.BuildDirectory)\source\SLN_$(projectName)\$(projectName)\$(projectName).dtproj /out log.txt
& "C:\Program Files (x86)\Microsoft Visual Studio\2017\SQL\Common7\IDE\devenv.com" $(Agent.BuildDirectory)\source\SLN_$(projectName)\SLN_$(projectName).sln /Build Debug /project $(Agent.BuildDirectory)\source\SLN_$(projectName)\$(projectName)_UnitTests\$(projectName)_UnitTests.sqlproj /out log.txt
- task: PublishBuildArtifacts@1
displayName: Publish ispac file to Pipeline Build Artifacts
inputs:
PathtoPublish: '$(Agent.BuildDirectory)\source\SLN_$(projectName)\$(projectName)\bin\Development'
ArtifactName: '$(projectName).ispac'
publishLocation: 'Container'
- task: PublishBuildArtifacts@1
displayName: Publish dacpac file to Pipeline Build Artifacts
inputs:
PathtoPublish: '$(Agent.BuildDirectory)\source\SLN_$(projectName)\$(projectName)_UnitTests\bin\Debug'
ArtifactName: '$(projectName)_UnitTests.dacpac'
publishLocation: 'Container'
# Azure Key Vault
# Download Azure Key Vault secrets
- task: AzureKeyVault@1
displayName: Retrieve Secrets from Azure Key Vault
inputs:
azureSubscription: 'Teach For America'
KeyVaultName: 'ssiscicdAKV'
SecretsFilter: '*'
# Technically we need a pom.xml file to use maven. But that seems like a lot, considering we aren't using maven to build the project (because it can't).
# So instead, we use a simple, generic pom.xml file from https://maven.apache.org/guides/introduction/introduction-to-the-pom.html and update the values on the command line.
- task: PowerShell@1
displayName: Publish ispac file to ssiscicd Artifact feed
inputs:
scriptType: 'inlineScript'
inlineScript: |
$settingsPath = "settings.xml";
Set-Content -Path $settingsPath -Value $env:MAVEN_CRED;
$minimalPomPath = "pom.xml";
Set-Content -Path $minimalPomPath -Value @"
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
</project>
"@;
& "C:\Program Files\apache-maven-3.6.2\bin\mvn.cmd" -s settings.xml -Durl=https://pkgs.dev.azure.com/elizabethbaron/ssiscicd/_packaging/ssiscicd/maven/v1 -DgroupId="org.teachforamerica.ssiscicd.demo" -DartifactId=$(projectName) -Dversion="1.0.$(Build.BuildId)" -Dfile=$(Agent.BuildDirectory)\source\SLN_$(projectName)\$(projectName)\bin\Development\$(projectName).ispac -Dpackaging=ispac -DrepositoryId=dev-azure-com-elizabethbaron-ssiscicd deploy:deploy-file -e
env:
MAVEN_CRED: $(mvncred)
- task: Kubernetes@1
displayName: kubectl create namespace pipeline-$(Build.BuildNumber)
inputs:
connectionType: 'Azure Resource Manager'
azureSubscriptionEndpoint: 'Teach For America'
azureResourceGroup: '$(azureResourceGroup)'
kubernetesCluster: '$(kubernetesCluster)'
command: 'create'
arguments: namespace pipeline-$(Build.BuildNumber)
outputFormat: 'yaml'
- task: Kubernetes@1
displayName: kubectl setup secret to container registry in namespace pipeline-$(Build.BuildNumber)
inputs:
connectionType: 'Azure Resource Manager'
azureSubscriptionEndpoint: 'Teach For America'
azureResourceGroup: '$(azureResourceGroup)'
kubernetesCluster: '$(kubernetesCluster)'
namespace: 'pipeline-$(Build.BuildNumber)'
command: 'create'
arguments: 'secret docker-registry regcred --docker-server=$(docker-server) --docker-username=$(docker-username) --docker-password=$(docker-password) --docker-email=$(docker-email)'
outputFormat: 'yaml'
- task: Kubernetes@1
displayName: kubectl apply deployment in pipeline-$(Build.BuildNumber)
inputs:
connectionType: 'Azure Resource Manager'
azureSubscriptionEndpoint: 'Teach For America'
azureResourceGroup: '$(azureResourceGroup)'
kubernetesCluster: '$(kubernetesCluster)'
namespace: 'pipeline-$(Build.BuildNumber)'
command: 'apply'
useConfigurationFile: true
configuration: aks\mssqlssis.deployment.yaml
secretType: 'dockerRegistry'
containerRegistryType: 'Azure Container Registry'
outputFormat: 'yaml'
- task: PowerShell@1
displayName: Login to Azure Cloud and set podName variable
inputs:
scriptType: 'inlineScript'
inlineScript: |
&az login --service-principal -u $(ssiscicdServicePrincipalURL) -p $(ssiscicdServicePrincipalPassword) --tenant $(ssiscicdTenantId)
&az aks get-credentials --resource-group $(azureResourceGroup) --name $(kubernetesCluster)
# Get all running pods in the namespace
$content = &kubectl get pods -n pipeline-$(Build.BuildNumber) --field-selector=status.phase=Running 2>&1
# Ensure that this task does not run for more than about 90 seconds
$waitLimit = 90
$timeWaiting = 0
While (($content -like 'No resources found*') -and ($timeWaiting -lt $waitLimit))
{
start-sleep -Seconds 5
$timeWaiting = $timeWaiting + 5
$content = &kubectl get pods -n pipeline-$(Build.BuildNumber) --field-selector=status.phase=Running 2>&1
}
$content = &kubectl get pods -n pipeline-$(Build.BuildNumber) --field-selector=status.phase=Running -o jsonpath="{.items[0].metadata.name}"
Write-Host $content
Write-Host "##vso[task.setvariable variable=podName;]$content"
- task: Kubernetes@1
displayName: kubectl create the service for the deployment in pipeline-$(Build.BuildNumber)
inputs:
connectionType: 'Azure Resource Manager'
azureSubscriptionEndpoint: 'Teach For America'
azureResourceGroup: '$(azureResourceGroup)'
kubernetesCluster: '$(kubernetesCluster)'
namespace: 'pipeline-$(Build.BuildNumber)'
command: 'expose'
arguments: 'deployment mssqlssis-deployment --type=LoadBalancer --name=mssqlssis-service --port=$(port) --target-port=1433'
outputFormat: 'yaml'
- task: PowerShell@1
displayName: Login to Azure Cloud and set ipAddress variable
inputs:
scriptType: 'inlineScript'
inlineScript: |
&az login --service-principal -u $(ssiscicdServicePrincipalURL) -p $(ssiscicdServicePrincipalPassword) --tenant $(ssiscicdTenantId)
&az aks get-credentials --resource-group $(azureResourceGroup) --name $(kubernetesCluster)
# Get ipAddress for service in namespace
$content = &kubectl get services -n pipeline-$(Build.BuildNumber) -o jsonpath="{.items[0].status.loadBalancer.ingress[0].ip}"
# Ensure that this task does not run for more than about 90 seconds
$waitLimit = 90
$timeWaiting = 0
While ([String]::IsNullOrWhiteSpace($content) -and ($timeWaiting -lt $waitLimit))
{
start-sleep -Seconds 5
$timeWaiting = $timeWaiting + 5
$content = &kubectl get services -n pipeline-$(Build.BuildNumber) -o jsonpath="{.items[0].status.loadBalancer.ingress[0].ip}"
}
$content = &kubectl get services -n pipeline-$(Build.BuildNumber) -o jsonpath="{.items[0].status.loadBalancer.ingress[0].ip}"
Write-Host $content
Write-Host "##vso[task.setvariable variable=ipAddress;]$content"
- task: Kubernetes@1
displayName: kubectl download and install ssis package in pipeline-$(Build.BuildNumber)
inputs:
connectionType: 'Azure Resource Manager'
azureSubscriptionEndpoint: 'Teach For America'
azureResourceGroup: '$(azureResourceGroup)'
kubernetesCluster: '$(kubernetesCluster)'
namespace: 'pipeline-$(Build.BuildNumber)'
command: 'exec'
arguments: '$(podName) -- powershell.exe -File SSIS_SCRIPTS/deploy_ssis_package.ps1 https://pkgs.dev.azure.com/elizabethbaron/ssiscicd/_packaging/ssiscicd/maven/v1/org/teachforamerica/ssiscicd/demo/$(projectName)/1.0.$(Build.BuildId)/$(projectName)-1.0.$(Build.BuildId).ispac $(projectName)-1.0.$(Build.BuildId).ispac $(projectName)'
outputFormat: 'yaml'
- task: PowerShell@1
displayName: Deploy unit test dacpac(s)
inputs:
scriptType: 'inlineScript'
workingFolder: '$(Agent.BuildDirectory)\source'
inlineScript: |
Copy-Item "$(Agent.BuildDirectory)\source\master.dacpac" -Destination "$(Agent.BuildDirectory)\source\SLN_$(projectName)\$(projectName)_UnitTests\Referenced Databases"
Copy-Item "$(Agent.BuildDirectory)\source\msdb.dacpac" -Destination "$(Agent.BuildDirectory)\source\SLN_$(projectName)\$(projectName)_UnitTests\Referenced Databases"
& "C:\Program Files\Microsoft SQL Server\150\DAC\bin\SqlPackage.exe" /Action:Publish /SourceFile:"SLN_$(projectName)\$(projectName)_UnitTests\Referenced Databases\db_tsqlt_ssis.dacpac" /TargetConnectionString:"Data Source=$(ipAddress),$(port);User ID=sa;Password=$(saPassword);Persist Security Info=False;Pooling=False;MultipleActiveResultSets=False;Connect Timeout=60;Encrypt=False;TrustServerCertificate=True;Initial Catalog=$(projectName)_UnitTests"
& "C:\Program Files\Microsoft SQL Server\150\DAC\bin\SqlPackage.exe" /Action:Publish /SourceFile:SLN_$(projectName)\$(projectName)_UnitTests\bin\Debug\$(projectName)_UnitTests.dacpac /TargetConnectionString:"Data Source=$(ipAddress),$(port);User ID=sa;Password=$(saPassword);Persist Security Info=False;Pooling=False;MultipleActiveResultSets=False;Connect Timeout=60;Encrypt=False;TrustServerCertificate=True;Initial Catalog=$(projectName)_UnitTests"
- task: Kubernetes@1
displayName: kubectl run unit tests in pipeline-$(Build.BuildNumber)
inputs:
connectionType: 'Azure Resource Manager'
azureSubscriptionEndpoint: 'Teach For America'
azureResourceGroup: '$(azureResourceGroup)'
kubernetesCluster: '$(kubernetesCluster)'
namespace: 'pipeline-$(Build.BuildNumber)'
command: 'exec'
arguments: '$(podName) -- sqlcmd.exe -d $(projectName)_UnitTests -Q "EXEC [tSQLt].[RunAll]"'
outputFormat: 'yaml'
- task: PowerShell@1
displayName: Save test results to xml file
inputs:
scriptType: 'inlineScript'
workingFolder: '$(Agent.BuildDirectory)\source'
inlineScript: |
$xmlTestResultsFormatting = "xmlFormat.sql";
Set-Content -Path $xmlTestResultsFormatting -Value @"
:XML ON
EXEC [tSQLt].[XmlResultFormatter];
"@;
& "C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\SQLCMD.EXE" -S $(ipAddress),$(port) -U sa -P $(saPassword) -d $(projectName)_UnitTests -i $xmlTestResultsFormatting -o test-results.xml
- task: PublishTestResults@2
inputs:
testResultsFormat: 'JUnit'
testResultsFiles: '**/TEST-*.xml'
failTaskOnFailedTests: false
- task: Kubernetes@1
displayName: kubectl delete namespace pipeline-$(Build.BuildNumber)
inputs:
connectionType: 'Azure Resource Manager'
azureSubscriptionEndpoint: 'Teach For America'
azureResourceGroup: '$(azureResourceGroup)'
kubernetesCluster: '$(kubernetesCluster)'
command: 'delete'
arguments: namespace pipeline-$(Build.BuildNumber)
outputFormat: 'yaml'