Important
This repository serves as a demo for how to structure a custom package.
Feel free to copy and adjust to your own needs.
This Python library hosts custom Airflow Operators, Sensors, Notifiers, etc. that are common across [insert company name] data teams. This guide will walk you through how this library works, how to use it in your project, making changes, running tests, and contributing changes.
Table of contents generated using https://derlin.github.io/bitdowntoc.
Since this repository is built only for example purposes, we name it democorp-airflow
and publish a Python package only to TestPyPI. Therefore, you'll need to add TestPyPI as an (extra) index URL. You can install the package using pip:
pip install -i https://test.pypi.org/simple/ democorp-airflow
Or define the package in a requirements.txt file and install using pip install -r requirements.txt
:
--extra-index-url https://test.pypi.org/simple
democorp_airflow
Import example:
from democorp_airflow.operators.example import ExampleOperator
The library is organized as follows:
.
├── .github
│ └── workflows
│ ├── ci.yaml
│ └── publish.yaml
├── democorp_airflow
│ ├── __init__.py
│ ├── hooks/
│ │ ├── __init__.py
│ │ ├── example.py
│ │ └── ...
│ ├── notifiers/
│ │ ├── __init__.py
│ │ └── ...
│ ├── operators/
│ │ ├── __init__.py
│ │ └── ...
│ └── sensors/
│ ├── __init__.py
│ └── ...
├── tests/
│ ├── __init__.py
│ ├── conftest.py
│ └── democorp_airflow
│ ├── hooks/
│ │ ├── __init__.py
│ │ ├── test_example.py
│ │ └── ...
│ ├── notifiers/
│ │ ├── __init__.py
│ │ └── ...
│ ├── operators/
│ │ ├── __init__.py
│ │ └── ...
│ └─ sensors/
│ ├── __init__.py
│ └── ...
├── README.md
├── pyproject.toml
└── setup.py
- CI/CD code is stored in
.github/workflows
- Modules are organized in
democorp_airflow/
according to Airflow components names (hooks
,operators
, etc.) but you're free to name modules fitting your use case - The
tests/
directory contains unit tests for each component, this folder structure matches the package structure (pytests calls this structure tests outside application code) pyproject.toml
is used for package configuration and versioningsetup.py
is used for backwards compatibility of newerpyproject.toml
syntax
We welcome contributions from every team to improve this library! Here's how you can get started:
- Clone the repository to your local machine.
- Create a new branch for your changes:
git checkout -b my-new-feature
.
- Make your desired changes to the library, following the structure and coding guidelines.
- Write unit tests for your changes in the
tests/
directory.
Before submitting your changes, ensure that all tests pass:
pytest tests/
We use setuptools-scm
to automatically manage versioning based on git tags. When you push a new tag, the library version is updated accordingly, and a release for the given tag is made. See .github/workflows/publish.yaml.
The CI/CD pipeline is managed using GitHub Actions:
- On every commit, syntax checks and unit tests are run to ensure code quality. See .github/workflows/ci.yaml.
- When a new tag is pushed, the library is built, published to TestPyPI, and a GitHub release is created. See .github/workflows/publish.yaml.
- Commit your changes and push them to your branch.
- Create a pull request from your branch to the
main
branch. - The CI/CD pipeline will automatically run tests on your pull request.
- Once the tests pass and your code is reviewed/accepted, your contribution will be merged into the main repository.
Once a new version of the democorp-airflow
library is released, you need to update your Airflow project to use it.
- Update the version constraint in your
requirements.txt
file. - After updating the package, it's crucial to test your project to ensure that the new version works as expected and doesn't introduce any compatibility issues or bugs. Run
astro dev start
to test changes locally. - Commit your changes to the
requirements.txt
file to Git so that other developers can easily reproduce your project environment.