# 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 href="#prerequisites" id="prerequisites"></a>

* 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 <a href="#generate-a-valohai-api-token" id="generate-a-valohai-api-token"></a>

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 <a href="#add-token-as-github-secret" id="add-token-as-github-secret"></a>

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 <a href="#create-the-fetch-script" id="create-the-fetch-script"></a>

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

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

```python
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 <a href="#set-up-github-action" id="set-up-github-action"></a>

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

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

```yaml
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 <a href="#verify-it-works" id="verify-it-works"></a>

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 <a href="#troubleshooting" id="troubleshooting"></a>

**"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`


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.valohai.com/automation-overview/rest-api/examples/auto-fetch-repository-on-push.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
