Skip to main content

A robust and extensible tool for validating Google Cloud Build YAML configuration files against schema specifications and custom rules.

Project description

Google Cloud Build YAML Validator

A robust and extensible tool for validating Google Cloud Build YAML configuration files against schema specifications and custom rules. By providing a comprehensive set of validation checks and the ability to extend its functionality, this program helps ensure the correctness and consistency of your Cloud Build configuration files, potentially saving time and resources in your CI/CD pipeline

Features

The Cloud Build YAML Validator performs comprehensive checks on your configuration files:

  • YAML Syntax: Ensures the file is a valid YAML document.
  • Schema Compliance: Validates the YAML structure against Cloud Build specifications.
  • Duplicate Step IDs: Identifies duplicate step IDs within the configuration file.
  • Step Dependencies: Verifies that all waitFor references point to valid step IDs.
  • Substitution Variables: Checks for unreferenced substitution variables and ensures they start with an underscore (_).
  • Custom Validations: Easily extendable with additional custom validation rules.

Installation

You can install the Cloud Build YAML Validator using pip, uv, or your preferred Python package manager:

git clone https://github.com/alimasri/google-cloudbuild-yaml-validator
cd google-cloudbuild-yaml-validator
pip install -e .

Usage

Command Line Interface

The validator can be run from the command line with the following syntax:

usage: cloudbuild-validator [-h] [-s SCHEMA] file

positional arguments:
  file                  Path to the content file to validate

options:
  -h, --help            show this help message and exit
  -s SCHEMA, --schema SCHEMA
                        Path to the schema file to validate against

Example

cloudbuild-validator /path/to/cloudbuild.yaml

Programmatic Usage

You can also use the validator as a Python library:

from cloudbuild_validator.core import CloudbuildValidator

validator = CloudbuildValidator(speficifactions_file="/path/to/specifications/file.yaml")
validator.validate_file('/path/to/cloudbuild.yaml')

Specifications

The validator enforces schema specifications for Google Cloud Build YAML configuration files, based on the official Cloud Build documentation. Users can provide a custom schema file using the -s or --schema option. The default schema file is located at src/cloudbuild_validator/data/cloudbuild-specifcations.yaml, which can be used as a reference for creating custom schemas.

By adhering to this schema, users ensure their Cloud Build configuration files are valid and correctly interpreted by the Cloud Build service. Example modifications could include adding organization-specific patterns for image names, environment variables, or other configuration options.

Extending the Validator

Adding New Validations

Method 1: Extending the default validations

The validator automatically discovers and executes all Validator subclasses in the validators.py file. To add a new validation rule:

  1. Create a new class that inherits from cloudbuild_validator.validators.Validator
  2. Implement the validate method

The validate method should accept a dictionary representing the Cloud Build configuration file and raise a cloudbuild_validator.exceptions.CloudBuildValidationError if the validation fails.

Example
class StepIdPrefixValidator(Validator):
    """Ensures that step IDs start with a specific prefix."""
    
    def __init__(self, prefix: str):
        super().__init__()
        self.prefix = prefix

    def validate(self, content: dict) -> None:
        for step in content.get('steps', []):
            step_id = step.get('id', '')
            if not step_id.startswith(self.prefix):
                raise CloudBuildValidationError(f"Step ID '{step_id}' does not start with the expected prefix '{self.prefix}'.")

Method 2: Using the add_validator method

The CloudbuildValidator class provides an add_validator method that allows users to add custom validation rules. This method accepts a Validator subclass and adds it to the list of validators that will be executed during the validation process.

Example
from cloudbuild_validator import CloudbuildValidator
from cloudbuild_validator.validators import Validator

class CustomValidator(Validator):
    def validate(self, content: dict) -> None:
        # Custom validation logic here
        pass

validator = CloudbuildValidator()
validator.add_validator(CustomValidator())

Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

License

This project is distributed under the MIT License. See the LICENSE file for more information.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cloudbuild_validator-0.1.1.tar.gz (28.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cloudbuild_validator-0.1.1-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file cloudbuild_validator-0.1.1.tar.gz.

File metadata

  • Download URL: cloudbuild_validator-0.1.1.tar.gz
  • Upload date:
  • Size: 28.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for cloudbuild_validator-0.1.1.tar.gz
Algorithm Hash digest
SHA256 9e1fdb8489c3a8fc93ea6a5b4a93286856fcff4eff824d76460b37e649b8120d
MD5 770af5d41a045070ab007d95d69a0b52
BLAKE2b-256 6eab0711b7f8b5e09716b8c72853d58a8c954cd1cbe8eddf92a309c64ed3558c

See more details on using hashes here.

Provenance

The following attestation bundles were made for cloudbuild_validator-0.1.1.tar.gz:

Publisher: python-publish.yml on alimasri/google-cloudbuild-yaml-validator

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file cloudbuild_validator-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for cloudbuild_validator-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c9341acdac07be077b7de03c900924d49402dcea6627bb2000b7117626814bfd
MD5 255449410c7c1a5fb100e22c434b697a
BLAKE2b-256 282cb231d31827ea3a058971650aa11b6537448e6abaaf953392af74a32af74d

See more details on using hashes here.

Provenance

The following attestation bundles were made for cloudbuild_validator-0.1.1-py3-none-any.whl:

Publisher: python-publish.yml on alimasri/google-cloudbuild-yaml-validator

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page