Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 32 additions & 24 deletions src/Commands/Deploy.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,26 +290,32 @@ private function uploadArtifacts(array $config, array $packageUrls): void
->intercept(new SetRequestTimeout(10, 10, $timeout, 60))
->build();

$promises = [];
foreach ($archivePaths as $id => $archivePath) {
$url = $packageUrls[$id];

IO::verbose(sprintf(
'Uploading %s (%d MB)',
$archivePath,
round(((float) filesize($archivePath)) / 1024. / 1024., 1)
));

$request = new Request($url, 'PUT', StreamedContent::fromFile($archivePath));
$promises[] = async(fn() => $client->request($request));
}

try {
await($promises);
} catch (TimeoutException) {
throw new Exception("Timeout while uploading packages after $timeout seconds. This is likely due to a slow network connection");
} catch (Exception $e) {
throw new Exception('Error while uploading packages: ' . $e->getMessage(), 0, $e);
$promises = [];
foreach ($archivePaths as $id => $archivePath) {
$url = $packageUrls[$id];

IO::verbose(sprintf(
'Uploading %s (%d MB)',
$archivePath,
round(((float) filesize($archivePath)) / 1024. / 1024., 1)
));

$request = new Request($url, 'PUT', StreamedContent::fromFile($archivePath));
$promises[] = async(fn() => $client->request($request));
}

try {
await($promises);
} catch (TimeoutException) {
throw new Exception("Timeout while uploading packages after $timeout seconds. This is likely due to a slow network connection");
} catch (Exception $e) {
throw new Exception('Error while uploading packages: ' . $e->getMessage(), 0, $e);
}
} finally {
foreach ($archivePaths as $archivePath) {
@unlink($archivePath);
}
}
}

Expand All @@ -318,10 +324,6 @@ private function uploadArtifacts(array $config, array $packageUrls): void
*/
private function packageArtifact(string $id, string $path, array $patterns): string
{
if (! is_dir('.bref') && ! mkdir('.bref') && ! is_dir('.bref')) {
throw new Exception(sprintf('Directory "%s" could not be created', '.bref'));
}

// Turn the package patterns into regexes
$patternRegexes = [];
foreach ($patterns as $pattern) {
Expand All @@ -335,7 +337,13 @@ private function packageArtifact(string $id, string $path, array $patterns): str
$patternRegexes[$regex] = $include;
}

$archivePath = ".bref/package-$id.zip";
// The archive is written outside the project so that it cannot end up inside another
// deployment package (e.g. a `serverless.yml` deployment of the same project), and it
// is deleted after the upload
$archivePath = tempnam(sys_get_temp_dir(), "bref-package-$id-");
if ($archivePath === false) {
throw new Exception('Could not create a temporary file to package the application');
}

$zip = new ZipArchive;
$zip->open($archivePath, ZipArchive::CREATE | ZipArchive::OVERWRITE);
Expand Down