|
| 1 | +# Deploy to Azure Kubernetes Service |
| 2 | +# Build and push image to Azure Container Registry; Deploy to Azure Kubernetes Service |
| 3 | +# https://docs.microsoft.com/azure/devops/pipelines/languages/docker |
| 4 | + |
| 5 | +# Trigger only for master branch, Bikes folder |
| 6 | +trigger: |
| 7 | + branches: |
| 8 | + include: |
| 9 | + - master |
| 10 | + paths: |
| 11 | + include: |
| 12 | + - samples/BikeSharingApp/Bikes/* |
| 13 | + |
| 14 | +resources: |
| 15 | +- repo: self |
| 16 | + |
| 17 | +variables: |
| 18 | + |
| 19 | + # Create container registry service connection and provide the connection name or it's GUID here. Learn more https://docs.microsoft.com/en-us/azure/devops/pipelines/library/service-endpoints?view=azure-devops&tabs=yaml |
| 20 | + dockerRegistryServiceConnection: 'CONTAINER-REGISTRY-CONNECTION-NAME' |
| 21 | + |
| 22 | + # Provide the container registry url for example builddemo.azurecr.io |
| 23 | + containerRegistry: 'CONTAINER-REGISTRY-URL' |
| 24 | + |
| 25 | + # Create Kubernetes service connection and provide the connection name or it's GUID here. Learn more https://docs.microsoft.com/en-us/azure/devops/pipelines/library/service-endpoints?view=azure-devops&tabs=yaml |
| 26 | + k8sServiceConnection: 'KUBERNETES-CONNECTION-NAME' |
| 27 | + |
| 28 | + # Provide the name of Kubernetes cluster |
| 29 | + AZDSControllerName: 'KUBERNETES-CLUSTER-NAME' |
| 30 | + |
| 31 | + # Provide the resource group name of Kubernetes cluster |
| 32 | + AZDSControllerRG: 'KUBERNETES-CLUSTER-RESOURCE-GROUP' |
| 33 | + |
| 34 | + # Create Azure Service connection and provide the connection name or it's GUID here. Learn more https://docs.microsoft.com/en-us/azure/devops/pipelines/library/service-endpoints?view=azure-devops&tabs=yaml#sep-azure-rm |
| 35 | + azureServiceConnection: 'AZURE-CONNECTION-NAME' |
| 36 | + |
| 37 | + # Create GitHub Service connection and provide the connection name or it's GUID here. Learn more https://docs.microsoft.com/en-us/azure/devops/pipelines/library/service-endpoints?view=azure-devops&tabs=yaml#sep-github |
| 38 | + githubServiceConnection: 'GITHUB-CONNECTION-NAME' |
| 39 | + |
| 40 | + imageRepository: 'bikesharing-bikes' |
| 41 | + dockerfilePath: 'samples/BikeSharingApp/Bikes/Dockerfile' |
| 42 | + tag: '$(Build.BuildId)' |
| 43 | + |
| 44 | + # Kubernetes Namespace where all changes from master branch are deployed |
| 45 | + k8sNamespace: 'dev' |
| 46 | + # Dynamically created Kubernetes Namespace where all pull request changes are deployed |
| 47 | + k8sNamespaceforpr: $(system.pullRequest.sourceBranch) |
| 48 | + |
| 49 | + # Name of imagepullsecret |
| 50 | + imagePullSecret: 'builddemodfb0-auth' |
| 51 | + |
| 52 | + |
| 53 | +# CI stage for build and pushing image to container registry |
| 54 | +stages: |
| 55 | +- stage: Build |
| 56 | + displayName: Build stage |
| 57 | + jobs: |
| 58 | + - job: Build |
| 59 | + displayName: Build job |
| 60 | + pool: |
| 61 | + vmImage: 'ubuntu-latest' |
| 62 | + |
| 63 | + steps: |
| 64 | + |
| 65 | + - task: Docker@2 |
| 66 | + displayName: Build and push an image to container registry |
| 67 | + inputs: |
| 68 | + command: buildAndPush |
| 69 | + repository: $(imageRepository) |
| 70 | + dockerfile: $(dockerfilePath) |
| 71 | + containerRegistry: $(dockerRegistryServiceConnection) |
| 72 | + tags: | |
| 73 | + $(tag) |
| 74 | + |
| 75 | + - task: PublishPipelineArtifact@0 |
| 76 | + inputs: |
| 77 | + artifactName: 'charts' |
| 78 | + targetPath: 'samples/BikeSharingApp/Bikes/charts/bikes' |
| 79 | + |
| 80 | +# Deploy all changes from master branch |
| 81 | +- stage: DeployToDev |
| 82 | + displayName: Deploy to Dev |
| 83 | + dependsOn: Build |
| 84 | + condition: and(succeeded(), startsWith(variables['Build.SourceBranch'], 'refs/heads/')) |
| 85 | + jobs: |
| 86 | + - deployment: Deploy |
| 87 | + displayName: Deploy job |
| 88 | + pool: |
| 89 | + vmImage: 'ubuntu-latest' |
| 90 | + |
| 91 | + environment: 'BikeSharingSampleApp-Dev' |
| 92 | + strategy: |
| 93 | + runOnce: |
| 94 | + deploy: |
| 95 | + steps: |
| 96 | + - task: DownloadPipelineArtifact@1 |
| 97 | + inputs: |
| 98 | + artifactName: 'charts' |
| 99 | + downloadPath: '$(System.ArtifactsDirectory)/charts' |
| 100 | + |
| 101 | + - task: KubernetesManifest@0 |
| 102 | + displayName: Create imagePullSecret |
| 103 | + inputs: |
| 104 | + kubernetesServiceConnection: $(k8sServiceConnection) |
| 105 | + action: createSecret |
| 106 | + secretName: $(imagePullSecret) |
| 107 | + namespace: $(k8sNamespace) |
| 108 | + dockerRegistryEndpoint: $(dockerRegistryServiceConnection) |
| 109 | + |
| 110 | + - task: KubernetesManifest@0 |
| 111 | + displayName: bake |
| 112 | + name: bake |
| 113 | + inputs: |
| 114 | + action: bake |
| 115 | + helmChart: '$(System.ArtifactsDirectory)/charts' |
| 116 | + releaseName: bikesharing |
| 117 | + overrides: | |
| 118 | + image.tag:$(Build.BuildId) |
| 119 | + buildID:"1" |
| 120 | + |
| 121 | + - task: KubernetesManifest@0 |
| 122 | + displayName: deploy |
| 123 | + name: deploy |
| 124 | + inputs: |
| 125 | + kubernetesServiceConnection: $(k8sServiceConnection) |
| 126 | + action: deploy |
| 127 | + namespace: $(k8sNamespace) |
| 128 | + manifests: $(bake.manifestsBundle) |
| 129 | + imagePullSecrets: $(imagePullSecret) |
| 130 | + |
| 131 | +# Deploy all PR changes |
| 132 | +- stage: DeployPR |
| 133 | + displayName: Deploy PR review app |
| 134 | + dependsOn: Build |
| 135 | + condition: and(succeeded(), startsWith(variables['Build.SourceBranch'], 'refs/pull/')) |
| 136 | + jobs: |
| 137 | + - deployment: Deploy |
| 138 | + displayName: Deploy job |
| 139 | + pool: |
| 140 | + vmImage: 'ubuntu-latest' |
| 141 | + |
| 142 | + environment: 'BikeSharing-PR' |
| 143 | + strategy: |
| 144 | + runOnce: |
| 145 | + deploy: |
| 146 | + steps: |
| 147 | + |
| 148 | + - task: DownloadPipelineArtifact@1 |
| 149 | + inputs: |
| 150 | + artifactName: 'charts' |
| 151 | + downloadPath: '$(System.ArtifactsDirectory)/charts' |
| 152 | + |
| 153 | + - task: Kubernetes@1 |
| 154 | + displayName: 'Create namespace' |
| 155 | + inputs: |
| 156 | + kubernetesServiceEndpoint: $(k8sServiceConnection) |
| 157 | + command: create |
| 158 | + arguments: 'ns $(system.pullRequest.sourceBranch)' |
| 159 | + checkLatest: true |
| 160 | + |
| 161 | + - task: Kubernetes@1 |
| 162 | + displayName: 'Add devspace label' |
| 163 | + inputs: |
| 164 | + kubernetesServiceEndpoint: $(k8sServiceConnection) |
| 165 | + command: label |
| 166 | + arguments: '--overwrite ns $(system.pullRequest.sourceBranch) azds.io/space=true' |
| 167 | + checkLatest: true |
| 168 | + |
| 169 | + - task: Kubernetes@1 |
| 170 | + displayName: 'Setup root devspace to dev' |
| 171 | + inputs: |
| 172 | + kubernetesServiceEndpoint: $(k8sServiceConnection) |
| 173 | + command: label |
| 174 | + arguments: '--overwrite ns $(system.pullRequest.sourceBranch) azds.io/parent-space=$(k8sNamespace)' |
| 175 | + checkLatest: true |
| 176 | + |
| 177 | + - task: KubernetesManifest@0 |
| 178 | + displayName: Create imagePullSecret |
| 179 | + inputs: |
| 180 | + kubernetesServiceConnection: $(k8sServiceConnection) |
| 181 | + action: createSecret |
| 182 | + secretName: $(imagePullSecret) |
| 183 | + namespace: $(k8sNamespaceforpr) |
| 184 | + dockerRegistryEndpoint: $(dockerRegistryServiceConnection) |
| 185 | + |
| 186 | + - task: KubernetesManifest@0 |
| 187 | + displayName: bake |
| 188 | + name: bake |
| 189 | + inputs: |
| 190 | + action: bake |
| 191 | + helmChart: '$(System.ArtifactsDirectory)/charts' |
| 192 | + releaseName: bikesharing |
| 193 | + overrides: | |
| 194 | + image.repository:$(containerRegistry)/$(imageRepository) |
| 195 | + image.tag:$(Build.BuildId) |
| 196 | + buildID:"1" |
| 197 | +
|
| 198 | + - task: KubernetesManifest@0 |
| 199 | + displayName: deploy |
| 200 | + name: deploy |
| 201 | + inputs: |
| 202 | + kubernetesServiceConnection: $(k8sServiceConnection) |
| 203 | + action: deploy |
| 204 | + namespace: $(k8sNamespaceforpr) |
| 205 | + manifests: $(bake.manifestsBundle) |
| 206 | + imagePullSecrets: $(imagePullSecret) |
| 207 | + |
| 208 | + - task: AzureCLI@1 |
| 209 | + displayName: 'List all review app URLs' |
| 210 | + inputs: |
| 211 | + azureSubscription: $(azureServiceConnection) |
| 212 | + scriptLocation: inlineScript |
| 213 | + inlineScript: | |
| 214 | + az aks use-dev-spaces -n $(AZDSControllerName) -g $(AZDSControllerName) -y -s $(k8sNamespace)/$(system.pullRequest.sourceBranch) --update |
| 215 | + azds up -d |
| 216 | + azds list-uris -o json |
| 217 | + azds list-uris -o json > $(AGENT.TEMPDIRECTORY)/azdsuri.txt |
| 218 | +
|
| 219 | + - powershell: | |
| 220 | + $uriret = (Get-Content $(AGENT.TEMPDIRECTORY)/azdsuri.txt | Out-String | ConvertFrom-Json)[0].uri |
| 221 | + Write-Host "##vso[task.setvariable variable=RETURI]$uriret" |
| 222 | + Write-Host "$uriret" |
| 223 | + displayName: 'Get PR review app url for the current PR' |
| 224 | + |
| 225 | + - task: jikuma.devops-github-extension.custom-build-release-task.azuredevopsgithub@0 |
| 226 | + displayName: 'Write the PR review app details to GitHub' |
| 227 | + name: UpdatePR |
| 228 | + inputs: |
| 229 | + gitHubConnection: $(githubServiceConnection) |
| 230 | + body: | |
| 231 | + { |
| 232 | + "state": "success", |
| 233 | + "target_url": "$(RETURI)", |
| 234 | + "description": "available", |
| 235 | + "context": "Dev Spaces review app" |
| 236 | + } |
| 237 | + githubrestapiurl: 'https://api.github.com/repos/$(BUILD.REPOSITORY.ID)/statuses/$(system.pullRequest.sourceCommitId)' |
0 commit comments