MCPcopy Index your code
hub / github.com/databricks/run-notebook

github.com/databricks/run-notebook @v0.0.3

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.0.3 ↗ · + Follow
57 symbols 130 edges 21 files 0 documented · 0%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

databricks/run-notebook v0

Overview

Given a Databricks notebook and cluster specification, this Action runs the notebook as a one-time Databricks Job run (docs: AWS | Azure | GCP) and awaits its completion:

  • optionally installing libraries on the cluster before running the notebook
  • optionally configuring permissions on the notebook run (e.g. granting other users permission to view results)
  • optionally triggering the Databricks job run with a timeout
  • optionally using a Databricks job run name
  • setting the notebook output, job run ID, and job run page URL as Action output
  • failing if the Databricks job run fails

You can use this Action to trigger code execution on Databricks for CI (e.g. on pull requests) or CD (e.g. on pushes to master).

Prerequisites

To use this Action, you need a Databricks REST API token to trigger notebook execution and await completion. The API token must be associated with a principal with the following permissions: * Cluster permissions (AWS | Azure | GCP): Allow unrestricted cluster creation entitlement, if running the notebook against a new cluster (recommended), or "Can restart" permission, if running the notebook against an existing cluster. * Workspace permissions (AWS | Azure | GCP): * If supplying local-notebook-path with one of the git-commit, git-tag, or git-branch parameters, no workspace permissions are required. However, your principal must have Git integration configured (AWS | Azure | GCP). You can associate git credentials with your principal by creating a git credential entry using your principal's API token. * If supplying the local-notebook-path parameter, "Can manage" permissions on the directory specified by the workspace-temp-dir parameter (the /tmp/databricks-github-actions directory if workspace-temp-dir is unspecified). * If supplying the workspace-notebook-path parameter, "Can read" permissions on the specified notebook.

We recommend that you store the Databricks REST API token in GitHub Actions secrets to pass it into your GitHub Workflow. The following section lists recommended approaches for token creation by cloud.

Note: we recommend that you do not run this Action against workspaces with IP restrictions. GitHub-hosted action runners have a wide range of IP addresses, making it difficult to whitelist.

AWS

For security reasons, we recommend creating and using a Databricks service principal API token. You can create a service principal, grant the Service Principal token usage permissions, and generate an API token on its behalf.

Azure

For security reasons, we recommend using a Databricks service principal AAD token.

Create an Azure Service Principal

Here are two ways that you can create an Azure Service Principal.

The first way is via the Azure Portal UI. See the Azure Databricks documentation. Record the Application (client) Id, Directory (tenant) Id, and client secret values generated by the steps.

The second way is via the Azure CLI. You can follow the instructions below: * Install the Azure CLI * Run az login to authenticate with Azure * Run az ad sp create-for-rbac -n <your-service-principal-name> --sdk-auth --scopes /subscriptions/<azure-subscription-id>/resourceGroups/<resource-group-name> --sdk-auth --role contributor, specifying the subscription and resource group of your Azure Databricks workspace, to create a service principal and client secret.

From the resulting JSON output, record the following values: * clientId: this is the client or application Id of your service principal. * clientSecret: this is the client service of your service princiapl. * tenantId: this is the tenant or directory Id of your service principal.

After you create an Azure Service Principal, you should add it to your Azure Databricks workspace using the SCIM API. Use the client or application Id of your service principal as the applicationId of the service principal in the add-service-principal payload.

Use the Service Principal in your GitHub Workflow

  • Store your service principal credentials into your GitHub repository secrets. The Application (client) Id should be stored as AZURE_SP_APPLICATION_ID, Directory (tenant) Id as AZURE_SP_TENANT_ID, and client secret as AZURE_SP_CLIENT_SECRET.
  • Add the following step at the start of your GitHub workflow. This will create a new AAD token for your Azure Service Principal and save its value in the DATABRICKS_TOKEN environment variable for use in subsequent steps.

