Effortless Automation: Prompting Your Way to Deployment with Claude Code and DeployHQ

AI, Devops & Infrastructure, Python, and Tutorials

Effortless Automation: Prompting Your Way to Deployment with Claude Code and DeployHQ

In the fast-paced world of software development, automation is key to efficiency. Imagine a workflow where you simply describe a task, and an AI not only writes the code but also helps you integrate it into your deployment pipeline. This isn't science fiction; it's the power of combining Claude Code with the DeployHQ OpenAPI.

This tutorial will guide you step-by-step on how to leverage this powerful duo to automate tasks and streamline your deployments, just by prompting.


Understanding the Tools

Before we dive into the tutorial, let's quickly re-familiarize ourselves with our key players:

  • Claude Code: An AI coding assistant from Anthropic, designed to understand your entire codebase. You can interact with it in your terminal or IDE, prompting it to generate, refactor, and debug code, or even create pull requests. Think of it as an intelligent coding partner you direct with high-level instructions.

  • DeployHQ OpenAPI: DeployHQ is a robust platform for automated code deployments. Its OpenAPI (Application Programming Interface) allows you to interact with DeployHQ programmatically. This means you can write scripts or use tools to trigger deployments, fetch status updates, and manage your projects without needing to log into the DeployHQ interface. The official documentation can be found at https://api.deployhq.com/docs.


Step 1: Basic Claude Code Setup

To get started with Claude Code, you'll need to install it. The recommended method is via npm.

  1. Install Claude Code: Open your terminal and run the following command:

    npm install -g @anthropic-ai/claude-code  
    

    Note: Avoid using sudo with this command to prevent permission and security issues.

  2. Verify Installation: After the installation is complete, you can run a diagnostic command to check your setup:

    claude doctor
    
  3. Start Coding: Navigate to your project directory and start a new session by simply running the claude command:

    cd your-project-directory
    claude
    

For more detailed installation instructions, including alternative methods and Windows-specific steps, please refer to the official Claude Code setup documentation.


Step 2: Setting up DeployHQ for API Access

