Skip to content

Examples

Authenticate with Default Credentials

# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

"""
Example of leveraging default authentication flows
Refer to the authentication section in the README for details:
https://github.com/microsoft/fabric-cicd/tree/main?tab=readme-ov-file#authentication
"""

from fabric_cicd import FabricWorkspace, publish_all_items, unpublish_all_orphan_items

# Sample values for FabricWorkspace parameters
workspace_id = "your-workspace-id"
environment = "your-environment"
repository_directory = "your-repository-directory"
item_type_in_scope = ["Notebook", "DataPipeline", "Environment"]

# Initialize the FabricWorkspace object with the required parameters
target_workspace = FabricWorkspace(
    workspace_id=workspace_id,
    environment=environment,
    repository_directory=repository_directory,
    item_type_in_scope=item_type_in_scope,
)

# Publish all items defined in item_type_in_scope
publish_all_items(target_workspace)

# Unpublish all items defined in item_type_in_scope not found in repository
unpublish_all_orphan_items(target_workspace)

Authenticate with SPN Credentials

# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

"""
Example of authenticating with SPN + Secret
Can be expanded to retrieve values from Key Vault or other sources
"""

from azure.identity import ClientSecretCredential

from fabric_cicd import FabricWorkspace, publish_all_items, unpublish_all_orphan_items

client_id = "your-client-id"
client_secret = "your-client-secret"
tenant_id = "your-tenant-id"
token_credential = ClientSecretCredential(client_id=client_id, client_secret=client_secret, tenant_id=tenant_id)

# Sample values for FabricWorkspace parameters
workspace_id = "your-workspace-id"
environment = "your-environment"
repository_directory = "your-repository-directory"
item_type_in_scope = ["Notebook", "DataPipeline", "Environment"]

# Initialize the FabricWorkspace object with the required parameters
target_workspace = FabricWorkspace(
    workspace_id=workspace_id,
    environment=environment,
    repository_directory=repository_directory,
    item_type_in_scope=item_type_in_scope,
    token_credential=token_credential,
)

# Publish all items defined in item_type_in_scope
publish_all_items(target_workspace)

# Unpublish all items defined in item_type_in_scope not found in repository
unpublish_all_orphan_items(target_workspace)

Setting Variables within Azure DevOps Release

# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

"""
Example to set variables based on the target environment.
Environment is determined based on the branch that produced the build.
"""

import os
from pathlib import Path

from fabric_cicd import FabricWorkspace, publish_all_items, unpublish_all_orphan_items

branch = os.getenv("BUILD_SOURCEBRANCHNAME")

# The defined environment values should match the names found in the parameter.yml file
if branch == "ppe":
    workspace_id = "a2745610-0253-4cf3-9e47-0b5cf8aa00f0"
    environment = "PPE"
elif branch == "main":
    workspace_id = "9010397b-7c0f-4d93-8620-90e51816e9e9"
    environment = "PROD"
else:
    raise ValueError("Invalid branch to deploy from")

# Sample values for FabricWorkspace parameters
repository_directory = "your-repository-directory"
item_type_in_scope = ["Notebook", "DataPipeline", "Environment"]

# Initialize the FabricWorkspace object with the required parameters
target_workspace = FabricWorkspace(
    workspace_id=workspace_id,
    environment=environment,
    repository_directory=repository_directory,
    item_type_in_scope=item_type_in_scope,
)

# Publish all items defined in item_type_in_scope
publish_all_items(target_workspace)

# Unpublish all items defined in item_type_in_scope not found in repository
unpublish_all_orphan_items(target_workspace)

Setting Variables within Local GIT Branch

# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

"""
Example to set variables based on the target environment.
Environment is determined based on the current branch name.
"""

from pathlib import Path

import git  # Depends on pip install gitpython

from fabric_cicd import FabricWorkspace, publish_all_items, unpublish_all_orphan_items

# In this example, this file is being ran in the root/sample directory
root_directory = Path(__file__).resolve().parent.parent
repo = git.Repo(root_directory)
repo.remotes.origin.pull()
branch = repo.active_branch.name

# The defined environment values should match the names found in the parameter.yml file
if branch == "ppe":
    workspace_id = "a2745610-0253-4cf3-9e47-0b5cf8aa00f0"
    environment = "PPE"
elif branch == "main":
    workspace_id = "9010397b-7c0f-4d93-8620-90e51816e9e9"
    environment = "PROD"
else:
    raise ValueError("Invalid branch to deploy from")

# Sample values for FabricWorkspace parameters
repository_directory = "your-repository-directory"
item_type_in_scope = ["Notebook", "DataPipeline", "Environment"]

# Initialize the FabricWorkspace object with the required parameters
target_workspace = FabricWorkspace(
    workspace_id=workspace_id,
    environment=environment,
    repository_directory=repository_directory,
    item_type_in_scope=item_type_in_scope,
)

# Publish all items defined in item_type_in_scope
publish_all_items(target_workspace)

# Unpublish all items defined in item_type_in_scope not found in repository
unpublish_all_orphan_items(target_workspace)

Using Optional Parameters

# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

"""
Example of optional parameters for FabricWorkspace and publish functions.
"""

from fabric_cicd import FabricWorkspace, change_log_level, publish_all_items, unpublish_all_orphan_items

# Sample values for FabricWorkspace parameters
workspace_id = "your-workspace-id"
environment = "your-environment"
repository_directory = "your-repository-directory"
item_type_in_scope = ["Notebook", "DataPipeline", "Environment"]
base_api_url = "https://msitapi.fabric.microsoft.com/"
token_credential = TokenCredential

# Optional: Print all API calls to log file
change_log_level("DEBUG")

# Initialize the FabricWorkspace object with the required and optional parameters
target_workspace = FabricWorkspace(
    workspace_id=workspace_id,
    environment=environment,
    repository_directory=repository_directory,
    item_type_in_scope=item_type_in_scope,
    # Optional: Override base URL in rare cases where it's different
    base_api_url=base_api_url,
    # Optional: Override token credential to use a different authentication
    token_credential=token_credential,
)

# Publish all items defined in item_type_in_scope
publish_all_items(target_workspace)

# Unpublish all items defined in item_type_in_scope not found in repository
unpublish_all_orphan_items(
    target_workspace,
    # Optional: Exclude item names matching the regex pattern
    item_name_exclude_regex=r"^DEBUG.*",
)

Parameter.yml File

find_replace:
    # SQL Connection Guid
    "db52be81-c2b2-4261-84fa-840c67f4bbd0":
        PPE: 81bbb339-8d0b-46e8-bfa6-289a159c0733
        PROD: 5d6a1b16-447f-464a-b959-45d0fed35ca0

spark_pool:
    # CapacityPool_Large
    "72c68dbc-0775-4d59-909d-a47896f4573b":
        type: "Capacity"
        name: "CapacityPool_Large"
    # CapacityPool_Medium
    "e7b8f1c4-4a6e-4b8b-9b2e-8f1e5d6a9c3d":
        type: "Workspace"
        name: "WorkspacePool_Medium"