-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy_be.py
62 lines (52 loc) · 1.98 KB
/
deploy_be.py
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
import git
import os
import subprocess
import shutil
from logger import logger
from config import BACKEND_DEV_SERVICE, BACKEND_PROJECT_DIR, \
BACKEND_REPO_URL, BACKEND_TEST_SERVICE, DEV_BRANCH, \
TEST_BRANCH
def prepare(tag):
logger.debug("start clone")
repo = git.Repo.clone_from(
url=BACKEND_REPO_URL, to_path=BACKEND_PROJECT_DIR)
repo.git.checkout(tag)
logger.debug("pull over.")
shutil.rmtree(os.path.join(BACKEND_PROJECT_DIR, '.git'))
def build(target):
logger.debug("start build...")
os.chdir(BACKEND_PROJECT_DIR)
subprocess.call(
f"/home/alex/workspace/install/apache-maven-3.8.1/bin/mvn clean package", shell=True)
subprocess.call(
f'docker-compose stop {target} && docker-compose rm -f {target}', shell=True)
subprocess.call(
f'docker rmi solstreet-be_{target}', shell=True)
subprocess.call(
f'docker-compose -f {BACKEND_PROJECT_DIR}/docker-compose.yaml up --build -d {target}', shell=True)
def clean():
for filename in os.listdir(BACKEND_PROJECT_DIR):
file_path = os.path.join(BACKEND_PROJECT_DIR, filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
logger.error('Failed to delete %s. Reason: %s' % (file_path, e))
def deploy_be(tag, branch):
try:
clean()
prepare(tag)
if DEV_BRANCH in branch:
logger.debug('start deploy backend dev')
build(BACKEND_DEV_SERVICE)
elif TEST_BRANCH in branch:
logger.debug('start deploy backend test')
build(BACKEND_TEST_SERVICE)
else:
logger.debug(f'ignore branch ==> {branch}')
except Exception as e:
logger.exception(e)
if __name__ == '__main__':
deploy_be('v0.0.5', DEV_BRANCH)