|
| 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 | +# Eventarc – Generic – PHP Sample |
| 4 | + |
| 5 | +This directory contains a sample for receiving a generic event using Cloud Run and Eventarc with PHP. For testing purposes, we use Cloud Pub/Sub as an event source for our sample. |
| 6 | + |
| 7 | +## Sample |
| 8 | + |
| 9 | +| Sample | Description | Deploy | |
| 10 | +| --------------------------------------- | ------------------------ | ------------- | |
| 11 | +|[Generic][generic] | Quickstart | [<img src="https://storage.googleapis.com/cloudrun/button.svg" alt="Run on Google Cloud" height="30"/>][run_button_generic] | |
| 12 | + |
| 13 | +## Setup |
| 14 | + |
| 15 | +1. [Set up for Cloud Run development](https://cloud.google.com/run/docs/setup) |
| 16 | + |
| 17 | +1. Install the gcloud command-line tool beta components: |
| 18 | + |
| 19 | + ```sh |
| 20 | + gcloud components install beta |
| 21 | + ``` |
| 22 | + |
| 23 | +1. Set the following gcloud configurations, where `PROJECT_ID` is your Google Cloud project ID: |
| 24 | + |
| 25 | + ```sh |
| 26 | + gcloud config set project PROJECT_ID |
| 27 | + gcloud config set run/region us-central1 |
| 28 | + gcloud config set run/platform managed |
| 29 | + gcloud config set eventarc/location us-central1 |
| 30 | + ``` |
| 31 | + |
| 32 | +1. [Enable the Cloud Run, Cloud Logging, Cloud Build, Pub/Sub, and Eventarc APIs][enable_apis_url]. |
| 33 | + |
| 34 | +1. Clone this repository and navigate to this directory: |
| 35 | + |
| 36 | + ```sh |
| 37 | + git clone https://github.com/GoogleCloudPlatform/php-docs-samples.git |
| 38 | + cd php-docs-samples/eventarc/generic |
| 39 | + ``` |
| 40 | + |
| 41 | +## Run the sample locally |
| 42 | + |
| 43 | +1. [Install docker locally](https://docs.docker.com/install/) |
| 44 | + |
| 45 | +1. [Build the container locally](https://cloud.google.com/run/docs/building/containers#building_locally_and_pushing_using_docker): |
| 46 | + |
| 47 | + ```sh |
| 48 | + export SAMPLE='generic' |
| 49 | + docker build --tag "eventarc-$SAMPLE" . |
| 50 | + ``` |
| 51 | + |
| 52 | +1. [Run containers locally](https://cloud.google.com/run/docs/testing/local) |
| 53 | + |
| 54 | + With the built container: |
| 55 | + |
| 56 | + ```sh |
| 57 | + PORT=8080 && docker run --rm -p 8080:${PORT} -e PORT=${PORT} $SAMPLE |
| 58 | + ``` |
| 59 | + |
| 60 | + Test the web server with `cURL`: |
| 61 | + |
| 62 | + ```sh |
| 63 | + curl -XPOST localhost:8080 -d '{ "test": "foo" }' |
| 64 | + ``` |
| 65 | + |
| 66 | + Observe the output logs your HTTP request: |
| 67 | + |
| 68 | + ``` |
| 69 | + Event received! |
| 70 | + |
| 71 | + HEADERS: |
| 72 | + Host: localhost:8080 |
| 73 | + User-Agent: curl/7.64.1 |
| 74 | + Accept: */* |
| 75 | + Content-Length: 17 |
| 76 | + Content-Type: application/x-www-form-urlencoded |
| 77 | + |
| 78 | + BODY: |
| 79 | + { "test": "foo" } |
| 80 | + ``` |
| 81 | +
|
| 82 | + Exit the container with `Ctrl-D`. |
| 83 | +
|
| 84 | +## Run the sample on Cloud Run |
| 85 | +
|
| 86 | +1. [Build the container using Cloud Build](https://cloud.google.com/run/docs/building/containers#builder) |
| 87 | +
|
| 88 | + ```sh |
| 89 | + gcloud builds submit --tag gcr.io/$(gcloud config get-value project)/eventarc-generic-php |
| 90 | + ``` |
| 91 | +
|
| 92 | +1. [Deploy the container](https://cloud.google.com/run/docs/deploying#service) |
| 93 | +
|
| 94 | + ```sh |
| 95 | + gcloud run deploy eventarc-generic-php \ |
| 96 | + --image gcr.io/$(gcloud config get-value project)/eventarc-generic-php \ |
| 97 | + --allow-unauthenticated |
| 98 | + ``` |
| 99 | +
|
| 100 | + The command line will display the service URL when deployment is complete. |
| 101 | +
|
| 102 | +### Create an Eventarc Trigger |
| 103 | +
|
| 104 | +1. Create an Eventarc trigger for your Cloud Run service |
| 105 | +
|
| 106 | + ```sh |
| 107 | + gcloud beta eventarc triggers create eventarc-generic-php-trigger \ |
| 108 | + --destination-run-service=eventarc-generic-php \ |
| 109 | + --destination-run-region=us-central1 \ |
| 110 | + --matching-criteria="type=google.cloud.pubsub.topic.v1.messagePublished" |
| 111 | + ``` |
| 112 | +
|
| 113 | +1. Confirm the trigger was successfully created, run: |
| 114 | +
|
| 115 | + ```sh |
| 116 | + gcloud beta eventarc triggers describe eventarc-generic-php-trigger |
| 117 | + ``` |
| 118 | +
|
| 119 | + > Note: It can take up to 10 minutes for triggers to be fully functional. |
| 120 | +
|
| 121 | +### Send an Event |
| 122 | +
|
| 123 | +1. Find and set the Pub/Sub topic as an environment variable: |
| 124 | +
|
| 125 | + ```sh |
| 126 | + export RUN_TOPIC=$(gcloud beta eventarc triggers describe eventarc-generic-php-trigger \ |
| 127 | + --format='value(transport.pubsub.topic)') |
| 128 | + ``` |
| 129 | +
|
| 130 | +1. Send a message to the Pub/Sub topic to generate an event: |
| 131 | +
|
| 132 | + ```sh |
| 133 | + gcloud pubsub topics publish $RUN_TOPIC --message="Hello, PHP" |
| 134 | + ``` |
| 135 | +
|
| 136 | + The event is sent to the Cloud Run (fully managed) service, which logs the generic HTTP request. |
| 137 | +
|
| 138 | +### View an Event in Logs |
| 139 | +
|
| 140 | +1. To view the event, go to the Cloud Run (fully managed) service logs: |
| 141 | +
|
| 142 | + 1. Go to the [Google Cloud Console](https://console.cloud.google.com/run). |
| 143 | +
|
| 144 | + 1. Click the `eventarc-generic-php` service. |
| 145 | +
|
| 146 | + 1. Select the **Logs** tab. |
| 147 | +
|
| 148 | + > Logs might take a few moments to appear. If you don't see them immediately, check again after a few moments. |
| 149 | +
|
| 150 | + 1. Look for the log message "Event received!" followed by other log entries. This log entry indicates a request was sent by Eventarc to your Cloud Run service. |
| 151 | +
|
| 152 | +### Cleaning Up |
| 153 | +
|
| 154 | +To clean up, delete the resources created above: |
| 155 | +
|
| 156 | +1. Delete the Cloud Build container: |
| 157 | +
|
| 158 | + ```sh |
| 159 | + gcloud container images delete gcr.io/$(gcloud config get-value project)/eventarc-generic-php |
| 160 | + ``` |
| 161 | +
|
| 162 | +1. Delete the Cloud Run service: |
| 163 | +
|
| 164 | + ```sh |
| 165 | + gcloud run services delete eventarc-generic-php |
| 166 | + ``` |
| 167 | +
|
| 168 | +1. Delete the Eventarc trigger: |
| 169 | +
|
| 170 | + ```sh |
| 171 | + gcloud beta eventarc triggers delete eventarc-generic-php-trigger |
| 172 | + ``` |
| 173 | +
|
| 174 | +1. Delete the Pub/Sub topic: |
| 175 | +
|
| 176 | + ```sh |
| 177 | + gcloud pubsub topics delete $RUN_TOPIC |
| 178 | + ``` |
| 179 | +
|
| 180 | +[enable_apis_url]: https://console.cloud.google.com/flows/enableapi?apiid=run.googleapis.com,logging.googleapis.com,cloudbuild.googleapis.com,pubsub.googleapis.com,eventarc.googleapis.com |
| 181 | +[generic]: . |
| 182 | +[run_button_generic]: https://deploy.cloud.run/?dir=eventarc/generic |
0 commit comments