Skip to content

Commit 5652d4b

Browse files
authored
feat: add example workflows (#18)
* feat: add example workflows * updates comments
1 parent a668135 commit 5652d4b

11 files changed

+641
-4
lines changed

.github/workflows/deploy-cloudrun-credentials-it.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
name: deploy-cloudrun credentials Integration
22

3-
on: [push]
3+
on:
4+
push:
5+
branches-ignore:
6+
- 'example-*'
47

58
jobs:
69
gcloud:

.github/workflows/deploy-cloudrun-it.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
name: deploy-cloudrun Integration
22

3-
on: [push]
3+
on:
4+
push:
5+
branches-ignore:
6+
- 'example-*'
47

58
jobs:
69
envvars:

.github/workflows/deploy-cloudrun.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: deploy-cloudrun Unit
22

3-
on: [push, pull_request]
3+
on: [pull_request]
44

55
jobs:
66
run:
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
# http://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+
on:
16+
push:
17+
branches:
18+
- example-deploy
19+
20+
name: Quickstart - Deploy Prebuilt Container
21+
22+
jobs:
23+
deploy:
24+
runs-on: ubuntu-latest
25+
steps:
26+
27+
- name: Deploy to Cloud Run
28+
id: deploy
29+
uses: google-github-actions/[email protected]
30+
with:
31+
image: gcr.io/cloudrun/hello
32+
service: hello-cloud-run
33+
credentials: ${{ secrets.GCP_SA_KEY }}
34+
35+
- name: Show Output
36+
run: echo ${{ steps.deploy.outputs.url }}
37+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
# http://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+
on:
16+
push:
17+
branches:
18+
- example-build-deploy
19+
20+
name: Build and Deploy a Container
21+
env:
22+
PROJECT_ID: ${{ secrets.GCP_PROJECT }}
23+
SERVICE: hello-cloud-run
24+
REGION: us-central1
25+
26+
jobs:
27+
deploy:
28+
runs-on: ubuntu-latest
29+
steps:
30+
- name: Checkout
31+
uses: actions/checkout@v2
32+
33+
- name: Setup Cloud SDK
34+
uses: google-github-actions/[email protected]
35+
with:
36+
project_id: ${{ env.PROJECT_ID }}
37+
service_account_key: ${{ secrets.GCP_SA_KEY }}
38+
export_default_credentials: true # Set to true to authenticate the Cloud Run action
39+
40+
- name: Authorize Docker push
41+
run: gcloud auth configure-docker
42+
43+
- name: Build and Push Container
44+
run: |-
45+
docker build -t gcr.io/${{ env.PROJECT_ID }}/${{ env.SERVICE }}:${{ github.sha }} example-app/
46+
docker push gcr.io/${{ env.PROJECT_ID }}/${{ env.SERVICE }}:${{ github.sha }}
47+
48+
- name: Deploy to Cloud Run
49+
id: deploy
50+
uses: google-github-actions/[email protected]
51+
with:
52+
service: ${{ env.SERVICE }}
53+
image: gcr.io/${{ env.PROJECT_ID }}/${{ env.SERVICE }}:${{ github.sha }}
54+
region: ${{ env.REGION }}
55+
56+
- name: Show Output
57+
run: echo ${{ steps.deploy.outputs.url }}

README.md

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,92 @@ only works using a custom runner hosted on GCP.**
174174
The action will automatically detect and use the Application Default
175175
Credentials.
176176

177+
## Example Workflows
178+
179+
* [Deploy a prebuilt container](#deploy-a-prebuilt-container)
180+
181+
* [Build and deploy a container](#build-and-deploy-a-container)
182+
183+
### Setup
184+
185+
1. Create a new Google Cloud Project (or select an existing project).
186+
187+
1. [Enable the Cloud Run API](https://console.cloud.google.com/flows/enableapi?apiid=run.googleapis.com).
188+
189+
1. [Create a Google Cloud service account][sa] or select an existing one.
190+
191+
1. Add the the following [Cloud IAM roles][roles] to your service account:
192+
193+
- `Cloud Run Admin` - allows for the creation of new Cloud Run services
194+
195+
- `Service Account User` - required to deploy to Cloud Run as service account
196+
197+
- `Storage Admin` - allow push to Google Container Registry (this grants project level access, but recommend reducing this scope to [bucket level permissions](https://cloud.google.com/container-registry/docs/access-control#grant).)
198+
199+
1. [Download a JSON service account key][create-key] for the service account.
200+
201+
1. Add the following [secrets to your repository's secrets][gh-secret]:
202+
203+
- `GCP_PROJECT`: Google Cloud project ID
204+
205+
- `GCP_SA_KEY`: the downloaded service account key
206+
207+
### Deploy a prebuilt container
208+
209+
To run this [workflow](.github/workflows/example-workflow-quickstart.yaml), push to the branch named `example-deploy`:
210+
211+
```sh
212+
git push YOUR-FORK main:example-deploy
213+
```
214+
215+
### Build and deploy a container
216+
217+
To run this [workflow](.github/workflows/example-workflow.yaml), push to the branch named `example-build-deploy`:
218+
219+
```sh
220+
git push YOUR-FORK main:example-build-deploy
221+
```
222+
223+
**Reminder: If this is your first deployment of a service, it will reject all unauthenticated requests. Learn more at [allowing unauthenticated requests](#Allow-unauthenticated-requests)**
224+
225+
## Migrating from `setup-gcloud`
226+
227+
Example using `setup-gcloud`:
228+
229+
```YAML
230+
- name: Setup Cloud SDK
231+
uses: google-github-actions/[email protected]
232+
with:
233+
project_id: ${{ env.PROJECT_ID }}
234+
service_account_key: ${{ secrets.GCP_SA_KEY }}
235+
236+
- name: Deploy to Cloud Run
237+
run: |-
238+
gcloud run deploy $SERVICE \
239+
--region $REGION \
240+
--image gcr.io/$PROJECT_ID/$SERVICE \
241+
--platform managed \
242+
--set-env-vars NAME="Hello World"
243+
```
244+
245+
Migrated to `deploy-cloudrun`:
246+
247+
```YAML
248+
- name: Deploy to Cloud Run
249+
uses: google-github-actions/[email protected]
250+
with:
251+
service: ${{ env.SERVICE }}
252+
image: gcr.io/${{ env.PROJECT_ID }}/${{ env.SERVICE }}
253+
region: ${{ env.REGION }}
254+
credentials: ${{ secrets.GCP_SA_KEY }}
255+
env_vars: NAME="Hello World"
256+
```
257+
Note: The action is for the "managed" platform and will not set access privileges such as [allowing unauthenticated requests](#Allow-unauthenticated-requests).
258+
259+
177260
[cloud-run]: https://cloud.google.com/run
178-
[sm]: https://cloud.google.com/secret-manager
179261
[sa]: https://cloud.google.com/iam/docs/creating-managing-service-accounts
262+
[create-key]: https://cloud.google.com/iam/docs/creating-managing-service-account-keys
180263
[gh-runners]: https://help.github.com/en/actions/hosting-your-own-runners/about-self-hosted-runners
181264
[gh-secret]: https://help.github.com/en/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets
182265
[setup-gcloud]: ./setup-gcloud

example-app/.dockerignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Dockerfile
2+
README.md
3+
node_modules
4+
npm-debug.log

example-app/Dockerfile

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
# http://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+
# Use the official lightweight Node.js 12 image.
16+
# https://hub.docker.com/_/node
17+
FROM node:12-slim
18+
19+
# Create and change to the app directory.
20+
WORKDIR /usr/src/app
21+
22+
# Copy application dependency manifests to the container image.
23+
# A wildcard is used to ensure both package.json AND package-lock.json are copied.
24+
# Copying this separately prevents re-running npm install on every code change.
25+
COPY package*.json ./
26+
27+
# Install production dependencies.
28+
RUN npm install --only=production
29+
30+
# Copy local code to the container image.
31+
COPY . ./
32+
33+
# Run the web service on container startup.
34+
CMD [ "npm", "start" ]

example-app/index.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
// http://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+
const express = require('express');
16+
const app = express();
17+
18+
app.get('/', (req, res) => {
19+
console.log('Hello world received a request.');
20+
21+
const target = process.env.TARGET || 'World';
22+
res.send(`Hello ${target}!`);
23+
});
24+
25+
const port = process.env.PORT || 8080;
26+
app.listen(port, () => {
27+
console.log('Hello world listening on port', port);
28+
});

0 commit comments

Comments
 (0)