databrickslabs / cicd-templates

Manage your Databricks deployments and CI with code.
Other
201 stars 100 forks source link
aws azure azure-devops cd-pipeline ci databricks github-actions gitlab mlops

[DEPRECATED] Databricks Labs CI/CD Templates

This repository provides a template for automated Databricks CI/CD pipeline creation and deployment.

NOTE: This repository is deprecated and provided for maintenance purposes only. Please use the dbx init functionality instead.

Table of Contents

Sample project structure (with GitHub Actions)

.
├── .dbx
│   └── project.json
├── .github
│   └── workflows
│       ├── onpush.yml
│       └── onrelease.yml
├── .gitignore
├── README.md
├── conf
│   ├── deployment.json
│   └── test
│       └── sample.json
├── pytest.ini
├── sample_project
│   ├── __init__.py
│   ├── common.py
│   └── jobs
│       ├── __init__.py
│       └── sample
│           ├── __init__.py
│           └── entrypoint.py
├── setup.py
├── tests
│   ├── integration
│   │   └── sample_test.py
│   └── unit
│       └── sample_test.py
└── unit-requirements.txt

Some explanations regarding structure:

Sample project structure (with Azure DevOps)

.
├── .dbx
│   └── project.json
├── .gitignore
├── README.md
├── azure-pipelines.yml
├── conf
│   ├── deployment.json
│   └── test
│       └── sample.json
├── pytest.ini
├── sample_project_azure_dev_ops
│   ├── __init__.py
│   ├── common.py
│   └── jobs
│       ├── __init__.py
│       └── sample
│           ├── __init__.py
│           └── entrypoint.py
├── setup.py
├── tests
│   ├── integration
│   │   └── sample_test.py
│   └── unit
│       └── sample_test.py
└── unit-requirements.txt

Some explanations regarding structure:

Sample project structure (with GitLab)

.
├── .dbx
│   └── project.json
├── .gitignore
├── README.md
├── .gitlab-ci.yml
├── conf
│   ├── deployment.json
│   └── test
│       └── sample.json
├── pytest.ini
├── sample_project_gitlab
│   ├── __init__.py
│   ├── common.py
│   └── jobs
│       ├── __init__.py
│       └── sample
│           ├── __init__.py
│           └── entrypoint.py
├── setup.py
├── tests
│   ├── integration
│   │   └── sample_test.py
│   └── unit
│       └── sample_test.py
└── unit-requirements.txt

Some explanations regarding structure:

Note on dbx

NOTE:
dbx is a CLI tool for advanced Databricks jobs management. It can be used separately from cicd-templates, and if you would like to preserve your project structure, please refer to dbx documentation on how to use it with customized project structure.

Quickstart

NOTE:
As a prerequisite, you need to install databricks-cli with a configured profile. In this instruction we're based on Databricks Runtime 7.3 LTS ML. If you don't need to use ML libraries, we still recommend to use ML-based version due to %pip magic support.

Local steps

Perform the following actions in your development environment:

Setting up CI/CD pipeline on GitHub Actions

Setting up CI/CD pipeline on Azure DevOps

Setting up CI/CD pipeline on Gitlab

Deployment file structure

A sample deployment file could be found in a generated project.

General file structure could look like this:

{
    "<environment-name>": {
        "jobs": [
            {
                "name": "sample_project-sample",
                "existing_cluster_id": "some-cluster-id", 
                "libraries": [],
                "max_retries": 0,
                "spark_python_task": {
                    "python_file": "sample_project/jobs/sample/entrypoint.py",
                    "parameters": [
                        "--conf-file",
                        "conf/test/sample.json"
                    ]
                }
            }
        ]
    }
}

Per each environment you could describe any amount of jobs. Job description should follow the Databricks Jobs API.

However, there is some advanced behaviour for a dbx deploy.

When you run dbx deploy with a given deployment file (by default it takes the deployment file from conf/deployment.json), the following actions will be performed:

