-
Notifications
You must be signed in to change notification settings - Fork 35
/
azure-pipelines.yml
239 lines (232 loc) · 8.89 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
232
233
234
235
236
237
238
239
# Azure pipeline to build & deploy GoAdmin demo site
# https://docs.microsoft.com/azure/devops/pipelines/
trigger:
batch: true
branches:
include:
- '*'
pool:
vmImage: ubuntu-latest
variables:
- name: goVersion
value: '1.17'
- name: gogiter8Version
value: '0.5.1'
- name: dockerVersion
value: '19.03.12'
- name: GOBIN
value: $(GOROOT)/bin
- name: GO111MODULE
value: 'on'
- name: goBuiltAppName
value: goadmin
- name: sessionDirectory
value: $(System.DefaultWorkingDirectory)/..
- name: projectName
value: goadmincp-seed
- name: projectDirectory
value: $(sessionDirectory)/$(projectName)
- name: branchMasterChanged
value: ${{ eq(variables['Build.SourceBranch'], 'refs/heads/master') }}
- name: branchDemoBuildChanged
value: ${{ eq(variables['Build.SourceBranch'], 'refs/heads/demo-build') }}
- name: dockerHubRepoName
value: goadmin-demo
- name: goadminVersion
value: r3
- group: Release # merge with ADO env group named 'Release'
stages:
- stage: generate_and_build
displayName: Generate and Build project
jobs:
- job: generate_and_build
displayName: Generate project and build
steps:
- task: GoTool@0
displayName: Prepare Go env
inputs:
version: '$(goVersion)'
- task: Go@0
inputs:
# minimum go-giter8 v0.5.1 for "quiet" mode
command: 'get'
arguments: 'github.com/btnguyen2k/go-giter8/g8@v$(gogiter8Version)'
displayName: Install go-giter8
- script: |
g8 new --no-inputs file:https://$(System.DefaultWorkingDirectory)
displayName: Generate project using go-giter8
workingDirectory: $(sessionDirectory)
- script: cd $(projectDirectory) && go build -o $(goBuiltAppName) -tags netgo -a
displayName: Build project
- stage: test
displayName: Run tests
dependsOn: generate_and_build
jobs:
- job: test_sqlite
displayName: Run tests against SQLite
steps:
- task: GoTool@0
displayName: Prepare Go env
inputs:
version: '$(goVersion)'
- script: |
export PWD=$(pwd)
mkdir -p $PWD/temp
export SQLITE_DRIVER="sqlite3"
export SQLITE_URL="$PWD/temp/temp.db"
cd $(System.DefaultWorkingDirectory)/src/main/g8 \
&& go test -v -p 1 -count 1 ./src/myapp/
displayName: Run tests against SQLite
- job: test_pgsql
displayName: Run tests against PostgreSQL
steps:
- task: GoTool@0
displayName: Prepare Go env
inputs:
version: '$(goVersion)'
- script: |
docker run -d --rm --name postgres -e POSTGRES_DB=test -e POSTGRES_USER=test -e POSTGRES_PASSWORD=test -p 5432:5432 postgres:11
sleep 5
displayName: Start PostgreSQL server
- script: |
export TIMEZONE="Asia/Ho_Chi_Minh"
export PGSQL_DRIVER="pgx"
export PGSQL_URL="postgres:https://test:test@localhost:5432/test?sslmode=disable&client_encoding=UTF-8&application_name=goadmin"
cd $(System.DefaultWorkingDirectory)/src/main/g8 \
&& go test -v -p 1 -count 1 ./src/myapp/
displayName: Run tests against PostgreSQL
- job: test_mysql
displayName: Run tests against MySQL
steps:
- task: GoTool@0
displayName: Prepare Go env
inputs:
version: '$(goVersion)'
- script: |
docker run -d --rm --name mysql -e MYSQL_ROOT_PASSWORD=test -e MYSQL_DATABASE=test -e MYSQL_USER=test -e MYSQL_PASSWORD=test -p 3306:3306 mysql:8
sleep 5
displayName: Start MySQL server
- script: |
export TIMEZONE="Asia/Ho_Chi_Minh"
export MYSQL_DRIVER="mysql"
export MYSQL_URL="test:test@tcp(localhost:3306)/test?charset=utf8mb4,utf8&parseTime=true&loc=\${loc}"
cd $(System.DefaultWorkingDirectory)/src/main/g8 \
&& go test -v -p 1 -count 1 ./src/myapp/
displayName: Run tests against MySQL (parseTime=true)
- script: |
export TIMEZONE="Asia/Ho_Chi_Minh"
export MYSQL_DRIVER="mysql"
export MYSQL_URL="test:test@tcp(localhost:3306)/test?charset=utf8mb4,utf8&parseTime=false&loc=\${loc}"
cd $(System.DefaultWorkingDirectory)/src/main/g8 \
&& go test -v -p 1 -count 1 ./src/myapp/
displayName: Run tests against MySQL (parseTime=false)
- job: test_mongo_standlone
displayName: Run tests against MongoDB (standalone)
steps:
- task: GoTool@0
displayName: Prepare Go env
inputs:
version: '$(goVersion)'
- script: |
docker run -d --rm --name mongodb -e MONGO_INITDB_ROOT_USERNAME=test -e MONGO_INITDB_ROOT_PASSWORD=test -e MONGO_INITDB_DATABASE=test -p 27017:27017 mongo:4.2
sleep 5
displayName: Start MongoDB server (standalone)
- script: |
export TIMEZONE="Asia/Ho_Chi_Minh"
export MONGO_DB="test"
export MONGO_URL="mongodb:https://test:test@localhost:27017/?authSource=admin"
cd $(System.DefaultWorkingDirectory)/src/main/g8 \
&& go test -v -p 1 -count 1 ./src/myapp/
displayName: Run tests against MongoDB (standalone)
- job: test_mongo_replicaset
displayName: Run tests against MongoDB (replicaset)
steps:
- task: GoTool@0
displayName: Prepare Go env
inputs:
version: '$(goVersion)'
- script: |
docker run -d --rm --name mongodb-replset -e MONGODB_REPLICA_SET_MODE=primary -e MONGODB_REPLICA_SET_KEY=myreplset -e MONGODB_ADVERTISED_HOSTNAME=localhost -e MONGODB_PRIMARY_PORT_NUMBER=27017 -e MONGODB_ROOT_PASSWORD=test -e MONGODB_DATABASE=test -p 27017:27017 bitnami/mongodb:4.2
sleep 5
displayName: Start MongoDB server (replicaset)
- script: |
export TIMEZONE="Asia/Ho_Chi_Minh"
export MONGO_DB="test"
export MONGO_URL="mongodb:https://root:test@localhost:27017/?authSource=admin&replicaSet=replicaset"
cd $(System.DefaultWorkingDirectory)/src/main/g8 \
&& go test -v -p 1 -count 1 ./src/myapp/
displayName: Run tests against MongoDB (replicaset)
- stage: build_dockerhub
displayName: Build image and publish to Docker Hub
dependsOn: test
condition: eq(variables['branchDemoBuildChanged'], true)
jobs:
- job: generate_build_publish
displayName: Generate project, build Docker image and publish
steps:
- task: GoTool@0
displayName: Prepare Go env
inputs:
version: '$(goVersion)'
- script: |
APP_VERSION='$(goadminVersion)'
echo '##vso[task.setvariable variable=APP_VERSION]'$APP_VERSION
APP_VERSION_BUILD=${APP_VERSION}-b$(Build.BuildId)
echo '##vso[task.setvariable variable=APP_VERSION_BUILD]'$APP_VERSION_BUILD
sed -i 's/version\s*=.*$/version = ${APP_VERSION_BUILD}/' ./src/main/g8/default.properties
displayName: Init app name, version and build id
- task: Go@0
inputs:
# minimum go-giter8 v0.5.1 for "quiet" mode
command: 'get'
arguments: 'github.com/btnguyen2k/go-giter8/g8@v$(gogiter8Version)'
displayName: Install go-giter8
- script: |
g8 new --no-inputs file:https://$(System.DefaultWorkingDirectory)
echo swapping directories...
rm -rf $(System.DefaultWorkingDirectory)/*
cp -r $(projectDirectory)/* $(System.DefaultWorkingDirectory)/
displayName: Generate project using go-giter8
workingDirectory: $(sessionDirectory)
- task: Docker@2
displayName: Build and Publish Docker image
inputs:
command: buildAndPush
containerRegistry: '$(DockerRegistry)'
repository: '$(DockerRegistryAccount)/$(dockerHubRepoName)'
tags: |
$(APP_VERSION)
$(APP_VERSION_BUILD)
latest
- script: |
echo '##vso[task.setvariable variable=APP_VERSION_BUILD;isOutput=true]'$APP_VERSION_BUILD
displayName: Transfer variables to next stages
name: transfer_variables
- stage: deploy_demo
displayName: Deploy to demo site
dependsOn: build_dockerhub
variables:
APP_VERSION_BUILD: $[ stageDependencies.build_dockerhub.generate_build_publish.outputs['transfer_variables.APP_VERSION_BUILD'] ]
jobs:
- job: deploy_demo
displayName: Deploy to demo site
steps:
- script: |
echo '##vso[task.setvariable variable=dockerImageTag]$(APP_VERSION_BUILD)'
displayName: Set Docker image tag
- script: |
echo '##vso[task.setvariable variable=dockerImageTag]latest'
displayName: Set Docker image tag to latest if not specified
condition: eq(variables['APP_VERSION_BUILD'], '')
- task: AzureCLI@2
inputs:
azureSubscription: $(AzureSubscription)
scriptType: bash
scriptLocation: inlineScript
inlineScript: |
az config set extension.use_dynamic_install=yes_without_prompt
az containerapp update \
--name $(ContainerAppName) \
--resource-group $(ResourceGroupName) \
--container-name $(ContainerAppContainerName) \
--image docker.io/$(DockerRegistryAccount)/$(dockerHubRepoName):$(APP_VERSION_BUILD)