|
9 | 9 | from google.cloud.devtools import cloudbuild_v1 |
10 | 10 |
|
11 | 11 | import settings |
12 | | -from models import App, Target |
| 12 | +from models import App, Target, ScheduledInvocation |
13 | 13 | from utils.alias_engine import AliasEngine |
14 | 14 |
|
15 | 15 | logger = logging.getLogger() |
@@ -109,10 +109,44 @@ def _get_description(self) -> str: |
109 | 109 | _event_str = f'tagged {self._build_setup.deploy_tag}' |
110 | 110 | return f'🦩 Deploy to {self._build_setup.build_pack.target} when {_event_str}' |
111 | 111 |
|
| 112 | + def _add_scheduled_invocation_step(self, scheduled_invocation: ScheduledInvocation, wait_for: str): |
| 113 | + schedule_name = f"{self.app.identifier}--{scheduled_invocation.name}" |
| 114 | + |
| 115 | + auth_params = [] |
| 116 | + if self._build_setup.is_authenticated: |
| 117 | + auth_params = [ |
| 118 | + '--oidc-token-audience', f"{self.app.endpoint}", |
| 119 | + '--oidc-service-account-email', f"{self._substitution.SERVICE_ACCOUNT}", |
| 120 | + ] |
| 121 | + |
| 122 | + scheduler = self._service.make_build_step( |
| 123 | + identifier=f"Schedule {scheduled_invocation.name}", |
| 124 | + name="gcr.io/google.com/cloudsdktool/cloud-sdk", |
| 125 | + entrypoint='gcloud', |
| 126 | + args=[ |
| 127 | + "beta", "scheduler", "jobs", "create", "http", "deploy", f"{schedule_name}", |
| 128 | + '--uri', f"{self.app.endpoint}{scheduled_invocation.path}", |
| 129 | + '--schedule', f'{scheduled_invocation.cron}', |
| 130 | + '--http-method', f'{scheduled_invocation.method}', |
| 131 | + '--headers', f'Content-Type={scheduled_invocation.content_type}', |
| 132 | + '--region', f"{self._substitution.REGION}", |
| 133 | + *auth_params, |
| 134 | + ], |
| 135 | + wait_for=[wait_for], |
| 136 | + ) |
| 137 | + self.steps.append(scheduler) |
| 138 | + |
112 | 139 | async def build(self) -> str: |
113 | 140 | self.init() |
114 | 141 | self._add_steps() |
115 | 142 |
|
| 143 | + last_step_id = self.steps[-1].id |
| 144 | + for scheduled_invocation in self.app.scheduled_invocations: |
| 145 | + self._add_scheduled_invocation_step( |
| 146 | + scheduled_invocation=scheduled_invocation, |
| 147 | + wait_for=last_step_id, |
| 148 | + ) |
| 149 | + |
116 | 150 | event = self.app.repository.as_event( |
117 | 151 | branch_name=self._build_setup.deploy_branch, |
118 | 152 | tag_name=self._build_setup.deploy_tag, |
@@ -329,6 +363,7 @@ def _create_placeholder(self): |
329 | 363 |
|
330 | 364 | @dataclass |
331 | 365 | class CloudFunctionsFactory(BuildTriggerFactory): |
| 366 | + # TODO: setup this <https://cloud.google.com/functions/docs/reference/iam/roles#additional-configuration> |
332 | 367 | def _get_setup_params(self) -> KeyValue: |
333 | 368 | from gcp_pilot.functions import CloudFunctions |
334 | 369 |
|
|
0 commit comments