Skip to content

Commit 5f78619

Browse files
authored
Added the google cloud functions guide (#9)
1 parent 07dbc80 commit 5f78619

File tree

9 files changed

+771
-0
lines changed

9 files changed

+771
-0
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,10 @@ The repository contains reference materials for the DevOps related videos made o
1414
- Reference Material - [Repo](./google-cloud-run-introduction/)
1515
- YouTube Video - [https://youtu.be/bKBBbryKj1w](https://youtu.be/bKBBbryKj1w)
1616

17+
### Google Cloud Functions Introduction
18+
19+
- Reference Material - [Repo](./google-cloud-functions-introduction/)
20+
- YouTube Video - [https://youtu.be/I0vJDmw6P4c](https://youtu.be/I0vJDmw6P4c)
21+
1722
### Shifting Policy Management left with OPA Gatekeeper
1823
- Reference Material - [Repo](./opa-gatekeeper)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Continuously Deploy Master Branch
2+
on:
3+
push:
4+
branches:
5+
- 'master'
6+
7+
jobs:
8+
cd:
9+
name: Deploy Master Branch
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v3
13+
14+
- id: auth
15+
name: 'Authenticate to Google Cloud'
16+
uses: 'google-github-actions/auth@v0'
17+
with:
18+
credentials_json: '${{ secrets.GCP_CREDS }}'
19+
20+
- name: 'Set up Cloud SDK'
21+
uses: 'google-github-actions/setup-gcloud@v0'
22+
with:
23+
project_id: space-cloud
24+
25+
- name: 'Deploy Function'
26+
run: 'gcloud functions deploy greeter --trigger-http --gen2 --runtime go116 --region us-east1 --entry-point=GreeterGet'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: Destroy Preview Environment
2+
on:
3+
pull_request:
4+
types: [closed]
5+
6+
env:
7+
SERVICE_NAME: greeter-${{ github.event.number }}
8+
9+
jobs:
10+
new-func:
11+
name: Destroy Preview Environment
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v3
15+
16+
- id: auth
17+
name: 'Authenticate to Google Cloud'
18+
uses: 'google-github-actions/auth@v0'
19+
with:
20+
credentials_json: '${{ secrets.GCP_CREDS }}'
21+
22+
- name: 'Set up Cloud SDK'
23+
uses: 'google-github-actions/setup-gcloud@v0'
24+
with:
25+
project_id: space-cloud
26+
27+
- name: 'Delete Function'
28+
run: |
29+
gcloud functions delete $SERVICE_NAME --gen2 --region us-east1
30+
31+
- uses: actions/github-script@v5
32+
with:
33+
github-token: ${{secrets.GITHUB_TOKEN}}
34+
script: |
35+
github.rest.issues.createComment({
36+
issue_number: context.issue.number,
37+
owner: context.repo.owner,
38+
repo: context.repo.repo,
39+
body: `Preview URL successfully destroyed`
40+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: Create Preview Environment
2+
on:
3+
pull_request:
4+
types: [opened, synchronize, reopened]
5+
6+
env:
7+
SERVICE_NAME: greeter-${{ github.event.number }}
8+
9+
jobs:
10+
new-func:
11+
name: Create Preview Environment
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v3
15+
16+
- id: auth
17+
name: 'Authenticate to Google Cloud'
18+
uses: 'google-github-actions/auth@v0'
19+
with:
20+
credentials_json: '${{ secrets.GCP_CREDS }}'
21+
22+
- name: 'Set up Cloud SDK'
23+
uses: 'google-github-actions/setup-gcloud@v0'
24+
with:
25+
project_id: space-cloud
26+
27+
- name: 'Deploy Function'
28+
run: |
29+
gcloud functions deploy $SERVICE_NAME \
30+
--set-env-vars='PR_NO=${{ github.event.number }}' \
31+
--allow-unauthenticated --trigger-http --gen2 --runtime go116 --region us-east1 --entry-point=GreeterGet
32+
33+
- name: 'Get PREVIEW URL'
34+
run: |
35+
echo PREVIEW_URL=$(gcloud functions describe $SERVICE_NAME --gen2 --region us-east1 --format="value(serviceConfig.uri)") >> $GITHUB_ENV
36+
37+
- uses: actions/github-script@v5
38+
with:
39+
github-token: ${{secrets.GITHUB_TOKEN}}
40+
script: |
41+
github.rest.issues.createComment({
42+
issue_number: context.issue.number,
43+
owner: context.repo.owner,
44+
repo: context.repo.repo,
45+
body: `Preview URL: ${process.env.PREVIEW_URL}`
46+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Google Cloud Run Introduction
2+
3+
> Here's a link to the YouTube video explaining this setup in greater detail:
4+
5+
[![Cloud Functions Introduction](./cloud-functions.png)](https://youtu.be/I0vJDmw6P4c)
6+
7+
## Prerequisites
8+
9+
- You'll need to have [gcloud](https://cloud.google.com/sdk/docs/installhttps://docs.docker.com/engine/install/) installed.
10+
- Make sure `gcloud` is configured. You can configure `gcloud` by running `gcloud init`.
11+
12+
## Steps To Reproduce The Video
13+
14+
### 1] Deploy Your Code On Google Cloud Function
15+
16+
```bash
17+
gcloud functions deploy greeter --trigger-http --gen2 --runtime go116 --region us-east1 --entry-point=GreeterGet
18+
```
19+
20+
> Answer `yes` for all the prompts.
21+
22+
You can test the app by running:
23+
24+
```bash
25+
curl https://[APP_URL]
26+
```
27+
28+
### 2] Implement Continous Deployments With Github Actions
29+
30+
1. Create a Service Account on GCP with `Admin` privileges and generate a `credential.json` file.
31+
2. Create a secret in your GitHub Repository with the name `GCP_CREDS` and paste the contents in the `credentials.json` file as the value.
32+
3. Push the contents of this directory including the `.github` folder to your git repo.
33+
4. Let the magic begin
34+
35+
### 3] Implement Preview Environments
36+
37+
Simply create a PR on the main branch and wait for the comment to appear.
Loading
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module github.com/YourTechBud/practical-guide/google-cloud-functions-introduction
2+
3+
go 1.17
4+
5+
require github.com/GoogleCloudPlatform/functions-framework-go v1.6.1
6+
7+
require (
8+
github.com/cloudevents/sdk-go/v2 v2.6.1 // indirect
9+
github.com/google/uuid v1.1.2 // indirect
10+
github.com/json-iterator/go v1.1.10 // indirect
11+
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
12+
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect
13+
go.uber.org/atomic v1.4.0 // indirect
14+
go.uber.org/multierr v1.1.0 // indirect
15+
go.uber.org/zap v1.10.0 // indirect
16+
)

0 commit comments

Comments
 (0)