-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathazure-pipeline-template.yml
156 lines (152 loc) · 5.34 KB
/
azure-pipeline-template.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
# Template parameters
parameters:
- name: stage
type: string
values:
- 'dev'
- 'prod'
default: 'dev'
# Variables used for all stages
variables:
imageRepository: 'accessmanager'
containerRegistry: '$(CONTAINER_REGISTRY)'
dockerfilePath: 'Dockerfile'
${{ if eq(parameters.stage, 'prod') }}:
tagVersion: '$(Build.BuildId)'
tagLatest: 'latest'
postfix: ''
${{ if eq(parameters.stage, 'dev') }}:
tagVersion: '$(Build.BuildId)-dev'
tagLatest: 'latest-dev'
postfix: '-dev'
stages:
# Test stage:
# Runs a Maven task that is equivalent to a "mvn test" command and collects the results in JaCoCo (how to access?).
- stage: Test
displayName: Test stage
jobs:
- job: Test
displayName: Test
steps:
- task: CmdLine@2
inputs:
script: |
mkdir -p ~/.m2
echo "$(settings.xml)" > "$HOME/.m2/settings.xml"
- task: MavenAuthenticate@0
inputs:
artifactsFeeds: sdk-snapshots, EFS-SDK
- task: Maven@3
inputs:
mavenPomFile: 'pom.xml'
goals: 'test'
publishJUnitResults: true
testResultsFiles: '**/surefire-reports/TEST-*.xml'
codeCoverageToolOption: 'JaCoCo'
javaHomeOption: 'JDKVersion'
jdkVersionOption: '1.17'
mavenVersionOption: 'Default'
mavenAuthenticateFeed: false
effectivePomSkip: false
sonarQubeRunAnalysis: false
# Build stage:
# 1. Runs a a Maven task that is equivalent to a "mvn build" command to build the java project into a jar artifact.
# 2. Runs a Docker task that builds the artifact into a docker image and pushes it to ACR.
# 3. Creates an artifact for the kubernetes folder holding the kubernetes yaml files.
- stage: Build
displayName: Build stage
dependsOn: Test
jobs:
- job: Build
displayName: Build
condition: and(succeeded(), not(startsWith(variables['Build.SourceBranch'], 'refs/pull/')))
steps:
- task: CmdLine@2
inputs:
script: |
mkdir -p ~/.m2
echo "$(settings.xml)" > "$HOME/.m2/settings.xml"
- task: MavenAuthenticate@0
inputs:
artifactsFeeds: sdk-snapshots, EFS-SDK
- task: Maven@3
displayName:
inputs:
mavenPomFile: 'pom.xml'
options: '-DskipTests'
publishJUnitResults: false
javaHomeOption: 'JDKVersion'
jdkVersionOption: '1.17'
mavenVersionOption: 'Default'
mavenAuthenticateFeed: false
effectivePomSkip: false
sonarQubeRunAnalysis: false
- task: CmdLine@2
inputs:
script: |
echo "dockerRegistryServiceConnection is $(dockerRegistryServiceConnection)"
echo "imageRepository is $(imageRepository)"
echo "dockerfilePath is $(dockerfilePath)"
- task: CmdLine@2
displayName: Download Application Insights
inputs:
script: 'curl -L -o applicationinsights-agent.jar https://github.com/microsoft/ApplicationInsights-Java/releases/download/3.4.14/applicationinsights-agent-3.4.14.jar'
- task: Docker@2
displayName: Build and push an image to container registry
inputs:
command: buildAndPush
repository: $(imageRepository)
dockerfile: $(dockerfilePath)
containerRegistry: $(dockerRegistryServiceConnection)
tags: |
$(tagVersion)
$(tagLatest)
- upload: kubernetes
artifact: kubernetes
- stage: Deploy
# Deploy stage:
# 1. Runs a KubernetesManifest task that creates a kubernetes secret that hold the credentials to connect to ACR
# 2. Runs a replacetokens task that replaces tokens in the kubernetes/*.yml files with pipeline environment variables
# 3. Runs a KubernetesManifest task that
# 1. deploys all the manifests in the kubernetes folder (= runs a "kubectl apply" for every one of them)
# 2. retrieves the previously built docker image from ACR and deploys it
displayName: Deploy stage
dependsOn: Build
jobs:
- deployment: Deploy
timeoutInMinutes: 2
condition: and(succeeded(), not(startsWith(variables['Build.SourceBranch'], 'refs/pull/')))
displayName: Deploy
environment: 'dev'
strategy:
runOnce:
deploy:
steps:
- task: CmdLine@2
inputs:
script: |
echo "kubernetesServiceConnection is $(kubernetesServiceConnection)"
- task: replacetokens@3
inputs:
rootDirectory: '$(Pipeline.Workspace)'
targetFiles: 'kubernetes/*.yml'
encoding: 'auto'
writeBOM: true
actionOnMissing: 'warn'
keepToken: false
tokenPrefix: '$('
tokenSuffix: ')'
- task: KubernetesManifest@0
displayName: Deploy to Kubernetes cluster
inputs:
action: 'deploy'
kubernetesServiceConnection: '$(kubernetesServiceConnection)'
namespace: 'backend'
manifests: |
$(Pipeline.Workspace)/kubernetes/config-map.yml
$(Pipeline.Workspace)/kubernetes/secret.yml
$(Pipeline.Workspace)/kubernetes/deployment.yml
$(Pipeline.Workspace)/kubernetes/ingress.yml
$(Pipeline.Workspace)/kubernetes/rbac.yml
$(Pipeline.Workspace)/kubernetes/service.yml
containers: '$(containerRegistry)/$(imageRepository):$(tagVersion)'