-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitbucket-pipelines.yml
271 lines (269 loc) · 16 KB
/
bitbucket-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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
image: sindriainc/image-builder:latest
definitions:
steps:
- step: &colors
name: Setting Colors
script:
- BLUE='\033[0;34m'
- YELLOW='\033[0;33m'
- RED='\033[0;31m'
- NC='\033[0m' #No Color
- NOW=$(date "+%Y-%m-%d_%H-%M-%S")
caches:
maven: src/target
ssh: ~/.ssh
pipelines:
# Staging
branches:
staging:
- step:
<<: *colors
name: Build Dependencies
image: sindriainc/openjdk:1.1.0-14
script:
- echo -e "${BLUE}Building dependencies...${NC}"
- cd src
- mvn compile
- mvn package
#- cowsay -f tux "Build Success"
caches:
- maven
artifacts:
- "src/target/tca-scoreboards-0.1.0.jar"
- step:
<<: *colors
name: Test App
image: sindriainc/openjdk:1.1.0-14
script:
- echo -e "${BLUE}Testing app...${NC}"
- cd src
- ls -la
#- cowsay -f tux "Test Success"
caches:
- maven
- step:
<<: *colors
name: Build Image
script:
- echo -e "${BLUE}Building image...${NC}"
- bash build.sh ${DOCKERHUB_NAMESPACE}/${BITBUCKET_REPO_SLUG} staging
- docker save ${DOCKERHUB_NAMESPACE}/${BITBUCKET_REPO_SLUG}:staging --output "${BITBUCKET_REPO_SLUG}.tar"
- cowsay -f tux "Build Success"
services:
- docker
caches:
- docker
artifacts:
- "*.tar"
- step:
<<: *colors
name: Push Image
script:
- echo -e "${BLUE}Loading cached image...${NC}"
- docker load --input "${BITBUCKET_REPO_SLUG}.tar"
- echo -e "${BLUE}Tagging cached image...${NC}"
- docker tag "${DOCKERHUB_NAMESPACE}/${BITBUCKET_REPO_SLUG}:staging" "${DOCKERHUB_NAMESPACE}/${BITBUCKET_REPO_SLUG}:staging"
- echo -e "${BLUE}Login into registry...${NC}"
- echo ${DOCKERHUB_PASSWORD} | docker login --username "${DOCKERHUB_USERNAME}" --password-stdin
- echo -e "${BLUE}Pushing image into registry...${NC}"
- docker push ${DOCKERHUB_NAMESPACE}/${BITBUCKET_REPO_SLUG}:staging
- echo -e "${BLUE}Cleaning...${NC}"
- rm -rf *.tar
- echo -e "${BLUE}Cleaning local registry...${NC}"
- docker image rm ${DOCKERHUB_NAMESPACE}/${BITBUCKET_REPO_SLUG}:staging
- cowsay -f dragon "Well Done! New docker image is now on your registry."
services:
- docker
- step:
<<: *colors
name: Init Deploy
image: sindriainc/deployer:latest
script:
- SERVER=${STAGING_SERVER}
- rm -f .env
- rm -f docker-compose.yml
- cp .env.staging .env
- cp docker-compose.staging.yml docker-compose.yml
- (umask 077; echo ${SINDRIA_CI_KEY} | base64 -d > ~/.ssh/sindria@ci)
- sed -e 's/#fqdn/${SERVER}/g' ~/.ssh/config
- rm -f ~/.ssh/known_hosts
- ssh-keyscan -t rsa ${SERVER} > my_known_hosts
- cat my_known_hosts > ~/.ssh/known_hosts
- chmod 700 ~/.ssh
- chmod 600 ~/.ssh/*
- cowsay -f tux "Init Success"
caches:
- ssh
artifacts:
- ".ssh/**"
- ".env"
- "docker-compose.yml"
- step:
<<: *colors
name: Deploy App
image: sindriainc/deployer:latest
script:
- SERVER=${STAGING_SERVER}
- ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ~/.ssh/sindria@ci ${SINDRIA_CI_USER}@${SERVER} "mkdir -p ${BASE_PATH}/${BITBUCKET_REPO_SLUG}"
- ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ~/.ssh/sindria@ci ${SINDRIA_CI_USER}@${SERVER} "mkdir -p ${BASE_PATH}/${BITBUCKET_REPO_SLUG}/storage"
- ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ~/.ssh/sindria@ci ${SINDRIA_CI_USER}@${SERVER} "mkdir -p ${BASE_PATH}/${BITBUCKET_REPO_SLUG}/storage/data"
- ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ~/.ssh/sindria@ci ${SINDRIA_CI_USER}@${SERVER} "mkdir -p ${BASE_PATH}/${BITBUCKET_REPO_SLUG}/storage/logs"
- ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ~/.ssh/sindria@ci ${SINDRIA_CI_USER}@${SERVER} "docker stop ${BITBUCKET_REPO_SLUG}" || echo -e "${YELLOW}No such container ${BITBUCKET_REPO_SLUG}${NC}"
- ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ~/.ssh/sindria@ci ${SINDRIA_CI_USER}@${SERVER} "docker rm ${BITBUCKET_REPO_SLUG}" || echo -e "${YELLOW}No such container ${BITBUCKET_REPO_SLUG}${NC}"
- ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ~/.ssh/sindria@ci ${SINDRIA_CI_USER}@${SERVER} "cd ${BASE_PATH}/${BITBUCKET_REPO_SLUG}; docker-compose down" || echo -e "${YELLOW}Deployment not found${NC}"
- rsync -ravP -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ~/.ssh/sindria@ci" --exclude='.git' --exclude='.gitignore' --exclude='my_known_hosts' --exclude='.git*' --exclude='.gitlab-ci.yml' --exclude='bitbucket-pipelines.yml' --exclude='old' --exclude=${BITBUCKET_REPO_SLUG}.tar --exclude='readme.md' --exclude='README.md' --exclude='build.sh' --exclude='storage' --exclude='bin' --exclude='src' --exclude='certs' --exclude='docker-compose.local.yml' --exclude='docker-compose.staging.yml' --exclude='docker-compose.production.yml' --exclude='.env.local' --exclude='.env.staging' --exclude='.env.production' --delete ${BITBUCKET_CLONE_DIR}/ ${SINDRIA_CI_USER}@${SERVER}:/${BASE_PATH}/${BITBUCKET_REPO_SLUG}/
- ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ~/.ssh/sindria@ci ${SINDRIA_CI_USER}@${SERVER} "sudo chmod 770 ${BASE_PATH}/${BITBUCKET_REPO_SLUG}"
- ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ~/.ssh/sindria@ci ${SINDRIA_CI_USER}@${SERVER} "docker image rm ${DOCKERHUB_NAMESPACE}/${BITBUCKET_REPO_SLUG}:staging" || echo -e "${YELLOW}Image not present into this local registry${NC}"
- ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ~/.ssh/sindria@ci ${SINDRIA_CI_USER}@${SERVER} "cd ${BASE_PATH}/${BITBUCKET_REPO_SLUG}; docker-compose up -d"
- cowsay "Deploy Success"
caches:
- ssh
- step:
<<: *colors
name: Logs App
image: sindriainc/deployer:latest
trigger: manual
script:
- SERVER=${STAGING_SERVER}
- ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ~/.ssh/sindria@ci ${SINDRIA_CI_USER}@${SERVER} "cd ${BASE_PATH}/${BITBUCKET_REPO_SLUG}; docker-compose logs -f"
caches:
- ssh
# Production
master:
- step: *colors
- step:
<<: *colors
name: Rollback App
image: sindriainc/deployer:latest
trigger: manual
script:
- SERVER=${PRODUCTION_SERVER}
- ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ~/.ssh/sindria@ci ${SINDRIA_CI_USER}@${SERVER} "docker stop ${BITBUCKET_REPO_SLUG}" || echo -e "${YELLOW}Container ${BITBUCKET_REPO_SLUG} Exited (255)${NC}"
- ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ~/.ssh/sindria@ci ${SINDRIA_CI_USER}@${SERVER} "docker rm ${BITBUCKET_REPO_SLUG}" || echo -e "${YELLOW}Container ${BITBUCKET_REPO_SLUG} Exited (255)${NC}"
- ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ~/.ssh/sindria@ci ${SINDRIA_CI_USER}@${SERVER} "docker image rm ${DOCKERHUB_NAMESPACE}/${BITBUCKET_REPO_SLUG}:latest" || echo -e "${YELLOW}Image not present into this local registry${NC}"
- ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ~/.ssh/sindria@ci ${SINDRIA_CI_USER}@${SERVER} "docker tag ${DOCKERHUB_NAMESPACE}/${BITBUCKET_REPO_SLUG}:rollback ${DOCKERHUB_NAMESPACE}/${BITBUCKET_REPO_SLUG}:latest" || echo -e "${YELLOW}Image not present into this local registry${NC}"
- ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ~/.ssh/sindria@ci ${SINDRIA_CI_USER}@${SERVER} "docker image rm ${DOCKERHUB_NAMESPACE}/${BITBUCKET_REPO_SLUG}:rollback" || echo -e "${YELLOW}Image not present into this local registry${NC}"
- ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ~/.ssh/sindria@ci ${SINDRIA_CI_USER}@${SERVER} "cd ${BASE_PATH}/${BITBUCKET_REPO_SLUG}; docker-compose up -d"
- cowsay "Rollback Success"
caches:
- ssh
# Production
tags:
"*":
- step:
<<: *colors
name: Build Dependencies
image: sindriainc/openjdk:1.1.0-14
script:
- echo -e "${BLUE}Building dependencies...${NC}"
- cd src
- mvn compile
- mvn package
#- cowsay -f tux "Build Success"
caches:
- maven
artifacts:
- "src/target/tca-scoreboards-0.1.0.jar"
- step:
<<: *colors
name: Test App
image: sindriainc/openjdk:1.1.0-14
script:
- echo -e "${BLUE}Testing app...${NC}"
- cd src
- ls -la
#- cowsay -f tux "Test Success"
caches:
- maven
- step:
<<: *colors
name: Build Image
script:
- echo -e "${BLUE}Building image...${NC}"
- bash build.sh ${DOCKERHUB_NAMESPACE}/${BITBUCKET_REPO_SLUG} ${BITBUCKET_TAG}
- docker save ${DOCKERHUB_NAMESPACE}/${BITBUCKET_REPO_SLUG}:${BITBUCKET_TAG} --output "${BITBUCKET_REPO_SLUG}.tar"
- cowsay -f tux "Build Success"
services:
- docker
caches:
- docker
artifacts:
- "*.tar"
- step:
<<: *colors
name: Push Image
script:
- echo -e "${BLUE}Loading cached image...${NC}"
- docker load --input "${BITBUCKET_REPO_SLUG}.tar"
- echo -e "${BLUE}Tagging cached image...${NC}"
- docker tag "${DOCKERHUB_NAMESPACE}/${BITBUCKET_REPO_SLUG}:${BITBUCKET_TAG}" "${DOCKERHUB_NAMESPACE}/${BITBUCKET_REPO_SLUG}:${BITBUCKET_TAG}"
- docker tag "${DOCKERHUB_NAMESPACE}/${BITBUCKET_REPO_SLUG}:${BITBUCKET_TAG}" "${DOCKERHUB_NAMESPACE}/${BITBUCKET_REPO_SLUG}:latest"
- echo -e "${BLUE}Login into registry...${NC}"
- echo ${DOCKERHUB_PASSWORD} | docker login --username "${DOCKERHUB_USERNAME}" --password-stdin
- echo -e "${BLUE}Pushing image into registry...${NC}"
- docker push ${DOCKERHUB_NAMESPACE}/${BITBUCKET_REPO_SLUG}:${BITBUCKET_TAG}
- docker push ${DOCKERHUB_NAMESPACE}/${BITBUCKET_REPO_SLUG}:latest
- echo -e "${BLUE}Cleaning...${NC}"
- rm -rf *.tar
- echo -e "${BLUE}Cleaning local registry...${NC}"
- docker image rm ${DOCKERHUB_NAMESPACE}/${BITBUCKET_REPO_SLUG}:${BITBUCKET_TAG}
- docker image rm ${DOCKERHUB_NAMESPACE}/${BITBUCKET_REPO_SLUG}:latest
- cowsay -f dragon "Well Done! New docker image is now on your registry."
services:
- docker
- step:
<<: *colors
name: Init Deploy
image: sindriainc/deployer:latest
script:
- SERVER=${PRODUCTION_SERVER}
- rm -f .env
- rm -f docker-compose.yml
- cp .env.production .env
- cp docker-compose.production.yml docker-compose.yml
- (umask 077; echo ${SINDRIA_CI_KEY} | base64 -d > ~/.ssh/sindria@ci)
- sed -e 's/#fqdn/${SERVER}/g' ~/.ssh/config
- rm -f ~/.ssh/known_hosts
- ssh-keyscan -t rsa ${SERVER} > my_known_hosts
- cat my_known_hosts > ~/.ssh/known_hosts
- chmod 700 ~/.ssh
- chmod 600 ~/.ssh/*
- cowsay -f tux "Init Success"
caches:
- ssh
artifacts:
- ".ssh/**"
- ".env"
- "docker-compose.yml"
- step:
<<: *colors
name: Deploy App
image: sindriainc/deployer:latest
script:
- SERVER=${PRODUCTION_SERVER}
- ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ~/.ssh/sindria@ci ${SINDRIA_CI_USER}@${SERVER} "mkdir -p ${BASE_PATH}/${BITBUCKET_REPO_SLUG}"
- ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ~/.ssh/sindria@ci ${SINDRIA_CI_USER}@${SERVER} "mkdir -p ${BASE_PATH}/${BITBUCKET_REPO_SLUG}/storage"
- ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ~/.ssh/sindria@ci ${SINDRIA_CI_USER}@${SERVER} "mkdir -p ${BASE_PATH}/${BITBUCKET_REPO_SLUG}/storage/data"
- ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ~/.ssh/sindria@ci ${SINDRIA_CI_USER}@${SERVER} "mkdir -p ${BASE_PATH}/${BITBUCKET_REPO_SLUG}/storage/logs"
- ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ~/.ssh/sindria@ci ${SINDRIA_CI_USER}@${SERVER} "docker stop ${BITBUCKET_REPO_SLUG}" || echo -e "${YELLOW}No such container ${BITBUCKET_REPO_SLUG}${NC}"
- ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ~/.ssh/sindria@ci ${SINDRIA_CI_USER}@${SERVER} "docker rm ${BITBUCKET_REPO_SLUG}" || echo -e "${YELLOW}No such container ${BITBUCKET_REPO_SLUG}${NC}"
- ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ~/.ssh/sindria@ci ${SINDRIA_CI_USER}@${SERVER} "cd ${BASE_PATH}/${BITBUCKET_REPO_SLUG}; docker-compose down" || echo -e "${YELLOW}Deployment not found${NC}"
- rsync -ravP -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ~/.ssh/sindria@ci" --exclude='.git' --exclude='.gitignore' --exclude='my_known_hosts' --exclude='.git*' --exclude='.gitlab-ci.yml' --exclude='bitbucket-pipelines.yml' --exclude='old' --exclude=${BITBUCKET_REPO_SLUG}.tar --exclude='readme.md' --exclude='README.md' --exclude='build.sh' --exclude='storage' --exclude='bin' --exclude='src' --exclude='certs' --exclude='docker-compose.local.yml' --exclude='docker-compose.staging.yml' --exclude='docker-compose.production.yml' --exclude='.env.local' --exclude='.env.staging' --exclude='.env.production' --delete ${BITBUCKET_CLONE_DIR}/ ${SINDRIA_CI_USER}@${SERVER}:/${BASE_PATH}/${BITBUCKET_REPO_SLUG}/
- ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ~/.ssh/sindria@ci ${SINDRIA_CI_USER}@${SERVER} "sudo chmod 770 ${BASE_PATH}/${BITBUCKET_REPO_SLUG}"
- ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ~/.ssh/sindria@ci ${SINDRIA_CI_USER}@${SERVER} "docker tag ${DOCKERHUB_NAMESPACE}/${BITBUCKET_REPO_SLUG}:latest ${DOCKERHUB_NAMESPACE}/${BITBUCKET_REPO_SLUG}:rollback" || echo -e "${YELLOW}Image not present into this local registry${NC}"
- ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ~/.ssh/sindria@ci ${SINDRIA_CI_USER}@${SERVER} "docker image rm ${DOCKERHUB_NAMESPACE}/${BITBUCKET_REPO_SLUG}:latest" || echo -e "${YELLOW}Image not present into this local registry${NC}"
- ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ~/.ssh/sindria@ci ${SINDRIA_CI_USER}@${SERVER} "docker system prune -f"
- ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ~/.ssh/sindria@ci ${SINDRIA_CI_USER}@${SERVER} "cd ${BASE_PATH}/${BITBUCKET_REPO_SLUG}; docker-compose up -d"
- cowsay "Deploy Success"
caches:
- ssh
- step:
<<: *colors
name: Logs App
image: sindriainc/deployer:latest
trigger: manual
script:
- SERVER=${PRODUCTION_SERVER}
- ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i ~/.ssh/sindria@ci ${SINDRIA_CI_USER}@${SERVER} "cd ${BASE_PATH}/${BITBUCKET_REPO_SLUG}; docker-compose logs -f"
caches:
- ssh