Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial commit for cloudrun build and deploy workflows #244

Merged
merged 6 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions .github/workflows/build-deploy-cloudrun-function.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
name: Build and Deploy Cloud Run Function

on:
push:
branches:
- $default-branch
- staging
workflow_call:
inputs:
environment:
type: string
description: environment to deploy to
required: true
function_name:
description: 'The name of the Cloud Function to deploy'
type: string
required: true
entry_point:
description: 'The python function serving as the entry point'
type: string
required: true
runtime:
description: 'The function runtime'
type: string
required: true
secrets:
GCP_PROJECT_ID:
GCP_PROJECT_NUMBER:
WORKLOAD_IDENTITY_POOL:
WORKLOAD_IDENTITY_PROVIDER:
GCP_SERVICE_ACCOUNT:
GCP_SERVICE_ACCOUNT_EMAIL:
GCP_REGION:

jobs:
build-and-deploy:
runs-on: ubuntu-latest
environment: ${{ inputs.environment }}
permissions:
contents: read
id-token: write
pull-requests: write
steps:
- name: Checkout code
uses: actions/checkout@v4

- id: 'auth'
name: Authenticate to Google Cloud
uses: google-github-actions/auth@v2
env:
GCP_PROJECT_ID: ${{ vars.GCP_PROJECT_ID }}
GCP_PROJECT_NUMBER: ${{ vars.GCP_PROJECT_NUMBER }}
WORKLOAD_IDENTITY_POOL: ${{ vars.WORKLOAD_IDENTITY_POOL }}
WORKLOAD_IDENTITY_PROVIDER: ${{ vars.WORKLOAD_IDENTITY_PROVIDER }}
GCP_SERVICE_ACCOUNT: ${{ vars.GCP_SERVICE_ACCOUNT }}
GCP_SERVICE_ACCOUNT_EMAIL: ${{ vars.GCP_SERVICE_ACCOUNT_EMAIL }}
with:
project_id: ${{ vars.GCP_PROJECT_ID }}
workload_identity_provider: 'projects/${{ vars.GCP_PROJECT_NUMBER }}/locations/global/workloadIdentityPools/${{ vars.WORKLOAD_IDENTITY_POOL }}/providers/${{ vars.WORKLOAD_IDENTITY_PROVIDER }}'
service_account: '${{ vars.GCP_SERVICE_ACCOUNT }}@${{ vars.GCP_PROJECT_ID }}.iam.gserviceaccount.com'

- name: Set up Cloud SDK
uses: google-github-actions/setup-gcloud@v2

- name: Deploy to Cloud Run Functions
id: deploy
env:
GCP_PROJECT_ID: ${{ vars.GCP_PROJECT_ID }}
GCP_SERVICE_ACCOUNT_EMAIL: ${{ vars.GCP_SERVICE_ACCOUNT_EMAIL }}
GCP_REGION: ${{ vars.GCP_REGION }}
run: |
gcloud config set project ${{ vars.GCP_PROJECT_ID }}
gcloud functions deploy ${{ inputs.function_name }} \
--region ${{ vars.GCP_REGION }} \
--source=./${{ inputs.function_name }} \
--entry-point=${{ inputs.entry_point }} \
--runtime=${{ inputs.runtime }} \
--build-service-account=projects/${{ vars.GCP_PROJECT_ID }}/serviceAccounts/${{ vars.GCP_SERVICE_ACCOUNT_EMAIL }}

- uses: actions/github-script@v6
if: github.event.pull_request.merged == true
with:
script: |
const output = `#### GCF Deploy ⚙️\`${{ steps.deploy.outcome }}\`
<details><summary>Show Deploy</summary>
\`\`\`\n
${{ steps.deploy.outputs.stdout }}
\`\`\`
</details>
*Pusher: @${{ github.actor }}, Action: \`${{ github.event_name }}\`, Working Directory: \`${{ inputs.function_name }}\`, Workflow: \`${{ github.workflow }}\`*`;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: output
14 changes: 14 additions & 0 deletions workflow-templates/build-deploy-cloudrun-function.properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "Build and Deploy Cloud Run Function",
"description": "Deploy a Cloud Function to Google Cloud Run. This workflow requires authentication to Google Cloud.",
"filePatterns": [
"^Dockerfile",
"^cloudrun-function-config.yml"
],
"inputs": {
"function_name": {
"description": "The name of the Cloud Function to deploy",
"required": true
}
}
}
41 changes: 41 additions & 0 deletions workflow-templates/build-deploy-cloudrun-function.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Build & Deploy Cloudrun Function POC

on:
push:
branches:
# Automatically deploy $default-branch. Create a workflow per branch.
- $default-branch
paths:
- 'Path-to-fuction/**' # Update with path to the function
workflow_dispatch:
inputs:
function_name:
description: 'The name of the Cloud Function to deploy'
required: true
default: 'function-name' # Update with function name
entry_point:
description: 'The python function serving as the entry point'
required: true
default: 'entry_point' # Update with entry point
runtime:
description: 'The function runtime'
required: true
default: 'runtime' # Update with runtime

jobs:
build_and_deploy:

uses: CruGlobal/.github/.github/workflows/build-deploy-cloudrun-function.yml@v1
with:
function_name: ${{ github.event.inputs.function_name }}
entry_point: { entry_point } # hello_http
runtime: { runtime } # python312
environment: { environment } # production
secrets:
GCP_PROJECT_ID: ${{ vars.GCP_PROJECT_ID }}
GCP_PROJECT_NUMBER: ${{ vars.GCP_PROJECT_NUMBER }}
WORKLOAD_IDENTITY_POOL: ${{ vars.WORKLOAD_IDENTITY_POOL }}
WORKLOAD_IDENTITY_PROVIDER: ${{ vars.WORKLOAD_IDENTITY_PROVIDER }}
GCP_SERVICE_ACCOUNT: ${{ vars.GCP_SERVICE_ACCOUNT }}
GCP_SERVICE_ACCOUNT_EMAIL: ${{ vars.GCP_SERVICE_ACCOUNT_EMAIL }}
GCP_REGION: ${{ vars.GCP_REGION }}
cru-Luis-Rodriguez marked this conversation as resolved.
Show resolved Hide resolved