yaml - name: Generate AAD Token run: | echo "DATABRICKS_TOKEN=$(curl -X POST -H 'Content-Type: application/x-www-form-urlencoded' \ https://login.microsoftonline.com/${{ secrets.AZURE_SP_TENANT_ID }}/oauth2/v2.0/token \ -d 'client_id=${{ secrets.AZURE_SP_APPLICATION_ID }}' \ -d 'grant_type=client_credentials' \ -d 'scope=2ff814a6-3304-4ab8-85cb-cd0e6f879c1d%2F.default' \ -d 'client_secret=${{ secrets.AZURE_SP_CLIENT_SECRET }}' | jq -r '.access_token')" >> $GITHUB_ENV Notes: * The generated Azure token has a default life span of 60 minutes. If you expect your Databricks notebook to take longer than 60 minutes to finish executing, then you must create a token lifetime policy and attach it to your service principal. * The generated Azure token will work across all workspaces that the Azure Service Principal is added to. You do not need to generate a token for each workspace.

GCP

For security reasons, we recommend inviting a service user to your Databricks workspace and using their API token. You can invite a service user to your workspace, log into the workspace as the service user, and create a personal access token to pass into your GitHub Workflow.

Usage

See action.yml for the latest interface and docs.

(Recommended) Run notebook within a temporary checkout of the current Repo

The workflow below runs a notebook as a one-time job within a temporary repo checkout, enabled by specifying the git-commit, git-branch, or git-tag parameter. You can use this to run notebooks that depend on other notebooks or files (e.g. Python modules in .py files) within the same repo.

name: Run a notebook within its repo on PRs

on:
  pull_request

env:
  DATABRICKS_HOST: https://adb-XXXX.XX.azuredatabricks.net

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checks out the repo
        uses: actions/checkout@v2
      # The step below does the following:
      # 1. Sends a POST request to generate an Azure Active Directory token for an Azure service principal
      # 2. Parses the token from the request response and then saves that in as DATABRICKS_TOKEN in the
      # GitHub enviornment.
      # Note: if the API request fails, the request response json will not have an "access_token" field and
      # the DATABRICKS_TOKEN env variable will be empty.
      - name: Generate and save AAD Token
        run: |
          echo "DATABRICKS_TOKEN=$(curl -X POST -H 'Content-Type: application/x-www-form-urlencoded' \
            https://login.microsoftonline.com/${{ secrets.AZURE_SP_TENANT_ID }}/oauth2/v2.0/token \
            -d 'client_id=${{ secrets.AZURE_SP_APPLICATION_ID }}' \
            -d 'grant_type=client_credentials' \
            -d 'scope=2ff814a6-3304-4ab8-85cb-cd0e6f879c1d%2F.default' \
            -d 'client_secret=${{ secrets.AZURE_SP_CLIENT_SECRET }}' |  jq -r  '.access_token')" >> $GITHUB_ENV
      - name: Trigger model training notebook from PR branch
        uses: databricks/run-notebook@v0
        with:
          local-notebook-path: notebooks/deployments/MainNotebook
          # If the current workflow is triggered from a PR,
          # run notebook code from the PR's head commit, otherwise use github.sha.
          git-commit: ${{ github.event.pull_request.head.sha || github.sha }}
          # The cluster JSON below is for Azure Databricks. On AWS and GCP, set
          # node_type_id to an appropriate node type, e.g. "i3.xlarge" for
          # AWS or "n1-highmem-4" for GCP
          new-cluster-json: >
            {
              "num_workers": 1,
              "spark_version": "10.4.x-scala2.12",
              "node_type_id": "Standard_D3_v2"
            }
          # Grant all users view permission on the notebook results
          access-control-list-json: >
            [
              {
                "group_name": "users",
                "permission_level": "CAN_VIEW"
              }
            ]

Run a self-contained notebook

The workflow below runs a self-contained notebook as a one-time job.

Python library dependencies are declared in the notebook itself using notebook-scoped libraries (AWS | Azure | GCP)

name: Run a notebook in the current repo on PRs

on:
  pull_request

