Пример #1
0
def quickstart():
    """Create and execute a simple Google Cloud Build configuration,
    print the in-progress status and print the completed status."""

    # Authorize the client with Google defaults
    credentials, project_id = google.auth.default()
    client = cloudbuild_v1.services.cloud_build.CloudBuildClient()

    build = cloudbuild_v1.Build()

    # The following build steps will output "hello world"
    # For more information on build configuration, see
    # https://cloud.google.com/build/docs/configuring-builds/create-basic-configuration
    build.steps = [{"name": "ubuntu",
                    "entrypoint": "bash",
                    "args": ["-c", "echo hello world"]}]

    operation = client.create_build(project_id=project_id, build=build)
    # Print the in-progress operation
    print("IN PROGRESS:")
    print(operation.metadata)

    result = operation.result()
    # Print the completed status
    print("RESULT:", result.status)
    def invoke_docker_build_and_push(self, container_image_name):
        project_id = self._google_cloud_options.project
        temp_location = self._google_cloud_options.temp_location
        # google cloud build service expects all the build source file to be
        # compressed into a tarball.
        tarball_path = os.path.join(self._temp_src_dir,
                                    '%s.tgz' % SOURCE_FOLDER)
        self._make_tarfile(tarball_path, self._temp_src_dir)
        _LOGGER.info(
            "Compressed source files for building sdk container at %s" %
            tarball_path)

        container_image_tag = container_image_name.split(':')[-1]
        gcs_location = os.path.join(
            temp_location, '%s-%s.tgz' % (SOURCE_FOLDER, container_image_tag))
        self._upload_to_gcs(tarball_path, gcs_location)

        from google.cloud.devtools import cloudbuild_v1
        client = cloudbuild_v1.CloudBuildClient()
        build = cloudbuild_v1.Build()
        build.steps = []
        step = cloudbuild_v1.BuildStep()
        step.name = 'gcr.io/kaniko-project/executor:latest'
        step.args = ['--destination=' + container_image_name, '--cache=true']
        step.dir = SOURCE_FOLDER

        build.steps.append(step)

        source = cloudbuild_v1.Source()
        source.storage_source = cloudbuild_v1.StorageSource()
        gcs_bucket, gcs_object = self._get_gcs_bucket_and_name(gcs_location)
        source.storage_source.bucket = os.path.join(gcs_bucket)
        source.storage_source.object = gcs_object
        build.source = source
        # TODO(zyichi): make timeout configurable
        build.timeout = Duration().FromSeconds(seconds=1800)

        now = time.time()
        operation = client.create_build(project_id=project_id, build=build)
        _LOGGER.info(
            'Building sdk container with Google Cloud Build, this may '
            'take a few minutes, you may check build log at %s' %
            operation.metadata.build.log_url)

        # block until build finish, if build fails exception will be raised and
        # stops the job submission.
        operation.result()

        _LOGGER.info(
            "Python SDK container pre-build finished in %.2f seconds" %
            (time.time() - now))
        _LOGGER.info("Python SDK container built and pushed as %s." %
                     container_image_name)
Пример #3
0
def trigger_cloud_build(request):
    build = {
        "steps": [{
            "name": "bash",
            "args": ["echo", "Hello Cloud Build"],
        }],
    }
    client = cloudbuild_v1.CloudBuildClient()

    response = client.create_build(
        project_id="PROJECT_ID",
        build=cloudbuild_v1.Build(build),
    )
Пример #4
0
    def _make_trigger(
        self,
        name: str,
        description: str,
        steps: List[cloudbuild_v1.BuildStep],
        event: AnyEventType,
        tags: List[str],
        images: List[str] = None,
        substitutions: Substitutions = None,
        timeout: int = None,
        machine_type: str = cloudbuild_v1.BuildOptions.MachineType.UNSPECIFIED,
    ) -> cloudbuild_v1.BuildTrigger:
        def _get_event_param():
            valid_events = {
                "trigger_template": cloudbuild_v1.RepoSource,
                "github": cloudbuild_v1.GitHubEventsConfig,
            }
            for key, klass in valid_events.items():
                if isinstance(event, klass):
                    return key
            raise exceptions.ValidationError(
                f"Unsupported event type {event.__class__.__name__,}")

        params = {_get_event_param(): event}

        return cloudbuild_v1.BuildTrigger(
            name=name,
            description=description,
            tags=tags,
            build=cloudbuild_v1.Build(
                steps=steps,
                images=images or [],
                timeout=self._as_duration(timeout),
                options=cloudbuild_v1.BuildOptions(
                    machine_type=machine_type, ),
            ),
            substitutions=substitutions.as_dict,
            **params,
        )
Пример #5
0
from google.cloud.devtools import cloudbuild_v1

project_id = "xxx"
build = {
    "steps": [{
        "name": 'gcr.io/cloud-builders/gcloud',
        "args": ["config", "list"],
    }]
}
client = cloudbuild_v1.CloudBuildClient()

# v1.1.0
# response = client.create_build(project_id=project_id, build=build)

# v2.0.0
response = client.create_build(project_id, cloudbuild_v1.Build(build))