Astro CLI 技能
何時使用此技能
- 初始化、設定或驗證 Astro/Airflow 專案時。
- 管理本機 Airflow 環境(Docker 或 Standalone 模式)時。
- 與 Astronomer 雲端部署、工作區和部署互動時。
- 透過
astro api airflow包裝器呼叫 Airflow REST API 時。 - 透過檢視日誌或管理環境變數來疑難排解生產環境部署時。
何時不該使用
- 撰寫非特定於 Astronomer CLI 操作的通用 Apache Airflow DAG 時。
先決條件
- 必須安裝 Astro CLI (
astro)。 - Docker 必須正在執行(用於標準本機開發)。
- 必須安裝
uv(用於 Standalone 本機開發)。
核心流程
- 在嘗試執行雲端 API 或部署指令前,請先使用
astro login進行身分驗證。 - 進行本機開發時,使用
astro dev start啟動 Airflow。 - 使用
astro api airflow對本機和雲端環境執行已驗證的 API 請求。
逐步工作流程
專案設定與配置
- 初始化專案:
astro dev init - 在本機驗證 DAG:
astro dev parse - 匯出連線/變數:
astro dev object export --connections --variables - 匯入連線/變數:
astro dev object import --connections --variables --file <path>
管理本機環境
- 啟動 (Docker):
astro dev start - 啟動 (Standalone):
astro dev start --standalone - 停止/終止:
astro dev stop/astro dev kill(kill會移除 volumes/venv) - 檢視本機日誌:
astro dev logs --scheduler(或--webserver、--workers) - 執行 Airflow CLI:
astro dev run dags list - 檢查代理狀態:
astro dev proxy status(用於多專案路由)
本機開發的 Astro CLI 指令
- 取得 Tab 自動完成:
astro completion <shell>(用於取得 Astro CLI 指令的 Tab 自動完成腳本) - 取得註冊表資訊:
astro api registry(從 Airflow 註冊表 API 取得資訊) - 在排程器中執行 bash:
astro dev bash(進入排程器容器以執行 bash 指令) - 移除所有容器:
astro dev kill(移除包括 metab、connections 在內的所有容器) - 取得串流/日誌:
astro dev logs(取得日誌 -s / -d / -t / -a,-f 用於串流) - 檢查匯入錯誤:
astro dev parse(檢查匯入錯誤) - 在容器中執行測試:
astro dev pytest(在容器中的 /tests 目錄執行測試) - 執行 Airflow CLI 指令:
astro dev run(執行任何 Airflow CLI 指令!) - 強制重新解析 DAG:
astro dev run dags reserialize(強制重新解析所有 DAG,包括新加入的) - 使用雲端連線在本機啟動:
astro dev start --deployment-id [deployment-id](適用於 Astro 客戶,使用與您的部署相同的 Airflow 連線在本機啟動)
雲端部署管理
- 切換工作區:
astro workspace switch <WORKSPACE_ID> - 列出/檢查部署:
astro deployment list/astro deployment inspect <DEPLOYMENT_ID> - 建立部署:
astro deployment create --label <NAME> --executor celery - 部署程式碼 (完整):
astro deploy <DEPLOYMENT_ID> - 僅部署 DAG:
astro deploy <DEPLOYMENT_ID> --dags(需要啟用dag-deploy-enabled) - 管理變數:
astro deployment variable create --deployment-id <ID> --key <KEY> --value <VAL> --secret
疑難排解雲端部署
- 檢視部署日誌:
astro deployment logs <DEPLOYMENT_ID> --error - 按元件過濾日誌:
astro deployment logs <ID> --scheduler(或--workers、--triggerer) - 搜尋日誌:
astro deployment logs <ID> --keyword "ImportError"
透過 Astro CLI 執行 Airflow REST API 呼叫
astro api airflow 指令會自動為您目前的上下文處理身分驗證。
範例:
- 列出 DAG:
astro api airflow get_dags - 取得 DAG 詳細資料:
astro api airflow get_dag -p dag_id=<dag_id> - 觸發 DAG 執行:
astro api airflow trigger_dag_run -p dag_id=<dag_id> -F conf[key]=value - 列出任務實例:
astro api airflow get_task_instances -p dag_id=<dag_id> -p dag_run_id=~ - 使用 jq 過濾:
astro api airflow get_dags -q '.dags[].dag_id'
常見陷阱
- 缺少身分驗證:在執行雲端操作前忘記執行
astro login。 - 上下文不符:在錯誤的工作區或針對錯誤的部署執行指令。請務必檢查
astro workspace list或astro deployment list。 - Standalone 版本鎖定:Standalone 模式需要 Airflow 3.x/Runtime 13.x+。
- 互動式指令掛起:避免在非互動式環境中執行像是
astro login這樣的互動式指令,除非使用適當的旗標。
相關技能
- astronomer-docs: 使用 llms.txt 上下文閱讀和瀏覽 Astronomer 文件。
- apache-airflow-api: 執行 Apache Airflow Stable REST API 查詢,管理 DAG、回溯、連線、變數和資產。
- 管理 Astro 本機環境
- 疑難排解 Astro 部署