|
| 1 | +--- |
| 2 | +title: GitHub Actions for CI/CD |
| 3 | +titleSuffix: Azure Machine Learning |
| 4 | +description: Learn about how to create a GitHub Actions workflow to train a model on Azure Machine Learning |
| 5 | +services: machine-learning |
| 6 | +ms.service: machine-learning |
| 7 | +ms.subservice: core |
| 8 | +author: juliamkm |
| 9 | +ms.author: jukullam |
| 10 | +ms.date: 10/19/2020 |
| 11 | +ms.topic: conceptual |
| 12 | +ms.custom: github-actions-azure |
| 13 | +--- |
| 14 | + |
| 15 | +# Deploy to App Service using GitHub Actions |
| 16 | + |
| 17 | +Get started with [GitHub Actions](https://help.github.com/en/articles/about-github-actions) to train a model on Azure Machine Learning. |
| 18 | + |
| 19 | +## Prerequisites |
| 20 | + |
| 21 | +- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F). |
| 22 | +- A GitHub account. If you don't have one, sign up for [free](https://github.com/join). |
| 23 | + |
| 24 | +## Create repository |
| 25 | + |
| 26 | +Create a new repository off the [ML Ops with GitHub Actions and Azure Machine Learning template](https://github.com/machine-learning-apps/ml-template-azure). |
| 27 | + |
| 28 | +1. Open the [template](https://github.com/machine-learning-apps/ml-template-azure) on GitHub. |
| 29 | +2. Select **Use this template**. |
| 30 | + |
| 31 | + :::image type="content" source="media/how-to-github-actions-machine-learning/gh-actions-use-template.png" alt-text="Select use this template"::: |
| 32 | +3. Create a new repository from the template. Set the repository name to `ml-learning` or a name of your choice. |
| 33 | + |
| 34 | + |
| 35 | +## Workflow file overview |
| 36 | + |
| 37 | +A workflow is defined by a YAML (.yml) file in the `/.github/workflows/` path in your repository. This definition contains the various steps and parameters that make up the workflow. |
| 38 | + |
| 39 | +The file has four sections: |
| 40 | + |
| 41 | +|Section |Tasks | |
| 42 | +|---------|---------| |
| 43 | +|**Authentication** | 1. Define a service principal. <br /> 2. Create a GitHub secret. | |
| 44 | +|**Connect** | 1. Connect to the machine learning workspace. <br /> 2. Connect to a compute target. | |
| 45 | +|**Run** | 1. Submit a training run. | |
| 46 | +|**Deploy** | 1. Register model in Azure Machine Learning registry. 1. Deploy the model. | |
| 47 | + |
| 48 | +## Generate deployment credentials |
| 49 | + |
| 50 | +You can create a [service principal](../active-directory/develop/app-objects-and-service-principals.md#service-principal-object) with the [az ad sp create-for-rbac](/cli/azure/ad/sp?view=azure-cli-latest#az-ad-sp-create-for-rbac&preserve-view=true) command in the [Azure CLI](/cli/azure/). Run this command with [Azure Cloud Shell](https://shell.azure.com/) in the Azure portal or by selecting the **Try it** button. |
| 51 | + |
| 52 | +```azurecli-interactive |
| 53 | +az ad sp create-for-rbac --name "myML" --role contributor \ |
| 54 | + --scopes /subscriptions/<subscription-id>/resourceGroups/<group-name> \ |
| 55 | + --sdk-auth |
| 56 | +``` |
| 57 | + |
| 58 | +In the example above, replace the placeholders with your subscription ID, resource group name, and app name. The output is a JSON object with the role assignment credentials that provide access to your App Service app similar to below. Copy this JSON object for later. |
| 59 | + |
| 60 | +```output |
| 61 | + { |
| 62 | + "clientId": "<GUID>", |
| 63 | + "clientSecret": "<GUID>", |
| 64 | + "subscriptionId": "<GUID>", |
| 65 | + "tenantId": "<GUID>", |
| 66 | + (...) |
| 67 | + } |
| 68 | +``` |
| 69 | + |
| 70 | +## Configure the GitHub secret |
| 71 | + |
| 72 | +1. In [GitHub](https://github.com/), browse your repository, select **Settings > Secrets > Add a new secret**. |
| 73 | + |
| 74 | +2. Paste the entire JSON output from the Azure CLI command into the secret's value field. Give the secret the name `AZURE_CREDENTIALS`. |
| 75 | + |
| 76 | +## Connect to the workspace |
| 77 | + |
| 78 | +Use the `Azure/aml-workspace@v1` action to connect to your Azure Machine Learning workspace. |
| 79 | + |
| 80 | +```yaml |
| 81 | + - name: Connect/Create Azure Machine Learning Workspace |
| 82 | + id: aml_workspace |
| 83 | + uses: Azure/aml-workspace@v1 |
| 84 | + with: |
| 85 | + azure_credentials: ${{ secrets.AZURE_CREDENTIALS }} |
| 86 | +``` |
| 87 | +
|
| 88 | +By default, the action expects a `workspace.json` file. If your JSON file has a different name, you can specify it with the `parameters_file` input parameter. If there is not a file, a new one will be created with the repository name. |
| 89 | + |
| 90 | + |
| 91 | +```yaml |
| 92 | + - name: Connect/Create Azure Machine Learning Workspace |
| 93 | + id: aml_workspace |
| 94 | + uses: Azure/aml-workspace@v1 |
| 95 | + with: |
| 96 | + azure_credentials: ${{ secrets.AZURE_CREDENTIALS }} |
| 97 | + parameters_file: "alternate_workspace.json" |
| 98 | +``` |
| 99 | +The action writes the workspace Azure Resource Manager (ARM) properties to a config file, which will be picked by all future Azure Machine Learning GitHub Actions. The file is saved to `GITHUB_WORKSPACE/aml_arm_config.json`. |
| 100 | + |
| 101 | +## Connect to a Compute Target in Azure Machine Learning |
| 102 | + |
| 103 | +Use the `Azure/aml-compute@v1` action to connect to a compute target in Azure Machine Learning. If the compute target exists, the action will connect to it. Otherwise the action will create a new compute target. The AML Compute action only supports Azure ML Clusters and AKS Clusters. |
| 104 | + |
| 105 | +```yaml |
| 106 | + - name: Connect/Create Azure Machine Learning Compute Target |
| 107 | + id: aml_compute_training |
| 108 | + uses: Azure/aml-compute@v1 |
| 109 | + with: |
| 110 | + azure_credentials: ${{ secrets.AZURE_CREDENTIALS }} |
| 111 | +``` |
| 112 | +## Submit training run |
| 113 | + |
| 114 | +Use the `Azure/aml-run@v1` action to submit a ScriptRun, an Estimator or a Pipeline to Azure Machine Learning. |
| 115 | + |
| 116 | +```yaml |
| 117 | + - name: Submit training run |
| 118 | + id: aml_run |
| 119 | + uses: Azure/aml-run@v1 |
| 120 | + with: |
| 121 | + azure_credentials: ${{ secrets.AZURE_CREDENTIALS }} |
| 122 | +``` |
| 123 | + |
| 124 | +## Register model in registry |
| 125 | + |
| 126 | +Use the `Azure/aml-registermodel@v1` action to register a model to Azure Machine Learning. |
| 127 | + |
| 128 | +```yaml |
| 129 | + - name: Register model |
| 130 | + id: aml_registermodel |
| 131 | + uses: Azure/aml-registermodel@v1 |
| 132 | + with: |
| 133 | + azure_credentials: ${{ secrets.AZURE_CREDENTIALS }} |
| 134 | + run_id: ${{ steps.aml_run.outputs.run_id }} |
| 135 | + experiment_name: ${{ steps.aml_run.outputs.experiment_name }} |
| 136 | +``` |
| 137 | + |
| 138 | +## Deploy model to Azure Machine Learning to ACI |
| 139 | + |
| 140 | +Use the `Azure/aml-deploy@v1` action to deploys a model and create an endpoint for the model. |
| 141 | + |
| 142 | +```yaml |
| 143 | + - name: Deploy model |
| 144 | + id: aml_deploy |
| 145 | + uses: Azure/aml-deploy@v1 |
| 146 | + with: |
| 147 | + azure_credentials: ${{ secrets.AZURE_CREDENTIALS }} |
| 148 | + model_name: ${{ steps.aml_registermodel.outputs.model_name }} |
| 149 | + model_version: ${{ steps.aml_registermodel.outputs.model_version }} |
| 150 | +
|
| 151 | +``` |
| 152 | + |
| 153 | +## Complete example |
| 154 | + |
| 155 | +Train your model and deploy to Azure Machine Learning. |
| 156 | + |
| 157 | +```yaml |
| 158 | +# Actions train a model on Azure Machine Learning |
| 159 | +name: Azure Machine Learning training and deployment |
| 160 | +on: |
| 161 | + push: |
| 162 | + branches: |
| 163 | + - master |
| 164 | + # paths: |
| 165 | + # - 'code/*' |
| 166 | +jobs: |
| 167 | + train: |
| 168 | + runs-on: ubuntu-latest |
| 169 | + steps: |
| 170 | + # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it |
| 171 | + - name: Check Out Repository |
| 172 | + id: checkout_repository |
| 173 | + uses: actions/checkout@v2 |
| 174 | + |
| 175 | + # Connect or Create the Azure Machine Learning Workspace |
| 176 | + - name: Connect/Create Azure Machine Learning Workspace |
| 177 | + id: aml_workspace |
| 178 | + uses: Azure/aml-workspace@v1 |
| 179 | + with: |
| 180 | + azure_credentials: ${{ secrets.AZURE_CREDENTIALS }} |
| 181 | + |
| 182 | + # Connect or Create a Compute Target in Azure Machine Learning |
| 183 | + - name: Connect/Create Azure Machine Learning Compute Target |
| 184 | + id: aml_compute_training |
| 185 | + uses: Azure/aml-compute@v1 |
| 186 | + with: |
| 187 | + azure_credentials: ${{ secrets.AZURE_CREDENTIALS }} |
| 188 | + |
| 189 | + # Submit a training run to the Azure Machine Learning |
| 190 | + - name: Submit training run |
| 191 | + id: aml_run |
| 192 | + uses: Azure/aml-run@v1 |
| 193 | + with: |
| 194 | + azure_credentials: ${{ secrets.AZURE_CREDENTIALS }} |
| 195 | +
|
| 196 | + # Register model in Azure Machine Learning model registry |
| 197 | + - name: Register model |
| 198 | + id: aml_registermodel |
| 199 | + uses: Azure/aml-registermodel@v1 |
| 200 | + with: |
| 201 | + azure_credentials: ${{ secrets.AZURE_CREDENTIALS }} |
| 202 | + run_id: ${{ steps.aml_run.outputs.run_id }} |
| 203 | + experiment_name: ${{ steps.aml_run.outputs.experiment_name }} |
| 204 | +
|
| 205 | + # Deploy model in Azure Machine Learning to ACI |
| 206 | + - name: Deploy model |
| 207 | + id: aml_deploy |
| 208 | + uses: Azure/aml-deploy@v1 |
| 209 | + with: |
| 210 | + azure_credentials: ${{ secrets.AZURE_CREDENTIALS }} |
| 211 | + model_name: ${{ steps.aml_registermodel.outputs.model_name }} |
| 212 | + model_version: ${{ steps.aml_registermodel.outputs.model_version }} |
| 213 | +
|
| 214 | +``` |
| 215 | + |
| 216 | +## Clean up resources |
| 217 | + |
| 218 | +When your resource group and repository are no longer needed, clean up the resources you deployed by deleting the resource group and your GitHub repository. |
| 219 | + |
| 220 | +## Next steps |
| 221 | + |
| 222 | +> [!div class="nextstepaction"] |
| 223 | +> [Create and run machine learning pipelines with Azure Machine Learning SDK](how-to-create-your-first-pipeline.md) |
0 commit comments