Important thing about referencing is that you can also reference arbitrary local files. This is very handy for python_file section. In the example above, the entrypoint file and the job configuration will be added to the job definition and uploaded to dbfs automatically. No explicit file upload is needed.

Different deployment types

Databricks Jobs API provides two methods for launching a particular workload:

Main logical difference between these methods is that Run Submit API allows to submit a workload directly without creating a job. Therefore, we have two deployment types - one for Run Submit API, and one for Run Now API.

Deployment for Run Submit API

To deploy only the files and not to override the job definitions, do the following:

dbx deploy --files-only

To launch the file-based deployment:

dbx launch --as-run-submit --trace

This type of deployment is handy for working in different branches, and it won't affect the job definition.

Deployment for Run Now API

To deploy files and update the job definitions:

dbx deploy

To launch the file-based deployment:

dbx launch --job=<job-name>

This type of deployment shall be mainly used in automated way during new release. dbx deploy will change the job definition (unless --files-only option is provided).

Troubleshooting

Q: When running dbx deploy I'm getting the following exception json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) and stack trace:

...
  File ".../lib/python3.7/site-packages/dbx/utils/common.py", line 215, in prepare_environment
    experiment = mlflow.get_experiment_by_name(environment_data["workspace_dir"])
...

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

What could be causing it and what is the potential fix?

A:
We've seen this exception when in the profile the host=https://{domain}/?o={orgid} format is used for Azure. It is valid for the databricks cli, but not for the API. If that's the cause, once the "?o={orgid}" suffix is removed, the problem should be gone.

FAQ

Q: I'm using poetry for package management. Is it possible to use poetry together with this template?

A:
Yes, it's also possible, but the library management during cluster execution should be performed via libraries section of job description. You also might need to disable the automatic rebuild for dbx deploy and dbx execute via --no-rebuild option. Finally, the built package should be in wheel format and located in /dist/ directory.

Q: How can I add my Databricks Notebook to the deployment.json, so I can create a job out of it?

A:
Please follow this documentation section and add a notebook task definition into the deployment file.

Q: Is it possible to use dbx for non-Python based projects, for example Scala-based projects?

A:
Yes, it's possible, but the interactive mode dbx execute is not yet supported. However, you can just take the dbx wheel to your Scala-based project and reference your jar files in the deployment file, so the dbx deploy and dbx launch commands be available for you.

Q: I have a lot of interdependent jobs, and using solely JSON seems like a giant code duplication. What could solve this problem?

A:
You can implement any configuration logic and simply write the output into a custom deployment.json file and then pass it via --deployment-file option. As an example, you can generate your configuration using Python script, or Jsonnet.

Q: How can I secure the project environment?

A:
From the state serialization perspective, your code and deployments are stored in two separate storages:

Q: I would like to use self-hosted (private) pypi repository. How can I configure my deployment and CI/CD pipeline?

A:
To set up this scenario, there are some settings to be applied:

Here is a sample for dbx deploy command:

dbx deploy --no-rebuild --no-package

Sample section to libraries configuration:

{
    "pypi": {"package": "my-package-name==1.0.0", "repo": "my-repo.com"}
}

Q: What is the purpose of init_adapter method in SampleJob?

A: This method should be primarily used for adapting configuration for dbx execute based run. By using this method, you can provide an initial configuration in case if --conf-file option is not provided.

Q: I don't like the idea of hosting the host and token variables into ~/.databrickscfg file inside the CI pipeline. How can I make this setup more secure?

A:
dbx now supports environment variables, provided via DATABRICKS_HOST and DATABRICKS_TOKEN. If these variables are defined in env, no ~/.databrickscfg file needed.

Legal Information

This software is provided as-is and is not officially supported by Databricks through customer technical support channels. Support, questions, and feature requests can be communicated through the Issues page of this repo. Please see the legal agreement and understand that issues with the use of this code will not be answered or investigated by Databricks Support.

Feedback

Issues with template? Found a bug? Have a great idea for an addition? Feel free to file an issue.

Contributing

Have a great idea that you want to add? Fork the repo and submit a PR!

Kudos