Skip to content

Deployment Variable Examples

A key concept in CI/CD is defining environment-specific deployment variables. The following are examples of how to inject variables from outside of the python script to handle values that are environment-specific, or common across other tooling. These examples provide starting points that should be adapted for your specific environment and security requirements.

⚠️ DEPRECATION NOTICE: Due to security best practices, the Default Credential (DefaultAzureCredential) authentication method is deprecated and will be removed in a future release. All examples below use explicit credential methods.

Note: All examples below use the AzureCliCredential token for demonstration purposes. You can substitute this with other explicit credential methods based on your environment.

Branch Based

Leverage the following when you have specific values that you need to define per branch you are deploying from.

'''Determines variables based on locally checked out branch'''

from pathlib import Path
import git  # Depends on pip install gitpython
from azure.identity import AzureCliCredential
from fabric_cicd import FabricWorkspace, publish_all_items, unpublish_all_orphan_items

# Use Azure CLI credential to authenticate
token_credential = AzureCliCredential()

# Assumes your script is one level down from root
root_directory = Path(__file__).resolve().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 == "dev":
    workspace_id = "dev-workspace-id"
    environment = "DEV"
elif branch == "main":
    workspace_id = "prod-workspace-id"
    environment = "PROD"
else:
    raise ValueError("Invalid branch to deploy from")

# Sample values for FabricWorkspace parameters
repository_directory = str(root_directory / "your-workspace-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)
'''
Determines variables based on the branch that originated the build
Uses Azure CLI credential with service connection
'''

import sys
import os
from pathlib import Path
from azure.identity import AzureCliCredential
from fabric_cicd import FabricWorkspace, publish_all_items, unpublish_all_orphan_items, change_log_level

# Force unbuffered output like `python -u`
sys.stdout.reconfigure(line_buffering=True, write_through=True)
sys.stderr.reconfigure(line_buffering=True, write_through=True)

# Enable debugging if defined in Azure DevOps pipeline
if os.getenv("SYSTEM_DEBUG", "false").lower() == "true":
    change_log_level("DEBUG")

# Use Azure CLI credential to authenticate
token_credential = AzureCliCredential()

# Assumes your script is one level down from root
root_directory = Path(__file__).resolve().parent

branch = os.getenv("BUILD_SOURCEBRANCHNAME")

# The defined environment values should match the names found in the parameter.yml file
if branch == "dev":
    workspace_id = "dev-workspace-id"
    environment = "DEV"
elif branch == "main":
    workspace_id = "prod-workspace-id"
    environment = "PROD"
else:
    raise ValueError("Invalid branch to deploy from")

# Sample values for FabricWorkspace parameters
repository_directory = str(root_directory / "your-workspace-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)
'''
Determines variables based on the branch that originated the build
Uses Azure CLI credential (requires az login in GitHub Actions workflow)
'''

import os
from pathlib import Path
from azure.identity import AzureCliCredential
from fabric_cicd import FabricWorkspace, publish_all_items, unpublish_all_orphan_items

# Use Azure CLI credential to authenticate
token_credential = AzureCliCredential()

# GitHub Actions sets GITHUB_WORKSPACE automatically
root_directory = Path(os.getenv("GITHUB_WORKSPACE", ".")).resolve()

# Get branch from GitHub environment variable
branch = os.getenv("GITHUB_REF_NAME")

# The defined environment values should match the names found in the parameter.yml file
if branch == "dev":
    workspace_id = "dev-workspace-id"
    environment = "DEV"
elif branch == "main":
    workspace_id = "prod-workspace-id"
    environment = "PROD"
else:
    raise ValueError("Invalid branch to deploy from")

# Sample values for FabricWorkspace parameters
repository_directory = str(root_directory / "your-workspace-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)

Passed Arguments

Leverage the following when you want to pass in variables outside of the python script. This is most common for scenarios where you want to use one py script, but have multiple deployments.

'''Accepts parameters passed into Python during execution'''

import argparse
from azure.identity import AzureCliCredential
from fabric_cicd import FabricWorkspace, publish_all_items, unpublish_all_orphan_items

# Use Azure CLI credential to authenticate
token_credential = AzureCliCredential()

# Accept parsed arguments
parser = argparse.ArgumentParser(description='Process deployment arguments.')
parser.add_argument('--workspace_id', type=str, required=True)
parser.add_argument('--environment', type=str)
parser.add_argument('--repository_directory', type=str, required=True)
parser.add_argument('--items_in_scope', type=str)
args = parser.parse_args()

# Sample values for FabricWorkspace parameters
workspace_id = args.workspace_id
environment = args.environment
repository_directory = args.repository_directory
item_type_in_scope = args.items_in_scope.split(",") if args.items_in_scope else ["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)
'''
Accepts parameters passed into Python during execution
Uses Azure CLI credential with service connection
'''

import sys
import os
import argparse
from pathlib import Path
from azure.identity import AzureCliCredential
from fabric_cicd import FabricWorkspace, publish_all_items, unpublish_all_orphan_items, change_log_level

# Force unbuffered output like `python -u`
sys.stdout.reconfigure(line_buffering=True, write_through=True)
sys.stderr.reconfigure(line_buffering=True, write_through=True)

# Enable debugging if defined in Azure DevOps pipeline
if os.getenv("SYSTEM_DEBUG", "false").lower() == "true":
    change_log_level("DEBUG")

# Use Azure CLI credential to authenticate
token_credential = AzureCliCredential()

# Accept parsed arguments
parser = argparse.ArgumentParser(description='Process deployment arguments.')
parser.add_argument('--workspace_id', type=str, required=True)
parser.add_argument('--environment', type=str)
parser.add_argument('--repository_directory', type=str, required=True)
parser.add_argument('--items_in_scope', type=str)
args = parser.parse_args()

# Sample values for FabricWorkspace parameters
workspace_id = args.workspace_id
environment = args.environment
repository_directory = args.repository_directory
item_type_in_scope = args.items_in_scope.split(",") if args.items_in_scope else ["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)
'''Unconfirmed example at this time, however, the Azure DevOps example is a good starting point'''