The package module provides functions for defining packages in Ocuroot.

Allowed in

  • Main thread of package.ocu.star

Usage

Load the module:

load("github.com/ocuroot/sdk/v0/package.star", "package")

Example

def build(ctx):
    # Build package artifacts
    alias = "build-{}".format(ctx.build.sequence)  # Set alias based on sequence number
    ctx.build.set_alias(alias)
    
    return {
        "outputs": {
            "image": "my-app:{}".format(ctx.build.id),  # Tag image with build ID
            "config": "config.yaml"
        }
    }

def deploy(ctx):
    # Deploy the package
    image = ctx.build.attributes.get("image")
    config = ctx.build.attributes.get("config")
    
    # Deployment logic here
    ctx.deploy.outputs["url"] = "https://my-app.example.com"
    ctx.deploy.outputs_secret["api_key"] = "secret-key"

def destroy(ctx):
    # Cleanup logic here
    print("Cleaning up resources...")

def policy(ctx):
    # Define deployment rules
    if ctx.environment.name == "prod":
        return policy.later("Production deployments require approval")
    return policy.ready()

def task1(ctx):
    # Custom task logic
    print("Running task1 for build {} in {}".format(ctx.build.id, ctx.environment.name))

# Define the package
package(
    name="my-app",
    build=build,
    policy=policy,
    deploy=deploy,
    destroy=destroy,
    tasks={
        "task1": task1,
    }
)

## API Reference

### package(name, build=_default_build, policy=_default_policy, deploy=_default_deploy, destroy=_default_destroy, tasks={})
Define a package to manage builds and deployments for the code within this directory.

**Arguments**
- `name`: Unique package name within the repository
- `build`: Function defining the build process
  - Takes a build context (`ctx`) argument with:
    - `build`: Build information
      - `id`: Build ID (string)
      - `sequence`: Build sequence number (integer)
      - `created`: Build creation timestamp (integer)
      - `attributes`: Build attributes dictionary
      - `set_alias(alias)`: Set build alias
    - `commit`: Commit information
      - `message`: Commit message
      - `ref`: Git reference
      - `hash`: Commit hash
      - `clean`: Boolean indicating if working directory is clean
  - Returns a result object with outputs
- `policy`: Function defining deployment rules
  - Takes a policy context (`ctx`) argument with:
    - `build`: Build information
      - `id`: Build ID (string)
      - `alias`: Build alias (string)
      - `sequence`: Build sequence number (integer)
      - `created`: Build creation timestamp (integer)
      - `attributes`: Build attributes dictionary
      - `annotations`: Build annotations dictionary
    - `environment`: Environment information
      - `name`: Environment name (string)
      - `attributes`: Environment attributes dictionary
  - Returns a policy result object
- `deploy`: Function defining the deployment process
  - Takes a deploy context (`ctx`) argument with:
    - `deploy`: Deployment information
      - `id`: Deploy ID (string)
      - `inputs`: Input parameters dictionary
      - `inputs_secret`: Secret input parameters dictionary
      - `outputs`: Output parameters dictionary
      - `outputs_secret`: Secret output parameters dictionary
    - `build`: Build information (same as policy context)
    - `environment`: Environment information (same as policy context)
  - Returns a result object with outputs
- `destroy`: Function defining the cleanup process
  - Takes a deploy context (`ctx`) argument (same structure as deploy function)
  - No return value
- `tasks`: Dictionary mapping task names to functions
  - Each function takes a task context (`ctx`) argument with:
    - `build`: Build information (same as policy context)
    - `environment`: Environment information (same as policy context)
  - Returns a result object

## Stubs

```python
def package(name, build=_default_build, policy=_default_policy, deploy=_default_deploy, destroy=_default_destroy, tasks={}):
    """
    Define a package to manage builds and deployments for the code within this directory.

    Args:
        name: The name of the package. Must be unique within this repository.
        build: A function defining the build process for the package. Accepts a build context.
        policy: A function defining the rules for deploying this package. Accepts a policy context.
        deploy: A function defining the deploy process for the package. Accepts a deploy context.
        destroy: A function defining the destroy process for the package. Accepts a deploy context.
        tasks: A map of task names to functions. Each task function accepts a task context.
    """
    pass