-
Notifications
You must be signed in to change notification settings - Fork 14.5k
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
Support YAML input for CloudBuildCreateOperator #8808
Changes from 1 commit
0c2fa00
500f43c
0d78de0
ddbb766
092dfb6
2643ab5
17029e7
bf2758d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
steps: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I'm afraid this file won't work. Can you make this optional key? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. providing a source is optional for a build config. Should we always check and fail if the body misses a source? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should allow the build object to be passed without sources. |
||
- name: 'ubuntu' | ||
args: ['echo', 'hello world'] | ||
joppevos marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,9 +18,11 @@ | |
"""Operators that integrat with Google Cloud Build service.""" | ||
import re | ||
from copy import deepcopy | ||
from typing import Any, Dict, Iterable, Optional | ||
from typing import Any, Dict, Iterable, Optional, Union | ||
from urllib.parse import unquote, urlparse | ||
|
||
import yaml | ||
|
||
from airflow.exceptions import AirflowException | ||
from airflow.models import BaseOperator | ||
from airflow.providers.google.cloud.hooks.cloud_build import CloudBuildHook | ||
|
@@ -39,7 +41,7 @@ class BuildProcessor: | |
* It is possible to provide the source as the URL address instead dict. | ||
|
||
:param body: The request body. | ||
See: https://cloud.google.com/cloud-build/docs/api/reference/rest/Shared.Types/Build | ||
See: https://cloud.google.com/cloud-build/docs/api/reference/rest/v1/projects.builds | ||
:type body: dict | ||
""" | ||
def __init__(self, body: Dict) -> None: | ||
|
@@ -83,13 +85,29 @@ def _reformat_storage_source(self): | |
|
||
self.body["source"]["storageSource"] = self._convert_storage_url_to_dict(source) | ||
|
||
def _load_body_to_dict(self): | ||
""" | ||
:param file: | ||
file path to YAML build config | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you also add JSON file support? This is not common, but build can also be defined as JSON files.
https://cloud.google.com/cloud-build/docs/build-config#structure_of_a_build_config_file |
||
:return: dict | ||
""" | ||
try: | ||
with open(self.body, 'r') as f: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think, we should use template_ext to load file. This will cause us to still have text instead of the dictionary, so we need to overwrite the prepare_template method to convert the string to a dictionary |
||
self.body = yaml.safe_load(f) | ||
except yaml.YAMLError as e: | ||
raise AirflowException("Exception when loading resource definition: %s\n" % e) | ||
|
||
def process_body(self): | ||
""" | ||
Processes the body passed in the constructor | ||
|
||
:return: the body. | ||
:type: dict | ||
""" | ||
|
||
if isinstance(self.body, str): | ||
self._load_body_to_dict() | ||
return self.body | ||
self._verify_source() | ||
self._reformat_source() | ||
return self.body | ||
|
@@ -178,7 +196,7 @@ class CloudBuildCreateOperator(BaseOperator): | |
|
||
@apply_defaults | ||
def __init__(self, | ||
body: dict, | ||
body: Union[dict, str], | ||
joppevos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
project_id: Optional[str] = None, | ||
gcp_conn_id: str = "google_cloud_default", | ||
api_version: str = "v1", | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add this example to guide? https://airflow.readthedocs.io/en/latest/howto/operator/gcp/cloud_build.html