Skip to content

Commit

Permalink
Adds "release_version" flag to gcloudWrapper::deploy (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer authored Jun 5, 2019
1 parent a1eb4e2 commit 3142fc9
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 18 deletions.
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

0 comments on commit 3142fc9

Please sign in to comment.