Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
feat: add delete cloud run service workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
erzz committed Jan 20, 2022
1 parent 725815b commit 0d1e58d
Show file tree
Hide file tree
Showing 5 changed files with 280 additions and 4 deletions.
107 changes: 107 additions & 0 deletions .github/workflows/delete-cloudrun.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
name: Delete Cloud Run

# TODO
# docs

on:
workflow_call:
inputs:
# <---------- SELECT AUTHENTICATION METHOD --------->
gcp-sa-auth:
required: false
type: boolean
description: "Set to true to enable GCP Service Account Key authentication"
default: false
gcp-oidc-auth:
required: false
type: boolean
description: "Set to true to enable GCP OIDC authentication"
default: false
# <--------------- CLOUD RUN OPTIONS --------------->
cr-service-name:
required: true
type: string
description: "The name of the cloud run service to delete"
cr-region:
required: true
type: string
description: "The GCP region in which to delete the service"
# <-------------------- GENERAL -------------------->
fail-job:
required: false
type: "string"
description: "The exit code to use if service to delete is not found. Setting 1 will produce a job failure."
default: 0
# <------------------ GITHUB ENV ------------------->
gh-env-delete:
required: false
type: boolean
description: "Should the job also attempt to delete the associated environment in Github"
default: true
gh-env-name:
required: false
type: string
description: "The name of the github environment to delete if gh-env-delete is enabled"
secrets:
# <------------------ OIDC AUTH -------------------->
wip:
required: false
description: "The workfload identity provider to use for OIDC auth"
service-account:
required: false
description: "The service account to impersonate when using OIDC auth"
# <----------------- SA KEY AUTH ------------------->
service-account-key:
required: false
description: "The service account key to use for authentication"
# <------------------ CLOUD RUN -------------------->
cr-project-id:
required: true
description: "The GCP project in which to deploy your Cloud Run service"
# <----------------- GITHUB AUTH ------------------->
token:
required: false
description: "The Github token to use for github environment deletion if gh-env-delete is enabled."

jobs:
delete:
# <------------- DELETE CLOUDRUN SERVICE ------------->
name: Delete Cloud Run Service
runs-on: ubuntu-latest
steps:
- name: Authenticate to GCP (SA Key)
if: ${{ inputs.gcp-sa-auth }}
uses: google-github-actions/auth@v0
with:
credentials_json: ${{ secrets.service-account-key }}

- name: Authenticate to GCP (OIDC)
if: ${{ inputs.gcp-oidc-auth }}
uses: google-github-actions/auth@v0
with:
workload_identity_provider: ${{ secrets.wip }}
service_account: ${{ secrets.service-account }}

- name: Delete Service
run: |
if gcloud run services describe --platform managed ${{ inputs.cr-service-name }} --region ${{ inputs.cr-region }} --project ${{ secrets.cr-project-id }}; then
gcloud run services delete --quiet --platform managed ${{ inputs.cr-service-name }} --region ${{ inputs.cr-region }} --project ${{ secrets.cr-project-id }} && \
echo "Cloud Run service ${{ inputs.cr-service-name }} in ${{ inputs.cr-region }} was successfully deleted from ${{ secrets.cr-project-id }}."
else
echo "SERVICE: ${{ env.SERVICE_NAME }} could not be found in ${{ inputs.cr-region }} for ${{ secrets.cr-project-id }}" && exit ${{ inputs.fail-job }}
fi
# <------------- DELETE GITHUB ENVIRONMENT ------------->
delete-gh-env:
name: Delete Github Environment
runs-on: ubuntu-latest
if: ${{ inputs.gh-env-delete }}
steps:
- name: Checkout the code
uses: actions/checkout@v2

- uses: strumwolf/delete-deployment-environment@v2
with:
token: ${{ secrets.token }}
environment: ${{ inputs.gh-env-name }}
continue-on-error: true
11 changes: 7 additions & 4 deletions .github/workflows/deploy-cloudrun.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
name: Deploy Cloud Run

# TODO
# docs

