Skip to content

Commit eec5f5c

Browse files
authored
feat(run): add helloworld (GoogleCloudPlatform#1175)
1 parent bd5e175 commit eec5f5c

File tree

9 files changed

+339
-1
lines changed

9 files changed

+339
-1
lines changed

run/README.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<img src="https://avatars2.githubusercontent.com/u/2810941?v=3&s=96" alt="Google Cloud Platform logo" title="Google Cloud Platform" align="right" height="96" width="96"/>
2+
3+
# Google Cloud Run PHP Samples
4+
5+
[Cloud Run][run_docs] runs stateless [containers](https://cloud.google.com/containers/) on a fully managed environment or in your own GKE cluster.
6+
7+
## Samples
8+
9+
| Sample | Description | Deploy |
10+
| --------------------------------------- | ------------------------ | ------------- |
11+
|[Hello World][helloworld] | Quickstart | [<img src="https://storage.googleapis.com/cloudrun/button.svg" alt="Run on Google Cloud" height="30"/>][run_button_helloworld] |
12+
13+
For more Cloud Run samples beyond PHP, see the main list in the [Cloud Run Samples repository](https://github.com/GoogleCloudPlatform/cloud-run-samples).
14+
15+
## Setup
16+
17+
1. [Set up for Cloud Run development](https://cloud.google.com/run/docs/setup)
18+
19+
2. Clone this repository:
20+
21+
```sh
22+
git clone https://github.com/GoogleCloudPlatform/php-docs-samples.git
23+
```
24+
25+
## How to run a sample locally
26+
27+
1. [Install docker locally](https://docs.docker.com/install/)
28+
29+
2. [Build the sample container](https://cloud.google.com/run/docs/building/containers#building_locally_and_pushing_using_docker):
30+
31+
```sh
32+
export SAMPLE='helloworld'
33+
cd php-docs-samples/run/$SAMPLE
34+
docker build --tag $SAMPLE .
35+
```
36+
37+
3. [Run containers locally](https://cloud.google.com/run/docs/testing/local)
38+
39+
With the built container:
40+
41+
```sh
42+
PORT=8080 && docker run --rm -p 8080:${PORT} -e PORT=${PORT} $SAMPLE
43+
```
44+
45+
Overriding the built container with local code:
46+
47+
```sh
48+
PORT=8080 && docker run --rm \
49+
-p 8080:${PORT} -e PORT=${PORT} \
50+
-v $PWD:/usr/src/app $SAMPLE
51+
```
52+
53+
Injecting your service account key:
54+
55+
```sh
56+
export SA_KEY_NAME=my-key-name-123
57+
PORT=8080 && docker run --rm \
58+
-p 8080:${PORT} -e PORT=${PORT} \
59+
-e GOOGLE_APPLICATION_CREDENTIALS=/tmp/keys/${SA_KEY_NAME}.json \
60+
-v $GOOGLE_APPLICATION_CREDENTIALS:/tmp/keys/${SA_KEY_NAME}.json:ro \
61+
-v $PWD:/usr/src/app $SAMPLE
62+
```
63+
64+
Opening a shell in the container:
65+
66+
1. Build the container.
67+
68+
2. Run the container with a shell:
69+
70+
```sh
71+
PORT=8080 && docker run --rm \
72+
--interactive --tty \
73+
-p 8080:${PORT} -e PORT=${PORT} \
74+
-v $PWD:/var/www/html $SAMPLE \
75+
/bin/bash
76+
```
77+
78+
3. Exit the container: `Ctrl-D`
79+
80+
## Deploying
81+
82+
See [Building containers][run_build] and [Deploying container images][run_deploy]
83+
for more information.
84+
85+
[run_docs]: https://cloud.google.com/run/docs/
86+
[run_build]: https://cloud.google.com/run/docs/building/containers
87+
[run_deploy]: https://cloud.google.com/run/docs/deploying
88+
[helloworld]: helloworld/
89+
[run_button_helloworld]: https://deploy.cloud.run/?dir=run/helloworld

run/helloworld/.dockerignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# The .dockerignore file excludes files from the container build process.
2+
#
3+
# https://docs.docker.com/engine/reference/builder/#dockerignore-file
4+
5+
# Exclude locally vendored dependencies.
6+
vendor/
7+
8+
# Exclude "build-time" ignore files.
9+
.dockerignore
10+
.gcloudignore
11+
12+
# Exclude git history and configuration.
13+
.gitignore

run/helloworld/.gcloudignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# The .gcloudignore file excludes file from upload to Cloud Build.
2+
# If this file is deleted, gcloud will default to .gitignore.
3+
#
4+
# https://cloud.google.com/cloud-build/docs/speeding-up-builds#gcloudignore
5+
# https://cloud.google.com/sdk/gcloud/reference/topic/gcloudignore
6+
7+
# Exclude locally vendored dependencies.
8+
vendor/
9+
10+
# Exclude git history and configuration.
11+
.git/
12+
.gitignore

run/helloworld/Dockerfile

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START run_helloworld_dockerfile]
16+
17+
# Use the official PHP image.
18+
# https://hub.docker.com/_/php
19+
FROM php:7.4-apache
20+
21+
# Configure PHP for Cloud Run.
22+
# Precompile PHP code with opcache.
23+
RUN docker-php-ext-install -j "$(nproc)" opcache
24+
RUN set -ex; \
25+
{ \
26+
echo "; Cloud Run enforces memory & timeouts"; \
27+
echo "memory_limit = -1"; \
28+
echo "max_execution_time = 0"; \
29+
echo "; File upload at Cloud Run network limit"; \
30+
echo "upload_max_filesize = 32M"; \
31+
echo "post_max_size = 32M"; \
32+
echo "; Configure Opcache for Containers"; \
33+
echo "opcache.enable = On"; \
34+
echo "opcache.validate_timestamps = Off"; \
35+
echo "; Configure Opcache Memory (Application-specific)"; \
36+
echo "opcache.memory_consumption = 32"; \
37+
} > "$PHP_INI_DIR/conf.d/cloud-run.ini"
38+
39+
# Copy in custom code from the host machine.
40+
WORKDIR /var/www/html
41+
COPY . ./
42+
43+
# Use the PORT environment variable in Apache configuration files.
44+
# https://cloud.google.com/run/docs/reference/container-contract#port
45+
RUN sed -i 's/80/${PORT}/g' /etc/apache2/sites-available/000-default.conf /etc/apache2/ports.conf
46+
47+
# Configure PHP for development.
48+
# Switch to the production php.ini for production operations.
49+
# RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
50+
# https://github.com/docker-library/docs/blob/master/php/README.md#configuration
51+
RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"
52+
53+
# [END run_helloworld_dockerfile]

run/helloworld/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Hello World for Cloud Run
2+
3+
This sample demonstrates how to deploy a **Hello World** application to Cloud Run.
4+
5+
**View the [full tutorial](https://cloud.google.com/run/docs/quickstarts/build-and-deploy#php)**

run/helloworld/index.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
/**
3+
* Copyright 2020 Google LLC.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
// [START run_helloworld_service]
19+
20+
$name = getenv('NAME', true) ?: 'World';
21+
echo sprintf('Hello %s!', $name);
22+
23+
// [END run_helloworld_service]

run/helloworld/phpunit.xml.dist

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
xml version="1.0" encoding="UTF-8"?>
2+
14+
<phpunit bootstrap="../../testing/vendor/autoload.php" convertWarningsToExceptions="false">
15+
<testsuites>
16+
<testsuite name="Cloud Run Hello World tests">
17+
<directory>testdirectory>
18+
testsuite>
19+
testsuites>
20+
<logging>
21+
<log type="coverage-clover" target="build/logs/clover.xml"/>
22+
logging>
23+
phpunit>

run/helloworld/test/DeployTest.php

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
2+
/**
3+
* Copyright 2020 Google LLC.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace Google\Cloud\Samples\Run\Helloworld;
19+
20+
use Google\Auth\ApplicationDefaultCredentials;
21+
use Google\Cloud\TestUtils\DeploymentTrait;
22+
use Google\Cloud\TestUtils\EventuallyConsistentTestTrait;
23+
use Google\Cloud\TestUtils\GcloudWrapper\CloudRun;
24+
use Google\Cloud\TestUtils\TestTrait;
25+
use GuzzleHttp\Client;
26+
use GuzzleHttp\HandlerStack;
27+
use PHPUnit\Framework\TestCase;
28+
29+
/**
30+
* Class DeployTest.
31+
*/
32+
class DeloyTest extends TestCase
33+
{
34+
use DeploymentTrait;
35+
use EventuallyConsistentTestTrait;
36+
use TestTrait;
37+
38+
/** @var \Google\Cloud\TestUtils\GcloudWrapper\CloudRun */
39+
private static $service;
40+
41+
/** @var string */
42+
private static $image;
43+
44+
/**
45+
* Deploy the application.
46+
*
47+
* @beforeClass
48+
*/
49+
public static function setUpDeploymentVars()
50+
{
51+
$projectId = self::requireEnv('GOOGLE_PROJECT_ID');
52+
$versionId = self::requireEnv('GOOGLE_VERSION_ID');
53+
self::$service = new CloudRun($projectId, ['service' => $versionId]);
54+
self::$image = sprintf('gcr.io/%s/%s:latest', $projectId, $versionId);
55+
}
56+
57+
private static function beforeDeploy()
58+
{
59+
// Ensure setUpDeploymentVars has been called
60+
if (is_null(self::$service)) {
61+
self::setUpDeploymentVars();
62+
}
63+
64+
// Suppress gcloud prompts during deployment.
65+
putenv('CLOUDSDK_CORE_DISABLE_PROMPTS=1');
66+
}
67+
68+
/**
69+
* Deploy the Cloud Run service.
70+
*/
71+
private static function doDeploy()
72+
{
73+
if (false === self::$service->build(self::$image)) {
74+
return false;
75+
}
76+
77+
if (false === self::$service->deploy(self::$image)) {
78+
return false;
79+
}
80+
81+
return true;
82+
}
83+
84+
/**
85+
* Delete a deployed Cloud Run service.
86+
*/
87+
private static function doDelete()
88+
{
89+
self::$service->delete();
90+
self::$service->deleteImage(self::$image);
91+
}
92+
93+
public function testService()
94+
{
95+
$targetAudience = self::getBaseUri();
96+
97+
// create middleware
98+
$middleware = ApplicationDefaultCredentials::getIdTokenMiddleware($targetAudience);
99+
$stack = HandlerStack::create();
100+
$stack->push($middleware);
101+
102+
// create the HTTP client
103+
$client = new Client([
104+
'handler' => $stack,
105+
'auth' => 'google_auth',
106+
'base_uri' => $targetAudience,
107+
]);
108+
109+
// Run the test.
110+
$resp = $client->get('/');
111+
$this->assertEquals('200', $resp->getStatusCode());
112+
$this->assertEquals('Hello World!', (string) $resp->getBody());
113+
}
114+
115+
public function getBaseUri()
116+
{
117+
return self::$service->getBaseUrl();
118+
}
119+
}

testing/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"phpunit/phpunit": "^7",
88
"bshaffer/phpunit-retry-annotations": "^0.1.0",
99
"guzzlehttp/guzzle": "^7.0",
10-
"symfony/browser-kit": "^4.0"
10+
"symfony/browser-kit": "^4.0",
11+
"google/auth": "^1.12"
1112
}
1213
}

0 commit comments

Comments
 (0)