1
+ # Build and push image to Azure Container Registry; Deploy to Azure Kubernetes Service
2
+ # https://docs.microsoft.com/azure/devops/pipelines/languages/docker
3
+
4
+ parameters :
5
+ - name : ' executeBuild'
6
+ displayName : ' Launch maven and docker build'
7
+ type : boolean
8
+ default : true
9
+
10
+ trigger :
11
+ branches :
12
+ include :
13
+ - release-*
14
+ - main
15
+ paths :
16
+ include :
17
+ - src/*
18
+ - helm/*
19
+ - pom.xml
20
+ - Dockerfile
21
+
22
+ pr : none
23
+
24
+ resources :
25
+ - repo : self
26
+
27
+ variables :
28
+
29
+ # vmImageNameDefault: 'ubuntu-latest'
30
+ vmImageNameDefault : ubuntu-22.04
31
+
32
+ imageRepository : ' $(K8S_IMAGE_REPOSITORY_NAME)'
33
+ deployNamespace : ' $(DEPLOY_NAMESPACE)'
34
+ helmReleaseName : ' $(HELM_RELEASE_NAME)'
35
+ settingsXmlROsecureFileName : ' $(SETTINGS_XML_RO_SECURE_FILE_NAME)'
36
+ settingsXmlSecureFileName : ' $(SETTINGS_XML_RO_SECURE_FILE_NAME)'
37
+ canDeploy : true
38
+
39
+ # If the branch is develop or a feature branch starting with CEN, deploy in DEV environment
40
+ ${{ if startsWith(variables['Build.SourceBranch'], 'refs/heads/release-dev') }} :
41
+ environment : ' DEV'
42
+ dockerRegistryServiceConnection : ' $(DEV_CONTAINER_REGISTRY_SERVICE_CONN)'
43
+ kubernetesServiceConnection : ' $(DEV_KUBERNETES_SERVICE_CONN)'
44
+ containerRegistry : ' $(DEV_CONTAINER_REGISTRY_NAME)'
45
+ selfHostedAgentPool : $(DEV_AGENT_POOL)
46
+
47
+ ${{ elseif startsWith(variables['Build.SourceBranch'], 'refs/heads/release-uat') }} :
48
+ environment : ' UAT'
49
+ dockerRegistryServiceConnection : ' $(UAT_CONTAINER_REGISTRY_SERVICE_CONN)'
50
+ kubernetesServiceConnection : ' $(UAT_KUBERNETES_SERVICE_CONN)'
51
+ containerRegistry : ' $(UAT_CONTAINER_REGISTRY_NAME)'
52
+ selfHostedAgentPool : $(UAT_AGENT_POOL)
53
+
54
+ ${{ elseif or(eq(variables['Build.SourceBranch'], 'refs/heads/main'),eq(variables['Build.SourceBranch'], 'refs/heads/release-prod')) }} :
55
+ environment : ' PROD'
56
+ dockerRegistryServiceConnection : ' $(PROD_CONTAINER_REGISTRY_SERVICE_CONN)'
57
+ kubernetesServiceConnection : ' $(PROD_KUBERNETES_SERVICE_CONN)'
58
+ containerRegistry : ' $(PROD_CONTAINER_REGISTRY_NAME)'
59
+ selfHostedAgentPool : $(PROD_AGENT_POOL)
60
+
61
+ ${{ else }} :
62
+ environment : ' DEV'
63
+ dockerRegistryServiceConnection : ' $(DEV_CONTAINER_REGISTRY_SERVICE_CONN)'
64
+ kubernetesServiceConnection : ' $(DEV_KUBERNETES_SERVICE_CONN)'
65
+ containerRegistry : ' $(DEV_CONTAINER_REGISTRY_NAME)'
66
+ selfHostedAgentPool : $(DEV_AGENT_POOL)
67
+
68
+ stages :
69
+ - stage : ' pom_version'
70
+ displayName : Release
71
+ condition : eq(variables.canDeploy, true)
72
+ jobs :
73
+ - job : POM
74
+ displayName : POM
75
+ pool :
76
+ vmImage : $(vmImageNameDefault)
77
+ steps :
78
+ - task : Bash@3
79
+ displayName : Get POM version
80
+ name : getpomversion
81
+ condition : and(succeeded(), eq(variables.canDeploy, true))
82
+ inputs :
83
+ targetType : ' inline'
84
+ script : |
85
+ version=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)
86
+ echo "##vso[task.setvariable variable=outputpomversion;isOutput=true]$version"
87
+ failOnStderr : true
88
+
89
+ - stage : ' build'
90
+ displayName : ' Build_and_Publish_to_${{ variables.environment }}'
91
+ dependsOn : ' pom_version'
92
+ variables :
93
+ pomversion : $[ stageDependencies.Release.POM.outputs['getpomversion.outputpomversion'] ]
94
+ jobs :
95
+ - job : Build
96
+ displayName : Build
97
+ pool :
98
+ vmImage : $(vmImageNameDefault)
99
+ steps :
100
+ - task : Docker@2
101
+ condition : and(succeeded(), ${{ parameters.executeBuild }})
102
+ displayName : ' Publish_image_to_${{ variables.environment }}'
103
+ inputs :
104
+ containerRegistry : ' $(dockerRegistryServiceConnection)'
105
+ repository : ' $(imageRepository)'
106
+ command : ' buildAndPush'
107
+ tags : |
108
+ $(Build.BuildId)
109
+ latest
110
+ $(pomversion)
111
+ # - task: PublishPipelineArtifact@1
112
+ # displayName: 'Publish Artifact manifests'
113
+ # condition: and(succeeded(), eq(variables.canDeploy, true))
114
+ # inputs:
115
+ # targetPath: '$(Build.Repository.LocalPath)/manifests'
116
+ # artifact: 'manifests'
117
+ # publishLocation: 'pipeline'
118
+
119
+ - stage : ' publish_artifact_helm'
120
+ displayName : ' Publish_artifact_Helm'
121
+ dependsOn : ['build']
122
+ jobs :
123
+ - job : Publish_artifact_helm
124
+ displayName : Publish_artifact_helm
125
+ pool :
126
+ vmImage : $(vmImageNameDefault)
127
+ steps :
128
+ - task : PublishPipelineArtifact@1
129
+ displayName : ' Publish Artifact manifests'
130
+ condition : succeeded()
131
+ inputs :
132
+ targetPath : ' $(Build.Repository.LocalPath)/helm'
133
+ artifact : ' helm'
134
+ publishLocation : ' pipeline'
135
+
136
+ - stage : ' deploy'
137
+ displayName : ' Deploy to ${{ variables.environment }} K8S'
138
+ dependsOn : ['publish_artifact_helm']
139
+ condition : and(succeeded(), eq(variables.canDeploy, true))
140
+ variables :
141
+ pomversion : $[ stageDependencies.Release.POM.outputs['getpomversion.outputpomversion'] ]
142
+ jobs :
143
+ - deployment : ' Deploy_to_${{ variables.environment }}'
144
+ displayName : ' Deploy to ${{ variables.environment }} K8S'
145
+ pool :
146
+ name : $(selfHostedAgentPool)
147
+ environment : ' $(environment)'
148
+ strategy :
149
+ runOnce :
150
+ deploy :
151
+ steps :
152
+ - download : none
153
+ - task : DownloadPipelineArtifact@2
154
+ inputs :
155
+ buildType : ' current'
156
+ artifactName : ' helm'
157
+ targetPath : ' $(Pipeline.Workspace)/helm'
158
+ - task : KubectlInstaller@0
159
+ - task : Bash@3
160
+ name : helm_dependency_build
161
+ displayName : Helm dependency build
162
+ inputs :
163
+ workingDirectory : ' $(Pipeline.Workspace)/helm'
164
+ targetType : ' inline'
165
+ script : |
166
+ helm repo add pagopa-microservice https://pagopa.github.io/aks-microservice-chart-blueprint
167
+ helm dep build
168
+ failOnStderr : true
169
+ - task : HelmDeploy@0
170
+ displayName : Helm upgrade
171
+ inputs :
172
+ kubernetesServiceEndpoint : ${{ variables.kubernetesServiceConnection }}
173
+ namespace : ' $(deployNamespace)'
174
+ command : upgrade
175
+ chartType : filepath
176
+ chartPath : $(Pipeline.Workspace)/helm
177
+ chartName : ${{ variables.helmReleaseName }}
178
+ releaseName : ${{ variables.helmReleaseName }}
179
+ valueFile : " $(Pipeline.Workspace)/helm/values-${{ lower(variables.environment) }}.yaml"
180
+ install : true
181
+ waitForExecution : true
182
+ arguments : " --timeout 5m0s --debug"
183
+ - task : KubernetesManifest@0
184
+ displayName : Patch
185
+ inputs :
186
+ kubernetesServiceConnection : ${{ variables.kubernetesServiceConnection }}
187
+ namespace : ' $(deployNamespace)'
188
+ action : patch
189
+ kind : deployment
190
+ name : ' $(helmReleaseName)-microservice-chart'
191
+ mergeStrategy : strategic
192
+ patch : ' {"spec":{"template":{"metadata":{"annotations":{"buildNumber":"$(Build.BuildNumber)"}}}}}'
0 commit comments