Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfabfile.py
More file actions
Latest commit
96 lines (74 loc) · 2.69 KB
/
Copy pathfabfile.py
File metadata and controls
96 lines (74 loc) · 2.69 KB
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
importjson
importre
fromdataclassesimportdataclass
fromosimportenvironasenv
importrequests
fromfabricimportConnection
fromfabricimporttask
REPO_NAME='schedule-API'
REPO_OWNER='thestd'
REPO_URL=f'https://github.com/{REPO_OWNER}/{REPO_NAME}'
APPS_ROOT='~/applications'
PROJECT_ROOT=f'{APPS_ROOT}/{REPO_NAME}'
@dataclass
classWebhookData:
action: str
is_prerelease: bool
is_draft: bool
tag: str
description: str
@staticmethod
defbuild(webhook_data):
returnWebhookData(webhook_data['action'],
webhook_data['release']['prerelease'],
webhook_data['release']['draft'],
webhook_data['release']['tag_name'],
webhook_data['release']['body'])
@task
defstaging(ctx):
ctx.user=env['SSH_USER']
ctx.host=env['SSH_HOST']
ctx.connect_kwargs.key_filename=env['SSH_KEY_FILENAME']
@task
defpost_release_webhook(ctx):
webhook_data=WebhookData.build(json.loads(env['POST_WEBHOOK_JSON']))
ifshould_to_deploy(webhook_data):
print('Run deploy process...')
deploy(ctx, webhook_data.tag)
else:
print('Skip deploy process...')
@task
defdeploy(ctx, tag):
withremote_connection(ctx) asc:
prepare(c)
withc.cd(PROJECT_ROOT):
deploy_process(c, tag)
defprepare(c):
c.run(f'mkdir -p {APPS_ROOT}')
withc.cd(APPS_ROOT):
c.run(f'if [ ! -d {PROJECT_ROOT} ] ; then git clone {REPO_URL}; fi')
defdeploy_process(c, tag):
c.run('git fetch --all --tags --prune --prune-tags --progress')
c.run(f'git checkout -f tags/{tag} -B release/{tag}')
c.run(' &&'.join((
'docker-compose up -d --build',
'docker ps --format="table {{.Names}}\t{{.Ports}}\t{{.Status}}"'
)))
defshould_to_deploy(webhook: WebhookData) ->bool:
latest_release_tag=get_latest_release_tag()
skip_flag=re.search(r'(<!--)(\s+)?(skip-deploy)(\s+)?(-->)',
webhook.description, re.IGNORECASE)
print('webhook = ', webhook,
'latest_release_tag = ', latest_release_tag,
'skip_flag = ', skip_flag)
return ((notwebhook.is_prereleaseandnotwebhook.is_draft)
andwebhook.actionin ('published', 'edited')
andlatest_release_tag==webhook.tag
andnotskip_flag)
defget_latest_release_tag():
data=requests.get((f'https://api.github.com/repos/'
f'{REPO_OWNER}/{REPO_NAME}/releases/latest')).json()
returndata.get('tag_name')
defremote_connection(ctx):
returnConnection(host=ctx.host, user=ctx.user,
connect_kwargs=ctx.connect_kwargs)