Before you begin, make sure you have the following:
-
An existing Valohai project with a valid
valohai.yaml
file that includes at least one step. You can access a samplevalohai.yaml
andtrain.py
script in the tutorial’s sample repository. -
Your project is connected to a GitHub repository, whether it’s public or private.
Generate a Valohai API Token
You’ll need to generate a Valohai API key to authorize your requests with your Valohai projects.
- Go to Valohai.
- Click on the
Hi, <username>!
on the top-right corner. - Go to My Profile -> Authentication.
- Click on Manage Tokens and scroll to the bottom of the page to generate a new token.
- Make a note of the token that pops up on the top of your page. It will be shown only once.
Add a GitHub Repository Secret
Save your Valohai API token as a secret in your repository to access it from your GitHub Actions.
- Go to your GitHub Project.
- Open Settings -> Secrets -> Actions.
- Create a new Repository Secret.
- Name: VH_API_TOKEN
- Value: paste your Valohai API Key
Define the Fetching Action
Set up an automated action that fetches changes to Valohai whenever code is pushed to the main or master branches.
- Create a new folder named
valohai-fetch-action/
in your project’s root directory. - Inside this folder, create a new file named
fetch.py
. In this file, implement a call to the Valohai APIs using your authentication token and project ID. You can find your project’s ID under its Settings-tab.
import requests
import json
import os
# Authenticate yourself with the token.
# Remember to follow your organization's security standards when handling the token.
auth_token = os.getenv('VH_API_TOKEN')
headers = {'authorization': 'token %s' % auth_token}
project_id = 'your-project-id'
# Fetch all new changes from the repository
# https://app.valohai.com/api/docs/#projects-fetch
# This will fetch changes from all the branches that you've defined on the Project->Settings->Repository tab
fetchResponse = requests.post(('https://app.valohai.com/api/v0/projects/{0}/fetch/').format(project_id), data={'id': project_id}, headers=headers)
fetchResponse.raise_for_status()