Skip to main content

automation.soubiran.dev

automation.soubiran.dev is an internal service that helps me automate various tasks with Cloudflare Workflows.

There is no public access or frontend interface. The endpoint only receives calls and triggers workflows.

Development

The service runs on a Cloudflare Worker that receives HTTP requests from my local CLI tool. Each endpoint triggers a different Cloudflare Workflow.

For example, I have a workflow that I use to remind myself to publish scheduled tweets.

src/index.ts
import type { WorkflowEvent, WorkflowStep } from 'cloudflare:workers'
import { WorkflowEntrypoint } from 'cloudflare:workers'

export default {
  async fetch(request: Request, env: Env): Promise<Response> {
    const body = await request.json<{ scheduleAt?: number, content?: string }>()

    await env.AUTOMATION_WORKFLOW.create({
      params: {
        scheduleAt: body.scheduleAt,
        content: body.content,
      },
    })

    return new Response('Scheduled', { status: 201 })
  },
}

export class Automation extends WorkflowEntrypoint<Env, any> {
  async run(event: WorkflowEvent<any>, step: WorkflowStep) {
    await step.sleepUntil('trigger time', event.payload.scheduleAt)

    await step.do('trigger action', async () => {
      await fetch(this.env.DISCORD_WEBHOOK_URL, {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({ content: event.payload.content }),
      })
    })
  }
}

!NOTE The code is simplified for clarity. The actual implementation includes error handling, logging, and other production-ready features.

The worker itself does not authenticate requests, even though it is exposed to the public internet. Instead, I rely on Cloudflare One with a service token to ensure that only my CLI tool can call the endpoints.

This keeps authentication outside the worker and prevents unauthorized requests from triggering workflows and generating costs.

Deployment

Cloudflare Builds deploys the service automatically. Every push to the main branch triggers a new deployment of the worker and its workflows.

Cloudflare's observability tools expose workflow executions and worker metrics in the dashboard.

The interactive diagram loads in your browser. Its contents are also available as a list.

Read diagram as a list
  • Cloudflare One, auth

    Protect the automation endpoints so only my CLI can trigger them.

    • Cloudflare Workers, deployment

      Expose the automation endpoints and trigger workflows from incoming requests.

      • Cloudflare Build, build

        Deploy the worker and workflows automatically on each push.

        • GitHub, repository

          Host the private source code for this internal automation service.

      • Cloudflare Workflows, workflows

        Run background jobs for the automation tasks handled by the service.

  • Cloudflare Domains, domain

    Manage the DNS records that route `automation.soubiran.dev` to the worker.

What do you think?

Discussions

Loading discussions...

Add a comment

Checking your session...