使用 GitHub Actions 進行模型定型

已完成

GitHub Actions 是一個平台,可讓您將 GitHub 存放庫內發生事件所觸發的工作自動化。 GitHub Actions 工作流程是由作業所組成。 作業會將您可以定義的一組步驟群組在一起。 其中一個步驟可以使用 CLI (v2) 執行 Azure Machine Learning 作業來定型模型。

若要使用 GitHub Actions 將模型定型自動化,您必須:

  • 使用 Azure CLI 建立服務主體。
  • 將 Azure 認證儲存在 GitHub 祕密中。
  • 在 YAML 中定義 GitHub 動作。

建立服務主體

當您使用 GitHub Actions 將 Azure Machine Learning 作業自動化時,您必須使用服務主體來驗證 GitHub,以管理 Azure Machine Learning 工作區。 例如,若要使用 Azure Machine Learning 計算來定型模型,您必須獲得授權才能使用該計算。

提示

深入了解如何使用 GitHub Actions 連線到 Azure

儲存 Azure 認證

您需要驗證的 Azure 認證不應該儲存在您的程式碼或純文字中,而應該儲存在 GitHub 祕密中。

若要將祕密新增至 GitHub 存放庫:

  1. 瀏覽至 [設定] 索引標籤。

    GitHub 存放庫中 [設定] 索引標籤的螢幕擷取畫面。

  2. 在 [設定] 索引標籤的 [安全性] 下,展開 [祕密] 選項,然後選取 [Actions]

    [安全性] 區段中 [祕密] 選項的螢幕擷取畫面。

  3. 輸入您的 Azure 認證作為祕密,並將祕密命名為 AZURE_CREDENTIALS

  4. 若要在 GitHub 動作中使用包含 Azure 認證的祕密,請參閱 YAML 檔案中的秘密。

    on: [push]
    
    name: Azure Login Sample
    
    jobs:
      build-and-deploy:
        runs-on: ubuntu-latest
        steps:
          - name: Log in with Azure
            uses: azure/login@v1
            with:
              creds: '${{secrets.AZURE_CREDENTIALS}}'
    

定義 GitHub 動作

若要定義工作流程,您必須建立 YAML 檔案。 您可以觸發工作流程,手動或使用推送事件來定型模型。 手動觸發工作流程適合測試,而使用事件將其自動化則比較適合自動化。

若要設定 GitHub Actions 工作流程,以便手動觸發工作流程,請使用 on: workflow_dispatch。 若要使用推送事件觸發工作流程,請使用 on: [push]

觸發 GitHub Actions 工作流程之後,您可以將各種步驟新增至作業。 例如,您可以使用一個執行 Azure Machine Learning 作業的步驟:

name: Manually trigger an Azure Machine Learning job

on:
  workflow_dispatch:

jobs:
  train-model:
    runs-on: ubuntu-latest
    steps:
    - name: Trigger Azure Machine Learning job
      run: |
        az ml job create --file src/job.yml