肉球でキーボード

MLエンジニアの技術ブログです

GitHub ActionsでPrefectのdeploymentを一括登録する

概要

Prefectを本番運用する場合、Flowの実行設定であるDeploymentをPrefect Cloudに登録する必要があります。

Prefectでワークフローを運用する場合、FlowとDeploymentをGitHubレポジトリでコード管理するケースが多いと思います。

この記事では、レポジトリ管理している複数のDeploymentを、並列でPrefect Cloudに登録するGitHub Actionsを解説します。

コード

github.com

構成

.
├── .github/workflows/deploy_deployment.yaml # deployment登録用 GitHub Actions
│
├── deployments # deploymentをまとめたフォルダ
│   ├── flow1_deployment.py
│   └── flow1_deployment.py
│   
├── flows # flowをまとめたフォルダ
│   ├── flow1.py
│   └── flow2.py
│
└── requirements.txt 

deploymentsフォルダの中にdeploymentファイルをまとめたフォルダ構成を想定します。

deploymentファイルはyaml形式pythonスクリプトのいずれかで記述できますが、今回は以下のようなpythonスクリプトで記述しています。

flowsディレクトリ以下のflow関数を呼び出しています。

prefect-github-actions/deployments/flow1_deployment.py at main · nsakki55/prefect-github-actions · GitHub

from flows.flow1 import hello_flow
from prefect.deployments import Deployment

deployment = Deployment.build_from_flow(
    flow=hello_flow,
    name="flow1-deployment",
)

deployment.apply()

コード

deploymentsフォルダ以下のdeploymentをPrefect Cloudに一括登録するGitHub Actionsです。

name: Run register prefect deployments

on:
  push:
    branches:
      - main

jobs:
  list-deployments:
    name: List prefect deployments
    runs-on: ubuntu-latest
    outputs:
      prefect_deployments: ${{ steps.set-matrix.outputs.deployments }}
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - id: set-matrix
        run: |
            echo "deployments=$(ls deployments/*.py | jq -R -s -c 'split("\n")[:-1]')" >> $GITHUB_OUTPUT

  deploy:
    name: Deploy
    needs: list-deployments
    runs-on: ubuntu-latest
    strategy:
      matrix:
        deployments: ${{ fromJson(needs.list-deployments.outputs.prefect_deployments) }}
    steps:
      - uses: actions/checkout@v3

      - name: Set up Python 3.8
        uses: actions/setup-python@v2
        with:
          python-version: 3.8

      - name: Install Dependencies
        run: |
          pip install pip --upgrade
          pip install -r requirements.txt

      - name: Register deployments to Prefect Cloud
        env:
          PYTHONPATH: :///home/runner/work/prefect-github-actions
          PREFECT_API_KEY: ${{ secrets.PREFECT_API_KEY }}
          PREFECT_API_URL: ${{ secrets.PREFECT_API_URL }}
        run: |
          python ${{ matrix.deployments }}

実行すると、以下のようにdeployments以下のファイルが実行されます。

GitHub Actions実行画面

解説

deployment一覧の取得

  list-deployments:
    name: List prefect deployments
    runs-on: ubuntu-latest
    outputs:
      prefect_deployments: ${{ steps.set-matrix.outputs.deployments }}
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - id: set-matrix
        run: |
            echo "deployments=$(ls deployments/*.py | jq -R -s -c 'split("\n")[:-1]')" >> $GITHUB_OUTPUT

deploymentsフォルダ以下にある *.py ファイル名の一覧を以下の部分で取得しています。

echo "deployments=$(ls deployments/*.py | jq -R -s -c 'split("\n")[:-1]')" >> $GITHUB_OUTPUT

deploymentの一括登録

deploy:
    name: Deploy
    needs: list-deployments
    runs-on: ubuntu-latest
    strategy:
      matrix:
        deployments: ${{ fromJson(needs.list-deployments.outputs.prefect_deployments) }}
    steps:
      - uses: actions/checkout@v3

      - name: Set up Python 3.8
        uses: actions/setup-python@v2
        with:
          python-version: 3.8

      - name: Install Dependencies
        run: |
          pip install pip --upgrade
          pip install -r requirements.txt

      - name: Register deployments to Prefect Cloud
        env:
          PYTHONPATH: :///home/runner/work/prefect-github-actions
          PREFECT_API_KEY: ${{ secrets.PREFECT_API_KEY }}
          PREFECT_API_URL: ${{ secrets.PREFECT_API_URL }}
        run: |
          python ${{ matrix.deployments }}

matrix構文を使って、deploymentを登録するjobを並列で実行します。

deployments: ${{ fromJson(needs.list-deployments.outputs.prefect_deployments) }}

Prefect Cloudへの認証を行うために、GitHub Secretから2つの環境変数を設定しています。

  • PREFECT_API_KEY
  • PREFECT_API_URL
    • Prefect CloudのworkspaceのAPI Endpoint
    • フォーマット: https://api.prefect.cloud/api/accounts/[ACCOUNT-ID]/workspaces/[WORKSPACE-ID]
    • ACCOUNT-ID, WORKSPACE-IDはPrefect Cloudにログインした時のURLから確認できます

      https://app.prefect.cloud/account/[ACCOUNT-ID]/workspaces/[WORKSPACE-ID]

レポジトリ直下で python deployments/flow1_deployment.py のようにdeployments以下のpythonファイルを実行するため、PYTHONPATHの設定を行なっています。

PYTHONPATH: :///home/runner/work/prefect-github-actions

参考