-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
taylorbot-postgres.yml
133 lines (113 loc) · 4.51 KB
/
taylorbot-postgres.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
trigger:
branches:
include:
- master
paths:
include:
- src/taylorbot-postgres
pr:
branches:
include:
- master
paths:
include:
- src/taylorbot-postgres
resources:
- repo: self
variables:
artifactName: taylorbot-sqitch
archiveName: taylorbot-sqitch-bundle.tgz
stages:
- stage: build
displayName: 'Bundle sqitch project'
jobs:
- job: build
displayName: 'Bundle, test and archive sqitch project'
pool:
vmImage: 'ubuntu-latest'
variables:
sqitchSourceFolderPath: '$(Build.SourcesDirectory)/src/taylorbot-postgres/sqitch'
sqitchBundleOutputFolderName: taylorbot-sqitch-bundle
bundleArchiveFilePath: '$(Build.ArtifactStagingDirectory)/$(archiveName)'
steps:
- script: |
# Necessary so files can be created in the mount
user=${USER-$(whoami)}
docker container run \
--user $(id -u ${user}):$(id -g ${user}) \
--rm \
--mount type=bind,source=$(sqitchSourceFolderPath),target=/repo \
sqitch/sqitch:latest \
bundle --dest-dir $(sqitchBundleOutputFolderName)
displayName: 'Bundle sqitch project'
- script: |
set -o errexit
set -o pipefail
set -o nounset
network_name=taylorbot-sqitch-test-network
container_name=taylorbot-sqitch-test-postgres
docker network create ${network_name}
docker container run \
--detach \
--name ${container_name} \
--network ${network_name} \
--env POSTGRES_PASSWORD=toor \
postgres:12
sleep 10s
db_name=sqitch-test-database
role_name=taylorswift
role_password=welcometonewyork1989
docker exec --interactive ${container_name} \
psql --username=postgres --command="CREATE ROLE ${role_name} WITH LOGIN PASSWORD '${role_password}';"
docker exec --interactive ${container_name} \
createdb --username=postgres --owner=${role_name} ${db_name}
docker exec --interactive ${container_name} \
psql --username=postgres --dbname=${db_name} --command="CREATE EXTENSION IF NOT EXISTS pgcrypto WITH SCHEMA public;"
docker container run \
--rm \
--network ${network_name} \
--mount type=bind,source=$(sqitchSourceFolderPath)/$(sqitchBundleOutputFolderName),dst=/repo \
sqitch/sqitch:latest \
deploy db:pg://${role_name}:${role_password}@${container_name}/${db_name}
docker container run \
--rm \
--network ${network_name} \
--mount type=bind,source=$(sqitchSourceFolderPath)/$(sqitchBundleOutputFolderName),dst=/repo \
sqitch/sqitch:latest \
revert -y db:pg://${role_name}:${role_password}@${container_name}/${db_name}
displayName: 'Test sqitch bundle on new database'
- script: tar -czf $(bundleArchiveFilePath) --directory $(sqitchSourceFolderPath)/$(sqitchBundleOutputFolderName) .
displayName: 'Create bundle archive'
- publish: $(bundleArchiveFilePath)
artifact: $(artifactName)
displayName: 'Publish sqitch bundle archive'
- stage: deploy
displayName: 'TaylorBot'
dependsOn: build
condition: and(succeeded(), and(eq(variables['Build.Reason'], 'Manual'), eq(variables['Build.SourceBranch'], 'refs/heads/master')))
jobs:
- deployment: deploy_database
displayName: 'Deploy to remote database'
environment: taylorbot
strategy:
runOnce:
deploy:
steps:
# This assumes we have set up a remote postgres cluster with a "taylorbot" role who all privileges on a "taylorbot" database
- script: |
set -o errexit
set -o pipefail
set -o nounset
sqitch_bundle_path=$(Pipeline.Workspace)/taylorbot-sqitch-bundle
mkdir ${sqitch_bundle_path}
tar -xvf $(Pipeline.Workspace)/$(artifactName)/$(archiveName) --directory ${sqitch_bundle_path} --strip-components=1
docker container run \
--rm \
--mount type=bind,source=${sqitch_bundle_path},dst=/repo \
sqitch/sqitch:latest \
deploy db:pg://taylorbot:${ROLE_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/taylorbot
displayName: 'Deploy sqitch bundle to remote database'
env:
POSTGRES_HOST: $(taylorbot_postgres_host)
POSTGRES_PORT: $(taylorbot_postgres_port)
ROLE_PASSWORD: $(taylorbot_postgres_password)