To interact with DeployHQ via its OpenAPI, you'll need an API key.

  1. Generate a DeployHQ API Key:
  • Log in to your DeployHQ account.
  • Navigate to your Account Settings.
  • Find the API Keys section and click to generate a new key. Make sure to give it a descriptive name and enable the necessary permissions (e.g., Read Projects, Initiate Deployments).
  • Securely store this API key. Treat it like a password; it grants access to your DeployHQ account.
  1. Identify Project and Server IDs:
  • You'll need the Project ID and Server ID for the specific project and server you want to deploy to.
  • You can usually find these in the URL when viewing your project or server settings in DeployHQ (e.g., https://deployhq.com/projects/<PROJECT_ID>/servers/<SERVER_ID>/edit).

Step 3: Prompting Claude Code to Create a Deployment Script

Now for the exciting part! We'll use Claude Code to write a Python script that triggers a DeployHQ deployment. Let's assume you have a project and want to deploy it to a specific server.

  1. Open your Terminal/IDE: Navigate to your project directory.
  2. Invoke Claude Code: Start an interaction with Claude Code. For a default setup, you'd type: claude
  3. Provide the Prompt: Clearly instruct Claude Code on what you want. Be specific about the API you're targeting.

User Prompt:

Write a Python script that triggers a deployment for a specific project and server using the DeployHQ OpenAPI. I need to provide the DeployHQ API key, project ID, and server ID. The API endpoint for triggering a deployment is https://api.deployhq.com/v1/projects/{project_id}/servers/{server_id}/deployments. Use the requests library for the HTTP POST request and include the API key in the Authorization header as a Bearer token. Add placeholders for the actual values.

Claude Code will then generate a Python script similar to this:

import requests
import os

# Configuration (replace with your actual values or environment variables)
DEPLOYHQ_API_KEY = os.environ.get("DEPLOYHQ_API_KEY", "YOUR_DEPLOYHQ_API_KEY")
PROJECT_ID = os.environ.get("DEPLOYHQ_PROJECT_ID", "YOUR_PROJECT_ID")
SERVER_ID = os.environ.get("DEPLOYHQ_SERVER_ID", "YOUR_SERVER_ID")

if DEPLOYHQ_API_KEY == "YOUR_DEPLOYHQ_API_KEY" or \
   PROJECT_ID == "YOUR_PROJECT_ID" or \
   SERVER_ID == "YOUR_SERVER_ID":
    print("Please set DEPLOYHQ_API_KEY, DEPLOYHQ_PROJECT_ID, and DEPLOYHQ_SERVER_ID "
          "environment variables or replace placeholders in the script.")
    exit(1)

DEPLOYMENT_URL = f"https://api.deployhq.com/v1/projects/{PROJECT_ID}/servers/{SERVER_ID}/deployments"

headers = {
    "Authorization": f"Bearer {DEPLOYHQ_API_KEY}",
    "Content-Type": "application/json"
}

print(f"Attempting to trigger deployment for Project ID: {PROJECT_ID}, Server ID: {SERVER_ID}")

try:
    response = requests.post(DEPLOYMENT_URL, headers=headers)
    response.raise_for_status()  # Raise an HTTPError for bad responses (4xx or 5xx)

    print("Deployment triggered successfully!")
    print("Response:")
    print(response.json())

except requests.exceptions.HTTPError as errh:
    print(f"HTTP Error: {errh}")
    print(f"Response content: {errh.response.text}")
except requests.exceptions.ConnectionError as errc:
    print(f"Error Connecting: {errc}")
except requests.exceptions.Timeout as errt:
    print(f"Timeout Error: {errt}")
except requests.exceptions.RequestException as err:
    print(f"Something went wrong: {err}")

Step 4: Customizing and Running Your Script

  1. Save the Script: Save the generated code as a Python file (e.g., deploy_script.py).
  2. Install Dependencies: Make sure you have the requests library installed:

    pip install requests
    
  3. Set Environment Variables: Crucially, set your DeployHQ API key, project ID, and server ID as environment variables before running the script. This is the most secure way to handle credentials.

    export DEPLOYHQ_API_KEY="your_actual_deployhq_api_key"
    export DEPLOYHQ_PROJECT_ID="your_actual_project_id"
    export DEPLOYHQ_SERVER_ID="your_actual_server_id"
    

    (Remember to replace the placeholders with your actual values!)

  4. Run the Script: Execute the Python script:

    python deploy_script.py  
    

    If successful, you'll see a message indicating the deployment was triggered, along with the API response. You can then check your DeployHQ dashboard to see the deployment in progress.


Expanding Your Automation Horizons

This is just the beginning! You can prompt Claude Code to create scripts for many other DeployHQ API functionalities:

  • Check Deployment Status: Ask for a script to retrieve the status of a specific deployment (GET /v1/projects/{project_id}/servers/{server_id}/deployments/{deployment_id}).
  • List Projects/Servers: Generate scripts to list all your projects or servers.
  • Rollback Deployment: Even prompt for a script to trigger a rollback (though use with extreme caution!).

You can integrate these scripts into your CI/CD pipelines (e.g., GitHub Actions, GitLab CI) to create a truly hands-off development and deployment experience. Imagine an AI writing a new feature, creating a pull request, and once merged, automatically triggering its deployment through a script it helped you create!


Conclusion

By combining the intelligent coding capabilities of Claude Code with the robust automation of DeployHQ OpenAPI, you can dramatically enhance your development workflow. No longer just a manual task, deployments become an easily prompted, automated step in your CI/CD pipeline. Start prompting, start automating, and deploy with confidence!

A little bit about the author

Facundo | CTO | DeployHQ | Continuous Delivery & Software Engineering Leadership - As CTO at DeployHQ, Facundo leads the software engineering team, driving innovation in continuous delivery. Outside of work, he enjoys cycling and nature, accompanied by Bono 🐶.