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 https://app.valohai.com
- 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()
Create your GitHub Action workflow
Workflows are used to define an automated process for your repository. In our case we define a GitHub workflow that triggers the Valohai fetch action whenever new code is pushed to the main
or master
branches.
- In your project, create a new folder named
.github/
. - Inside
.github/
, create another folder named.github/workflows/
. - In
.github/workflows/
, create a file namedfetch.yml
and define the workflow:
name: Workflow for Fetch to Valohai on pus
on:
push:
branches:
- master
- main
jobs:
build:
name: Fetch to Valohai on push to main/master
runs-on: ubuntu-latest
env:
VH_API_TOKEN: ${{ secrets.VH_API_TOKEN }}
steps:
- name: checkout repo content
uses: actions/checkout@v3
- name: setup python
uses: actions/setup-python@v4
with:
python-version: '3.9'
- name: install python packages
run: |
python -m pip install --upgrade pip
pip install requests
- name: execute fetch script # run fetch.py
run: python ./valohai-fetch-action/fetch.py
Push a new commit
Ta-da! That’s it! Now you can commit your code and the push changes.
git add .github/ *
git commit -m "added github action to trigger vh executions"`
git push
Navigate to your GitHub repository’s Actions
tab to see the new action…in action.
Now, each time you push new code to the main
or master
branches of your GitHub repository, the Valohai fetch action will automatically trigger, ensuring your Valohai project stays up to date with the latest commits.
Comments
0 comments
Please sign in to leave a comment.