on:
workflow_call:
inputs:
Expand Down Expand Up @@ -42,6 +39,12 @@ on:
type: string
description: "Additional cloud run flags to apply during deployment"
default: "--port 8080 --cpu 1 --memory 1024Mi --timeout 5m --concurrency 80 --min-instances 0 --max-instances 1 --no-allow-unauthenticated"
# <---------- GITHUB ENVIRONMENT OPTIONS ---------->
gh-env-name:
required: false
type: string
description: "The name to give to the environment created in Github"
default: ${{ github.ref_name }}
# <----------- DEPLOYMENT METRICS OPTIONS ---------->
metrics:
required: false
Expand Down Expand Up @@ -106,7 +109,7 @@ jobs:
outputs:
url: ${{ steps.deploy.outputs.url }}
environment:
name: ${{ inputs.cr-service-name }}
name: ${{ inputs.gh-env-name }}
url: ${{ steps.deploy.outputs.url }}
steps:
- name: Notify Deployment Start
Expand Down
2 changes: 2 additions & 0 deletions docs/_sidebar.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
- [Usage (Basic Authentication)](container/container.md)
- [Usage (OIDC Authentication)](container/container-oidc.md)

- [**Delete Cloud Run Workflow**](delete-cloudrun/README.md)

- [**Deploy Cloud Run Workflow**](deploy-cloudrun/README.md)

- [**Go Test Workflow**](go-tests/README.md)
Expand Down
162 changes: 162 additions & 0 deletions docs/delete-cloudrun/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
# Delete Cloud Run Workflow Overview

## Purpose

This workflow simply removes a Cloud Run deployment along with any Github environment created with it. It's most commonly used in branch cleanup context where you wish to delete a preview environment after merge or closing of the branch.

- Deletes the named Cloud Run service
- Supports either OIDC or GCP Service Account key for authentication
- By default the job doesn't fail if it finds no service with the name
- Option (default true) to delete an associated Github environment

## Included Jobs

```mermaid
%%{init: {'theme': 'neutral'}}%%
flowchart LR
subgraph Pre-Requisites
subgraph Mandatory
sa-auth>"GCP Service \n Account Key"]-.-|one_of|oidc-auth>"GPC OIDC \n Auth"]
end
subgraph Optional
github-token>"Github Token"]
end
end
subgraph Jobs
subgraph Deploy to Cloud Run
deletecr{"Delete Cloud \n Run Service"}
deletegh{"Delete Github \n Environment"}
end
end
subgraph Artifacts
none
end
%% dependencies -> Jobs
sa-auth-->deletecr
oidc-auth-->deletecr
github-token-.->deletegh
%% Jobs -> Artifacts
deletecr-->none
deletegh-->none
```

### Delete Cloud Run Service

Uses simple gcloud commands to check if service exists and delete it if found.

