← Back to Blog

Building Custom LangChain Tools for CI/CD Pipeline Interrogation



Building Custom LangChain Tools for CI/CD Pipeline Interrogation

Handling Failed Deployments

When a GitHub Actions workflow fails across a microservices architecture, debugging typically requires switching between execution logs, commit histories, and build artifacts. Standard observability alerts signal that a build has broken, but an AI agent equipped with custom tool capabilities can retrieve specific log files and accurately pinpoint the root cause. The primary objective is to supply clear, deterministic access to CI/CD APIs so that the language model avoids hallucinating commands or requesting missing data.

Designing the Pipeline Interrogator Tool

To grant an LLM direct access to build logs, we can extend LangChain's BaseTool. This tool queries the GitHub Actions API for workflow run logs, parses the failed step, and extracts relevant tracebacks. Using Pydantic for input validation ensures parameters remain well-formed requiring an explicit repository name and run ID before issuing requests.

from langchain.tools import BaseTool
from pydantic import BaseModel, Field
import requests
import re

class GitHubActionLogInput(BaseModel): repo: str = Field(..., description="The repository name in format owner/repo") run_id: int = Field(..., description="The unique ID of the failed GitHub Action run")

class GitHubActionLogTool(BaseTool): name = "fetch_failed_action_logs" description = "Fetches and extracts the error traceback from a failed GitHub Actions run." args_schema = GitHubActionLogInput github_token: str

def _run(self, repo: str, run_id: int) -> str: # Line 1: Construct the API endpoint for workflow run logs url = f"https://api.github.com/repos/{repo}/actions/runs/{run_id}/logs"

# Line 2: Set authorization and accept headers for zip download headers = { "Authorization": f"Bearer {self.github_token}", "Accept": "application/vnd.github+json" }

# Line 3: Execute request, handling redirects for log archives response = requests.get(url, headers=headers, allow_redirects=True) if response.status_code != 200: return f"Error fetching logs: HTTP {response.status_code}"

# Line 4: Extract the core error traceback using regex over the raw logs raw_logs = response.text error_pattern = re.compile(r'(?i)(error|exception|failed|traceback).*?(?=\n\n|\Z)', re.DOTALL) match = error_pattern.search(raw_logs)

# Line 5: Return the isolated context to the agent, avoiding token limits return match.group(0) if match else "No explicit error traceback found in logs."

Code Breakdown & Implementation Choices

  • Lines 1 & 2: We target GitHub's /logs endpoint with the appropriate Accept header to signal a request for log archives. The API token is passed during tool initialization rather than embedding sensitive credentials in model prompts.
  • Line 3: allow_redirects=True is mandatory because GitHub redirects log download requests (302) to signed Amazon S3 URLs. Disabling redirect handling yields an empty response.
  • Line 4: Build logs often exceed context limits (100k+ tokens for extensive build suites). Rather than inserting raw logs into the prompt, regular expressions extract and isolate the primary error traceback.
  • Line 5: Returning only the relevant error snippet keeps prompt size low and allows the model to concentrate on diagnosing the issue, such as a missing dependency or failing unit test.

Combining API retrieval with targeted regex filtering enables custom tools to supply models with precise context without exceeding token constraints or requiring manual log extraction during incidents.