Answer the question
In order to leave comments, you need to log in
Sequential execution of php exec commands?
Colleagues, welcome!
I set up continuous deployment for testing in production
It is known that && logical AND
The second command will be executed if the first one completed without errors.
But the problem is that a number of commands give errors, but at the same time it completes successfully.
For example:
/usr/local/bin/docker-compose -f docker-compose-epsyteam.yml exec app composer install
<?php
define('GITHUB_SECRET', '***********');
set_time_limit(3600);
header('Content-Type', 'text/plain');
// Ensure signature exists
if (!isset($_SERVER['HTTP_X_HUB_SIGNATURE'])) {
http_response_code(403);
die('Missing signature');
}
// Ensure that signature is formed as expected
$signature = explode('=', $_SERVER['HTTP_X_HUB_SIGNATURE']);
if (2 !== count($signature)) {
http_response_code(400);
die('Malformed signature');
}
// Parse signature
$algos = hash_algos();
list($algo, $hash) = $signature;
if (!in_array($algo, $algos)) {
http_response_code(400);
die("Unknown hashing algo: $algo");
}
// Load payload
$payload = file_get_contents('php://input');
// Check signature
$expectedHash = hash_hmac($algo, $payload, GITHUB_SECRET);
if ($expectedHash !== $hash) {
http_response_code(403);
die('Invalid signature');
}
// Parse data
$data = json_decode($payload, true);
// Configure commands
$commands = [
"epsy-me/epsy-api" => [
"cd /var/www/epsyteam/data/epsy-api/",
"pwd",
"git reset --hard",
"git pull origin develop",
"/usr/local/bin/docker-compose -f docker-compose-epsyteam.yml kill",
"/usr/local/bin/docker-compose -f docker-compose-epsyteam.yml down",
"/usr/local/bin/docker-compose -f docker-compose-epsyteam.yml up -d --build",
"/usr/local/bin/docker-compose -f docker-compose-epsyteam.yml exec app composer install",
"/usr/local/bin/docker-compose -f docker-compose-epsyteam.yml exec app composer dump-env prod",
"/usr/local/bin/docker-compose -f docker-compose-epsyteam.yml exec app php bin/console doctrine:schema:update --dump-sql --force"
]
];
// Run
$repo = $data['repository']['full_name'];
if (isset($commands[$repo])) {
$command = implode(' && ', $commands[$repo]);
$output = [];
$output[] = $repo;
$output[] = "================================================";
exec($command, $output, $returnCode);
mailTo("[email protected]", implode("\n - ", $output), "EPSY DEPLOYMENT");
} else {
http_response_code(400);
$message = "Repository not configured: $repo\n";
mailTo("[email protected]", $message, "EPSY DEPLOYMENT ERROR");
die($message);
}
function mailTo($to, $message, $subject)
{
$headers = "From: [email protected]" . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
}
Answer the question
In order to leave comments, you need to log in
# man bash
...
Commands separated by a ; are executed sequentially; the shell waits for each command
to terminate in turn.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question