Code Samples¶
Authentication¶
The following are the most common authentication flows for fabric-cicd. However, because fabric-cicd supports any TokenCredential, there are multiple authentication methods available beyond the ones described here.
Default Credentials¶
This approach utilizes the default credential flow, meaning no explicit TokenCredential is provided. It is the most common authentication method and is particularly useful when integrating with Azure DevOps. For local development, ensure that you are logged in using either the Azure CLI (az login) or the Azure PowerShell module (Connect-AzAccount).
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)
SPN Credentials¶
This method explicitly utilizes a Service Principal Name (SPN) with a Secret credential flow, leveraging the ClientSecretCredential class. Organizations permitted to use SPN + Secret credentials may also combine this approach with Azure Key Vault to securely retrieve the secret.
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", "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)
Deployment Variables¶
A key concept in CI/CD is defining environment-specific deployment variables. At minimum, a Workspace Id needs to be defined for every branch you intend to deploy from. Additionally, if leveraging the parameter.yml file, a target environment name is also required.
Azure DevOps Release¶
When deploying from an Azure DevOps Release, the BUILD_SOURCEBRANCHNAME
environment variable determines the target environment. This approach is also compatible with composite YAML-based build and release pipelines.
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)
Local GIT Branch¶
When deploying from a local machine, the script identifies the target environment based on the active Git branch. The script fetches the latest updates from the remote repository before determining the branch name. Ensure that the required dependency (gitpython) is installed before running the script.
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)