Use GitHub Actions with Azure ML Studio: train, deploy/publish, monitor

Practicing DatScy
5 min readAug 25, 2023

I recently took the Azure Data Scientist Associate certification exam DP-100, thankfully I passed after about 3–4 months for studying the Microsoft Data Science Learning Path and the Coursera Microsoft Azure Data Scientist Associate Specialization. I highly recommend anyone coming from a Machine Learning or Deep Learning modeling background who wants to learn about deploying models (MLOps) on a cloud platform to take this exam or an equivalent; the exam also includes topics on SQL data ingestion with Azure and Databricks, which is also a very important skill to have in Data Science. Finally, the exam is sort of an initiation to the world of MLOps, because even-though the exam is over you have to keep learning and practicing the skills in order to re-pass the test every year.

One aspect of this Data Science exam experience that I thought was lacking, was doing a complete MLOps workflow using GitHub Actions in addition to the Python SDK. In this post, I go through a complete workflow for: [0] creating a .py training script, [1] creating a .yml script to configure a virtual machine to run the training script on, [2] running the scripts using GitHub Actions instead of with the azureml python SDK.

[Step 0] Create resources using the Azure command line library (azure-cli)

I personally like to make a shell script that creates the needed resources/software items on the Azure service platform, when making the resources you can store the variable names/ids as environmental variables. Resources include the: Resource group, Azure ML studio, Azure Compute Cluster.

az login

export subscription_id=$(az account get-access-token --query subscription | sed 's/"//g')
dotenv set subscription_id $subscription_id

export location=$(echo "global")
dotenv set location $location
# Be aware that the mlworkspacename name needs to be in a special format
# that matches with the naming convention in the github actions demo
# example below (mlops-v2-gha-demo/infrastructure/modules/resource-group/main.tf)
export resourceGroupname=$(echo "rg-mlopslite-ao04prod")
dotenv set resourceGroupname $resourceGroupname

az group create --name $resourceGroupname --location $location
# Be aware that the mlworkspacename name needs to be in a special format
# that matches with the naming convention in the github actions demo
# example below (mlops-v2-gha-demo/infrastructure/modules/aml-workspace/main.tf)
export mlworkspacename=$(echo "mlw-mlopslite-ao04prod")
dotenv set mlworkspacename $mlworkspacename

az ml workspace create -n $mlworkspacename \
--resource-group $resourceGroupname \
--location $location \
--public-network-access Enabled

export tenant_id=$(az account tenant list | jq '.[].tenantId' | tr -d '"')
dotenv set tenant_id $tenant_id
export mlcomputeclustername=$(echo "cpu-cluster")
dotenv set mlcomputeclustername $mlcomputeclustername

az ml compute create --name $mlcomputeclustername \
--resource-group $resourceGroupname \
--workspace-name $mlworkspacename \
--size STANDARD_DS11_V2 \
--min-instances 0 \
--max-instances 2 \
--type AmlCompute

[Step 1] Set the role permissions

export serviceprincipalname=$(echo "github-aml-sp")
dotenv set serviceprincipalname $serviceprincipalname

az ad sp create-for-rbac --name $serviceprincipalname \
--role contributor\
--scopes /subscriptions/$subscription_id/resourceGroups/$resourceGroupname/providers/Microsoft.MachineLearningServices/workspaces/$mlworkspacename \
--sdk-auth

When running this command it will output some credentials in JSON format, that allow GitHub Actions to connect to Azure ML studio.

Next, insert credentials into GitHub (Click on Settings — Under “Secrets and variables” on the left hand side, click on “Actions”) following the instructions at Microsoft’s guide at reference [0].

# Go to the project repository : https://github.com/DevopsPractice7/github_actions 

# Click on Settings - Under "Secrets and variables" on the left hand side,
# click on "Actions"

# Click on "New repository secret"
# Name: AZURE_CREDENTIALS
# Secret: paste the JSON output
# Add Secret

# Click on "New repository secret"
# Name: ARM_CLIENT_ID
# Secret: clientId_JSON_output
# Add Secret

# Click on "New repository secret"
# Name: ARM_CLIENT_SECRET
# Secret: clientSecret_JSON_output
# Add Secret

# Click on "New repository secret"
# Name: ARM_SUBSCRIPTION_ID
# Secret: subscriptionId_JSON_output
# Add Secret

# Click on "New repository secret"
# Name: ARM_TENANT_ID
# Secret: tenantId_JSON_output
# Add Secret

[Step 2] Use the given GitHub Actions template for Azure given in Reference [0]

The direct link for the GitHub Actions template is at https://github.com/Azure/mlops-v2-gha-demo. There are six main files/folders that need to be modified with your resource names, data files, python script files, and yml files defining the virtual machine environment:

  1. config-infra-prod.yml: this file contains the name of the service principle name.
  2. data: this folder contains the .csv data files.
  3. data-science: this folder contains two folders called environment and src. The environment folder contains the computing environment yml file for creating the virtual machine to run the training scripts. The src file contains the .py scripts to train the model. In this post I train and register a model with one python script (training.py), however in the mlops-v2-gha-demo they perform each MLOps step individually including prep.py, train.py, register.py, and evaluate.py.
  4. infrastructure: the folder infrastructure contains TypeScript templates for the naming convention of the machine learning resources that were created in [Step 0]. As mentioned in step 0, you can change the naming convention of your resources using the main.tf files in the respective resource folders.
  5. mlops: the folder contains three .yml files; one for specifying the name of the registered dataset (data.yml), one for specifying the name of the virtual machine specification file (train-env.yml), and one for sequentially running each of the MLOps steps (pipeline.yml).
  6. .github/workflow: the hidden folder contain the github action .yml run script. When you go to the Actions tab in GitHub, you will run this .yml file. In my workflow, I called this .yml file main.yml. Below, is the view that one would see when you are running the script/workflow under the Actions tab.
View on GitHub under Actions of the finished job. Completed working workflow: https://github.com/CodeSolutions2/github_actions .
Azure ML studio view of the finished job.
Expanded view of the Azure ML studio job, as you can see I only performed the train model template specifications in the pipeline.yml file so only this template output is registered.

Summary

I initially found using GitHub Actions very confusing, I had to perform this tutorial several times with out success before finally really understanding. Having a repository program like GitHub automatically run Python SDK commands is really useful in terms of running all the tedious Machine Learning steps of training, evaluating, deploying, and monitoring a deployed model. I hope this information will help demystify GitHub Actions, and help others to get started with using GitHub Actions!

References

[0] Set up MLOps with GitHub: https://learn.microsoft.com/en-us/azure/machine-learning/how-to-setup-mlops-github-azure-ml?view=azureml-api-2&tabs=azure-shell

[1] Easy to follow GitHub Action workflow: https://github.com/Azure/mlops-v2-gha-demo

Happy practicing! 👋

🎁 Donate | 💻 GitHub | 🔔 Subscribe

WRITER at MLearning.ai

--

--

Practicing DatScy

Practicing coding, Data Science, and research ideas. Blog brand: Use logic in a clam space, like a forest, and use reliable Data Science workflows!