-
Notifications
You must be signed in to change notification settings - Fork 0
/
dockerHub.php
130 lines (108 loc) · 2.94 KB
/
dockerHub.php
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
<?php
/**
* Docker Hub Bot for Slack.
* Inspired by j0k3r
*
* You will need a webhook from Slack.
*
* @author MentheFraiche <[email protected]>
* @license MIT
*/
/**
* Let's configure
*/
// Slack stuff
const SECRET = '';
const SLACK_WEBHOOK = 'XXXXXXXXXXXXXXXXXXXXX';
const SLACK_CHANNEL = '#general';
const SLACK_BOT_NAME = 'Docker';
const SLACK_BOT_AVATAR = 'http://i.imgur.com/uo2vlQC.png';
/**
* Let's run
*/
function getUrl($url, $json)
{
$ch = curl_init($url);
$options = array(
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $json,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Content-Length: ' . strlen($json),
),
CURLOPT_TIMEOUT => 5,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_SSL_VERIFYPEER => false,
);
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (200 !== $httpcode)
{
curl_close($ch);
return false;
}
if ($response !== false)
{
curl_close($ch);
return $response;
}
curl_close($ch);
die();
}
function postMessage($text, $attachments)
{
return postToSlack($text, $attachments);
}
function postToSlack($text, $attachments)
{
foreach ($attachments as $title => $value)
{
$fields[] = array(
'title' => $title,
'value' => '<' . $value['url'] . '|' . $value['name'] . '>',
'short' => false,
);
}
$response = array(
'channel' => SLACK_CHANNEL,
'username' => SLACK_BOT_NAME,
'icon_url' => SLACK_BOT_AVATAR,
'unfurl_links' => false,
'attachments' => array(
array(
'fallback' => $text,
'pretext' => $text,
'color' => 'good',
'fields' => $fields,
),
),
);
$json = json_encode($response);
return getUrl(SLACK_WEBHOOK, $json);
}
function postToDocker($callbackUrl, $code)
{
$post = array(
'state' => ($code == 200 ? 'success' : 'error'),
);
$json = json_encode($post);
return getUrl($callbackUrl, $json);
}
if (SECRET == $_GET['token'])
{
$json = file_get_contents('php://input');
$docker = json_decode($json, true);
if (array_key_exists('push_data', $docker))
{
$fields = array(
'Tag' => array(
'name' => $docker['push_data']['tag'],
'url' => $docker['repository']['repo_url'] . '/tags/' . $docker['push_data']['tag'],
),
);
$response = postMessage('[' . $docker['repository']['repo_name'] . '] Image pushed by <https://hub.docker.com/u/' . $docker['push_data']['pusher'] . '|' . $docker['push_data']['pusher'] . '>', $fields);
postToDocker($docker['callback_url'], (true == $response ? 200 : 500));
}
}