diff --git a/.github/workflows/build-deploy-cloudrun-function.yml b/.github/workflows/build-deploy-cloudrun-function.yml new file mode 100644 index 0000000..429df3a --- /dev/null +++ b/.github/workflows/build-deploy-cloudrun-function.yml @@ -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 }}\` +
Show Deploy + \`\`\`\n + ${{ steps.deploy.outputs.stdout }} + \`\`\` +
+ *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 \ No newline at end of file diff --git a/workflow-templates/build-deploy-cloudrun-function.properties.json b/workflow-templates/build-deploy-cloudrun-function.properties.json new file mode 100644 index 0000000..86bb5f0 --- /dev/null +++ b/workflow-templates/build-deploy-cloudrun-function.properties.json @@ -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 + } + } +} \ No newline at end of file diff --git a/workflow-templates/build-deploy-cloudrun-function.yml b/workflow-templates/build-deploy-cloudrun-function.yml new file mode 100644 index 0000000..a53d568 --- /dev/null +++ b/workflow-templates/build-deploy-cloudrun-function.yml @@ -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 }}