**Uses:**
- [google-github-actions/auth@v0](https://github.com/google-github-actions/auth)
- `gcloud run services delete --quiet --platform managed ${{ inputs.cr-service-name }} --region ${{ inputs.cr-region }} --project ${{ secrets.cr-project-id }}`

### Delete Github Environment

Simple job that removes any environment by name from the Github UI.

**Uses:** [strumwolf/delete-deployment-environment@v2](https://github.com/strumwolf/delete-deployment-environment)


## Usage

!> You must select a type of authentication using either `gcp-sa-auth: true` or `gcp-oidc-auth: true`

### Basic (OIDC Authentication)

Essentially the job just needs authentication plus the name & region of the service.

```yaml
delete:
uses: erzz/workflows/.github/workflows/delete-cloudrun.yml@main
with:
gcp-oidc-auth: true
cr-service-name: ${{ github.ref_name }}-${{ github.event.repository.name }}
cr-region: europe-north1
gh-env-name: test-branch1
secrets:
wip: projects/012345678901/locations/global/workloadIdentityPools/github/providers/github
service-account: [email protected]
cr-project-id: my-gcp-project
token: ${{ secrets.GITHUB_PAT }}
```
### Basic (SA Key Authentication)
If using SA Key authentication then replace `gcp-oidc-auth: true` with `gcp-sa-auth: true` and provide the service account's JSON key as a secret

```yaml
delete:
uses: erzz/workflows/.github/workflows/delete-cloudrun.yml@main
with:
gcp-sa-auth: true
cr-service-name: ${{ github.ref_name }}-${{ github.event.repository.name }}
cr-region: europe-north1
gh-env-name: test-branch1
secrets:
service-account-key: ${{ secrets.DEV_GCP_DEPLOY_SA }}
cr-project-id: my-gcp-project
token: ${{ secrets.GITHUB_PAT }}
```

## Secrets

| Input | Required | Details |
| --------------------- | ------------- | --------------------------------------------------------------------------------------------------- |
| `wip` | for OIDC auth | The workload identity provider to use if OIDC Authentication with GCP is required to run unit tests |
| `service-account` | for OIDC auth | The service account to impersonate if OIDC Authentication with GCP is required to run unit tests |
| `service-account-key` | for SA auth | The service account JSON to use if SA JSON key auth with GCP is required to run unit tests |
| `cr-project-id` | true | The GCP project in which to delete your Cloud Run service |
| `token` | false | The Github token to use for github environment deletion if `gh-env-delete` is enabled. |

## Inputs

| Input | Required | Default | Details |
| ----------------- | -------- | -------------- | ----------------------------------------------------------------------------------------------- |
| `gcp-sa-auth` | depends | `false` | Set to `true` to enable GCP Service Account Key authentication |
| `gcp-oidc-auth` | depends | `false` | Set to `true` to enable GCP OIDC authentication |
| `cr-service-name` | true | N/A - Required | The name of the cloud run service to delete |
| `cr-region` | true | N/A - Required | The GCP region in which to delete the service |
| `gh-env-delete` | false | `true` | Should the job also attempt to delete the associated environment in Github |
| `gh-env-name` | false | `""` | The name of the github environment to delete if gh-env-delete is enabled |
| `fail-job` | false | `0` | The exit code to use if service to delete is not found. Setting `1` will produce a job failure. |

## Outputs

None at this time

## Advanced Examples

### Without Github Environment deletion

```yaml
delete:
uses: erzz/workflows/.github/workflows/delete-cloudrun.yml@main
with:
gcp-oidc-auth: true
cr-service-name: ${{ github.ref_name }}-${{ github.event.repository.name }}
cr-region: europe-north1
gh-env-delete: false
secrets:
wip: projects/012345678901/locations/global/workloadIdentityPools/github/providers/github
service-account: [email protected]
cr-project-id: my-gcp-project
```

### Fail if service not found

```yaml
delete:
uses: erzz/workflows/.github/workflows/delete-cloudrun.yml@main
with:
gcp-oidc-auth: true
cr-service-name: ${{ github.ref_name }}-${{ github.event.repository.name }}
cr-region: europe-north1
gh-env-name: test-branch1
fail-job: 1
secrets:
wip: projects/012345678901/locations/global/workloadIdentityPools/github/providers/github
service-account: [email protected]
cr-project-id: my-gcp-project
token: ${{ secrets.GITHUB_PAT }}
```
2 changes: 2 additions & 0 deletions docs/deploy-cloudrun/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ The simplest scenario would produce the following deployment:
- Deployed in Region europe-west3
- 1 CPU, 1Gb RAM, Port 8080, Scale to zero, max 1 instance, timeout 5m and no unauthenticated access
- No deployment metrics
- A github environment created and named after the branch

```yaml
deploy:
Expand Down Expand Up @@ -192,6 +193,7 @@ deploy:
| `cr-region` | true | N/A - Required | Set relative path to your own code climate configuration if `cc-default-config`=`false` |
| `cr-suffix` | false | `""` | Max number of INFO Code Climate findings allowed before forcing a failed result |
| `cr-flags` | false | `--port 8080 --cpu 1 --memory 1024Mi --timeout 5m --concurrency 80 --min-instances 0 --max-instances 1 --no-allow-unauthenticated` | Additional cloud run flags to apply during deployment |
| `gh-env-name` | false | `${{ github.ref_name }}` | The name to give to the environment created in Github |
| `metrics` | false | `false` | Max number of MAJOR Code Climate findings allowed before forcing a failed result |
| `metrics-team` | false | `""` | Max number of CRITICAL Code Climate findings allowed before forcing a failed result |
| `metrics-service` | false | `${{ github.event.repository.name }}` | Max number of BLOCKER Code Climate findings allowed before forcing a failed result |
Expand Down

0 comments on commit 0d1e58d

Please sign in to comment.