Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds ability to deploy using gcloud beta #64

Merged
merged 6 commits into from
Jun 5, 2019
Merged
Show file tree
Hide file tree
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
53 changes: 35 additions & 18 deletions src/TestUtils/GcloudWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,34 @@ public function setDir($dir)
/**
* Deploy the app to the Google Cloud Platform.
*
* @param string $targets optional The yaml files for deployments.
* @param bool $promote optional true if you want to promote the new app.
* @param int $retries optional Number of retries upon failure.
* @param array $options list of options
* $targets string The yaml files for deployments.
* $promote bool True if you want to promote the new app.
* $retries int Number of retries upon failure.
* $release_version string Run using "alpha" or "beta" version of gcloud deploy
* @return bool true if deployment suceeds, false upon failure.
*/
public function deploy(
$targets = 'app.yaml',
$promote = false,
$retries = self::DEFAULT_RETRIES
) {
public function deploy($options = [])
{
// Handle old function signature
if (!is_array($options)) {
$options = array_filter([
'targets' => @func_get_arg(0),
'promote' => @func_get_arg(1),
]) + array_filter([
'retries' => @func_get_arg(2),
], 'is_numeric');
}
$options = array_merge([
'targets' => 'app.yaml',
'promote' => false,
'retries' => self::DEFAULT_RETRIES,
'release_version' => null,
], $options);
if (!in_array($options['release_version'], [null, 'alpha', 'beta'])) {
$this->errorLog('release_version must be "alpha" or "beta"');
return false;
}
if ($this->deployed) {
$this->errorLog('The app has been already deployed.');
return false;
Expand All @@ -131,16 +149,15 @@ public function deploy(
$this->errorLog('Can not chdir to ' . $this->dir);
return false;
}
$cmd = "gcloud -q " . self::GCLOUD_APP . " deploy "
. "--project " . $this->project . " "
. "--version " . $this->version . " ";
if ($promote) {
$cmd .= "--promote ";
} else {
$cmd .= "--no-promote ";
}
$cmd .= $targets;
$ret = $this->execWithRetry($cmd, $retries);
$cmd = sprintf('gcloud -q %s%s deploy --project %s --version %s %s %s',
$options['release_version'] ? $options['release_version'] . ' ' : '',
self::GCLOUD_APP,
$this->project,
$this->version,
$options['promote'] ? '--promote' : '--no-promote',
$options['targets']
);
$ret = $this->execWithRetry($cmd, $options['retries']);
chdir($orgDir);
if ($ret) {
$this->deployed = true;
Expand Down
37 changes: 37 additions & 0 deletions test/TestUtils/GcloudWrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,43 @@ public function testDeployAndDeleteWithCustomArgs()
$this->mockGcloudWrapper->delete('myservice', 4);
}

public function testDeployWithArgsArray()
{
$this->mockGcloudWrapper = $this->getMockBuilder(
'\Google\Cloud\TestUtils\GcloudWrapper'
)
->setMethods(array('execWithRetry'))
->setConstructorArgs(array('project', 'version'))
->getMock();
$this->mockGcloudWrapper->method('execWithRetry')->willReturn(true);
$deployCmd = 'gcloud -q beta app deploy '
. '--project project --version version '
. '--promote app.yaml backend.yaml';
$deleteCmd = 'gcloud -q app versions delete '
. '--service myservice version '
. '--project project';
$this->mockGcloudWrapper->expects($this->exactly(2))
->method('execWithRetry')
->withConsecutive(
array($this->equalTo($deployCmd), $this->equalTo(4)),
array($this->equalTo($deleteCmd), $this->equalTo(4))
);

$this->mockGcloudWrapper->deploy([
'targets' => 'app.yaml backend.yaml',
'promote' => true,
'retries' => 4,
'release_version' => 'beta',
]);

$this->assertEquals(
'https://version-dot-project.appspot.com',
$this->mockGcloudWrapper->getBaseUrl()
);

$this->mockGcloudWrapper->delete('myservice', 4);
}

public function testRunAndStopWithDefault()
{
$this->mockGcloudWrapper = $this->getMockBuilder(
Expand Down