-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun.php
55 lines (36 loc) · 1.83 KB
/
run.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
<?php
require_once __DIR__ . '/lib.php';
$projects = include('projects.php');
if (!isset($argv[1])) {
throw new \InvalidArgumentException('Undefined project name');
}
$project = $argv[1];
$triggerFile = __DIR__ . '/triggers/' . $project;
if (!is_file($triggerFile)) {
exit(0);
}
if (!unlink($triggerFile)) {
throw new \RuntimeException('Erreur suppressison du fichier de trigger ' . $triggerFile);
}
$lockFile = __DIR__ . '/locks/' . $project;
if (is_file($lockFile)) {
exit(0);
}
if (!touch($lockFile)) {
throw new \RuntimeException('Erreur écriture du fichier de lock ' . $lockFile);
}
$channelName = "#outils-notifications";
$playbook = escapeshellarg(__DIR__ . '/playbooks/' . $project . '.yml');
$command = 'ANSIBLE_LOCAL_TEMP=/tmp/ansible_local_tmp_deploy ANSIBLE_REMOTE_TEMP=/tmp/ansible_remote_tmp_deploy ANSIBLE_CALLBACK_WHITELIST=profile_tasks /usr/local/bin/ansible-playbook -i "localhost," -c local ' . $playbook . ' | grep --line-buffered -v -P "changed:|ok:|\(\d{1}:\d{2}:\d{2}.\d{3}\)|==============" ';
$slack = new Slack(getenv('DEPLOY_API_KEY'), "Déploiement", 'https://avatars2.githubusercontent.com/u/1090307?s=200&v=4');
$deploySlack = new DeploySlack($slack, $channelName);
$slack->postMessage($channelName, sprintf('Le déploiement du projet %s a débuté', $project));
$outputStr = $deploySlack->executeAndSendToSlack($slack, $command);
$logFile = __DIR__ . '/logs/deploy_' . $project . '_' . date('Y-m-d_H-i-s') . '_' . getmypid() . '.log';
if (!file_put_contents($logFile, $outputStr)) {
throw new \RuntimeException('Erreur écriture log ' . $logFile);
}
if (!unlink($lockFile)) {
throw new \RuntimeException('Erreur suppression du fichier de lock ' . $lockFile);
}
$slack->postMessage($channelName, sprintf('Le projet %s a été mis à jour', $project), isset($projects[$project]) ? $projects[$project] : []);