Skip to content

Commit

Permalink
feature: option to trigger cloud run cron job on modify (#226)
Browse files Browse the repository at this point in the history
Signed-off-by: Kenny Leung <kleung@chainguard.dev>
  • Loading branch information
k4leung4 authored Apr 1, 2024
1 parent 0adf6ea commit b6c2e7a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
4 changes: 4 additions & 0 deletions modules/cron/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ No requirements.
|------|---------|
| <a name="provider_google"></a> [google](#provider\_google) | n/a |
| <a name="provider_ko"></a> [ko](#provider\_ko) | n/a |
| <a name="provider_null"></a> [null](#provider\_null) | n/a |

## Modules

Expand All @@ -86,6 +87,8 @@ No requirements.
| [google_project_service.cloudscheduler](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/project_service) | resource |
| [google_service_account.delivery](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/service_account) | resource |
| [ko_build.image](https://registry.terraform.io/providers/ko-build/ko/latest/docs/resources/build) | resource |
| [null_resource.exec](https://registry.terraform.io/providers/hashicorp/null/latest/docs/resources/resource) | resource |
| [google_client_config.default](https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/client_config) | data source |
| [google_client_openid_userinfo.me](https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/client_openid_userinfo) | data source |
| [google_project.project](https://registry.terraform.io/providers/hashicorp/google/latest/docs/data-sources/project) | data source |

Expand All @@ -96,6 +99,7 @@ No requirements.
| <a name="input_base_image"></a> [base\_image](#input\_base\_image) | The base image that will be used to build the container image. | `string` | `"cgr.dev/chainguard/static:latest-glibc"` | no |
| <a name="input_cpu"></a> [cpu](#input\_cpu) | The CPU limit for the job. | `string` | `"1000m"` | no |
| <a name="input_env"></a> [env](#input\_env) | A map of custom environment variables (e.g. key=value) | `map` | `{}` | no |
| <a name="input_exec"></a> [exec](#input\_exec) | Execute job on modify. | `bool` | `false` | no |
| <a name="input_execution_environment"></a> [execution\_environment](#input\_execution\_environment) | The execution environment to use for the job. | `string` | `""` | no |
| <a name="input_importpath"></a> [importpath](#input\_importpath) | The import path that contains the cron application. | `string` | n/a | yes |
| <a name="input_invokers"></a> [invokers](#input\_invokers) | List of user emails to grant invoker perimssions to invoke the job. | `list(string)` | `[]` | no |
Expand Down
28 changes: 28 additions & 0 deletions modules/cron/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,34 @@ resource "google_cloud_run_v2_job" "job" {
}
}

data "google_client_config" "default" {}

// Call cloud run api to execute job once.
// https://cloud.google.com/run/docs/execute/jobs#command-line
resource "null_resource" "exec" {
count = var.exec ? 1 : 0

provisioner "local-exec" {
command = join(" ", [
"gcloud",
"--project=${var.project_id}",
"run",
"jobs",
"execute",
google_cloud_run_v2_job.job.name,
"--region=${google_cloud_run_v2_job.job.location}",
"--wait"
])
}

lifecycle {
// Trigger job each time cron job is modified.
replace_triggered_by = [
google_cloud_run_v2_job.job
]
}
}

resource "google_service_account" "delivery" {
project = var.project_id
account_id = "${var.name}-dlv"
Expand Down
6 changes: 6 additions & 0 deletions modules/cron/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,9 @@ variable "invokers" {
type = list(string)
default = []
}

variable "exec" {
description = "Execute job on modify."
type = bool
default = false
}

0 comments on commit b6c2e7a

Please sign in to comment.