env:
  DATABRICKS_HOST: https://adb-XXXX.XX.azuredatabricks.net

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repo
        uses: actions/checkout@v2
      # The step below does the following:
      # 1. Sends a POST request to generate an Azure Active Directory token for an Azure service principal
      # 2. Parses the token from the request response and then saves that in as DATABRICKS_TOKEN in the
      # GitHub enviornment.
      # Note: if the API request fails, the request response json will not have an "access_token" field and
      # the DATABRICKS_TOKEN env variable will be empty.
      - name: Generate and save AAD Token
        run: |
          echo "DATABRICKS_TOKEN=$(curl -X POST -H 'Content-Type: application/x-www-form-urlencoded' \
            https://login.microsoftonline.com/${{ secrets.AZURE_SP_TENANT_ID }}/oauth2/v2.0/token \
            -d 'client_id=${{ secrets.AZURE_SP_APPLICATION_ID }}' \
            -d 'grant_type=client_credentials' \
            -d 'scope=2ff814a6-3304-4ab8-85cb-cd0e6f879c1d%2F.default' \
            -d 'client_secret=${{ secrets.AZURE_SP_CLIENT_SECRET }}' |  jq -r  '.access_token')" >> $GITHUB_ENV
      - name: Trigger notebook from PR branch
        uses: databricks/run-notebook@v0
        with:
          local-notebook-path: notebooks/MainNotebook.py
          # Alternatively, specify an existing-cluster-id to run against an existing cluster.
          # The cluster JSON below is for Azure Databricks. On AWS and GCP, set
          # node_type_id to an appropriate node type, e.g. "i3.xlarge" for
          # AWS or "n1-highmem-4" for GCP
          new-cluster-json: >
            {
              "num_workers": 1,
              "spark_version": "10.4.x-scala2.12",
              "node_type_id": "Standard_D3_v2"
            }
          # Grant all users view permission on the notebook results, so that they can
          # see the result of our CI notebook 
          access-control-list-json: >
            [
              {
                "group_name": "users",
                "permission_level": "CAN_VIEW"
              }
            ]

Run a notebook using library dependencies in the current repo and on PyPI

In the workflow below, we build Python code in the current repo into a wheel, use upload-dbfs-temp to upload it to a tempfile in DBFS, then run a notebook that depends on the wheel, in addition to other libraries publicly available on PyPI.

Databricks supports a range of library types, including Maven and CRAN. See the docs ([Azure](https://docs.microsoft.com/en-us/azure/databricks/dev-tools/api/lates

Extension points exported contracts — how you extend this code

JobRunOutput (Interface)
(no doc)
packages/common/src/interfaces.ts

Core symbols most depended-on inside this repo

importNotebookIfNeeded
called by 7
packages/main/src/import-tmp-notebook.ts
request
called by 7
packages/common/src/api-client.ts
runAndAwaitNotebook
called by 6
packages/main/src/run-notebook.ts
runMain
called by 3
packages/main/src/run-main.ts
isGitRefSpecified
called by 3
packages/common/src/utils.ts
deleteTmpNotebooks
called by 3
packages/post/src/delete-tmp-notebook.ts
runPost
called by 2
packages/post/src/run-post.ts
getNotebookUploadDirectory
called by 1
packages/main/src/import-tmp-notebook.ts

Shape

Function 43
Method 9
Class 4
Interface 1

Languages

TypeScript100%

Modules by API surface

packages/common/src/utils.ts17 symbols
__tests__/test-utils.ts17 symbols
packages/common/src/api-client.ts10 symbols
packages/main/src/import-tmp-notebook.ts4 symbols
packages/post/src/run-post.ts2 symbols
packages/main/src/run-main.ts2 symbols
packages/post/src/delete-tmp-notebook.ts1 symbols
packages/main/src/run-notebook.ts1 symbols
packages/common/src/request.ts1 symbols
packages/common/src/interfaces.ts1 symbols
__tests__/utils.test.ts1 symbols

For agents

$ claude mcp add run-notebook \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact