-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_webhook.py
64 lines (56 loc) · 1.99 KB
/
run_webhook.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
63
64
import datetime
import json
import os
import sys
import time
import urllib.error
import urllib.request
def do_run_webhook(payload):
req = urllib.request.Request(os.getenv("WEBHOOK_URL"))
req.add_header("Content-Type", "application/json; charset=utf-8")
json_payload = json.dumps(payload).encode("utf-8")
req.add_header("Content-Length", len(json_payload))
response = urllib.request.urlopen(req, json_payload)
if response.code >= 300:
print("Unexpected HTTP {}/{} response".format(response.code, response.msg))
print(response.read().decode("UTF-8"))
return 1
def run_webhook():
latest = os.getenv("DOCKER_TAG_LATEST", "") == "true"
payload = {
"push_data": {
"pushed_at": int(datetime.datetime.now().timestamp()),
"images": [os.getenv("IMAGE_NAME")],
"tag": "latest" if latest else os.getenv("DOCKER_TAG"),
"pusher": "docker-publish-action",
},
"repository": {"repo_name": os.getenv("IMAGE_NAME")},
}
print("Calling webhook at {}".format(os.getenv("WEBHOOK_URL")))
print("---\n{}\n---".format(json.dumps(payload, indent=4)))
# webhook receiver might be fragile or have difficulties handling
# concurrent requests.
# ex: 2 close-apart requests for different apps in same sloppy project raises 409
attempts = 0
max_attempts = 3
while attempts < max_attempts + 1:
attempts += 1
try:
return do_run_webhook(payload)
except urllib.error.HTTPError as exc:
print(
"Unexpected Error on Attempt {}/{}: {}. ".format(
attempts, max_attempts, exc
)
)
time.sleep(attempts * 30)
continue
print("Exhausted retry attempts")
return 1
if __name__ == "__main__":
if not os.getenv("WEBHOOK_URL"):
sys.exit(0)
if not os.getenv("DOCKER_TAG"):
print("no tag pushed, skipping.")
sys.exit(0)
sys.exit(run_webhook())