HF Cloud
Docs / GitHub Actions

GitHub Actions

Deploy a server or run Terraform from a GitHub Actions workflow with a HF Cloud service token stored as a secret.

1. Create a service token

In the console, API → New token → Acts as → The organization (a service token). Choose:

  • role member to create and change resources (it cannot delete servers), or viewer for read-only jobs;
  • an expiry, so a leaked token stops working by itself;
  • allowed addresses, if your runners have fixed ones (self-hosted runners).

Store it in GitHub: Settings → Secrets and variables → Actions → New repository secret, named HFCLOUD_TOKEN. Never print it in logs.

2. Deploy a server with the API

This workflow creates a server when started by hand and waits until it is ready. The Idempotency-Key is based on the run, so a re-run of the same job never creates a second server.

name: Create server
on:
  workflow_dispatch:
    inputs:
      name:
        description: Server name
        required: true

jobs:
  create:
    runs-on: ubuntu-latest
    env:
      API: https://hfcloud.eu/api/cloud/v1
      HFCLOUD_TOKEN: ${{ secrets.HFCLOUD_TOKEN }}
      NAME: ${{ inputs.name }}   # through the environment, never pasted into the script
    steps:
      - name: Create
        id: create
        run: |
          # ssh_key_ids: an organization SSH key (GET /ssh-keys)
          body=$(jq -n --arg name "$NAME" \
            '{name: $name, image: "ubuntu-24.04", size: 1, ssh_key_ids: ["<ssh-key-uuid>"], labels: {created_by: "github"}}')
          id=$(curl -sf -X POST "$API/servers" \
            -H "Authorization: Bearer $HFCLOUD_TOKEN" -H "Content-Type: application/json" \
            -H "Idempotency-Key: gh-${{ github.run_id }}-${{ github.run_attempt }}" \
            -d "$body" | jq -r .data.id)
          echo "id=$id" >> "$GITHUB_OUTPUT"

      - name: Wait until ready
        run: |
          for i in $(seq 1 120); do
            status=$(curl -sf "$API/servers/${{ steps.create.outputs.id }}" \
              -H "Authorization: Bearer $HFCLOUD_TOKEN" | jq -r .data.status)
            echo "status: $status"
            [ "$status" = ready ] && exit 0
            [ "$status" = failed ] && exit 1
            sleep 5
          done
          exit 1

A service token can use only the organization's SSH keys (Settings → SSH keys → Organization keys) in ssh_key_ids; you can also pass public keys directly in ssh_keys.

3. Run Terraform

Keep the Terraform state in a remote backend (not in the runner) and give the provider the token through the environment:

name: Terraform
on:
  push:
    branches: [main]
  pull_request:

jobs:
  terraform:
    runs-on: ubuntu-latest
    env:
      HFCLOUD_TOKEN: ${{ secrets.HFCLOUD_TOKEN }}
      HFCLOUD_ENDPOINT: https://hfcloud.eu/api/cloud/v1
    steps:
      - uses: actions/checkout@v4
      - uses: hashicorp/setup-terraform@v3

      # Until the provider is in the Terraform Registry: put a build into the filesystem mirror
      # (see the Terraform page), e.g. from a release asset or your own artifact store.
      - name: Install provider
        run: |
          DIR=~/.terraform.d/plugins/registry.terraform.io/humanfrog/hfcloud/0.1.0/linux_amd64
          mkdir -p "$DIR"
          cp tools/terraform-provider-hfcloud "$DIR/terraform-provider-hfcloud_v0.1.0"

      - run: terraform init
      - run: terraform plan -out=plan.tfplan
      - if: github.ref == 'refs/heads/main' && github.event_name == 'push'
        run: terraform apply -auto-approve plan.tfplan

Tips

  • Use concurrency: in the workflow so two runs never change the same servers at once.
  • Rate limits are per token (120 requests per minute); the Terraform provider retries 429 by itself.
  • Use labels (created_by: github, env: staging) to find what CI created: GET /servers?label=created_by=github.
  • Subscribe to server.failed webhooks to hear about failures outside the workflow.