Auto-Fetch Repository Changes on Push

Keep Valohai synced with your repository automatically. Every time you push code to GitHub, this action fetches the latest commits to Valohai without manual clicking.

Prerequisites

  • A Valohai project connected to a GitHub repository (public or private)

  • A valid valohai.yaml with at least one step defined

  • The repository branches you want to sync configured in Project Settings → Repository → Branch Configuration

Generate a Valohai API Token

Create an API token to authenticate your GitHub Action:

  1. Click "Hi, <username>!" in the top-right corner

  2. Go to My Profile → Authentication

  3. Click Manage Tokens and scroll to the bottom

  4. Click Generate New Token

  5. Copy the token immediately — it's shown only once

⚠️ Keep your token safe. Store it as a GitHub secret, never commit it to your repository.

Add Token as GitHub Secret

Store your Valohai API token in GitHub:

  1. Go to your GitHub repository

  2. Navigate to Settings → Secrets and variables → Actions

  3. Click New repository secret

    • Name: VH_API_TOKEN

    • Value: Paste your Valohai API token

  4. Click Add secret

Create the Fetch Script

Create a script that calls the Valohai API to fetch repository changes.

Create valohai-fetch-action/fetch.py in your repository root:

import requests
import os

# Authenticate with your Valohai API token
auth_token = os.getenv('VH_API_TOKEN')
headers = {'Authorization': f'Token {auth_token}'}

# Replace with your project ID
# Find it in Project Settings → General, or copy from your project URL
project_id = 'your-project-id-here'

# Fetch all new changes from configured branches
# This syncs commits from branches defined in Project Settings → Repository
fetch_url = f'https://app.valohai.com/api/v0/projects/{project_id}/fetch/'
response = requests.post(fetch_url, headers=headers)
response.raise_for_status()

print('✓ Successfully fetched latest commits to Valohai')

💡 Find your project ID in Project Settings → General or extract it from your project URL: https://app.valohai.com/p/owner/project-name/ → the ID appears in the page details.

Set Up GitHub Action

Create a workflow that runs the fetch script on every push to main.

Create .github/workflows/valohai-fetch.yml:

name: Fetch to Valohai

on:
  push:
    branches:
      - main

jobs:
  fetch:
    runs-on: ubuntu-latest
    
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
      
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.9'
      
      - name: Install dependencies
        run: pip install requests
      
      - name: Fetch commits to Valohai
        env:
          VH_API_TOKEN: ${{ secrets.VH_API_TOKEN }}
        run: python valohai-fetch-action/fetch.py

Verify It Works

Push a commit to your main branch and watch the GitHub Action run:

  1. Make a change to your code

  2. Commit and push to main

  3. Go to Actions tab in GitHub to see the workflow run

  4. Check your Valohai project — the latest commit should appear automatically

The fetch makes your commits visible in Valohai. You can now manually trigger executions from these commits, or chain this action with automated execution creation.

Troubleshooting

"Failed to fetch" error:

  • Verify your VH_API_TOKEN secret is set correctly in GitHub

  • Confirm your project ID matches your Valohai project

  • Check that your repository is connected in Project Settings → Repository

Commits not appearing:

  • Ensure the branch is configured in Project Settings → Repository → Branch Configuration

  • The API fetches all configured branches — verify yours is listed there

Self-hosted Valohai: Replace https://app.valohai.com with your installation URL in fetch.py

Last updated

Was this helpful?