denoland / deploy_feedback

For reporting issues with Deno Deploy
https://deno.com/deploy
74 stars 5 forks source link

"Deno deploy actions" #585

Closed fro-profesional closed 8 months ago

fro-profesional commented 10 months ago

Deno Deploy Actions Proposal

As a passionate Deno user and developer of a SaaS platform primarily powered by Deno, I've identified a need for a more integrated solution for running scheduled and triggered jobs. Currently, I rely on GitHub Actions, using Deno for scripting. Tools like dax have been immensely helpful.

Proposed Feature: Deno Deploy Actions

Concept

Imagine being able to define actions within your Deno script, something akin to:

import $ from "https://deno.land/x/dax/mod.ts";

// Example script to check and install Docker
async function checkDockerInstallation() {
    // ... implementation ...
}

// Define a Deno action
Deno.action("install-docker", async () => {
  await checkDockerInstallation();
});

When this action is triggered, an ephemeral, secure Docker image is spun up, providing a secure runtime environment with access only to Deno.

API Suggestion

Triggering Actions

To trigger an action:

await Deno.actionTrigger("install-docker");

Local Execution

Locally, this could work by attaching the action name to the Deno namespace, effectively abstracting the underlying implementation.

On Deno Deploy

For Deno Deploy, triggering an action could spin up a small, secure Docker container, isolating the runtime environment for security and efficiency.

Use Cases

Common Solutions

Batch/Queue Triggering

Using Deno's KV storage for queue management:

const db = await Deno.openKv();

db.listenQueue(async (msg) => {
   await Deno.actionTrigger("some-action");
});

Recurring Tasks

Scheduling tasks using a cron-like syntax:

Deno.cron("Sample cron job", "*/10 * * * *", async () => {
  await Deno.actionTrigger("sample-action");
});

This proposal aims to integrate a robust action handling system into Deno Deploy, enhancing its capabilities for automation and job management in a secure and efficient manner. The proposed API and usage examples illustrate a flexible and powerful toolset that would be valuable for many Deno developers

Migration Strategy

Example 1: Automated Testing

GitHub Action:

A typical GitHub Action for running automated tests might look like this:

name: Run Tests

on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Set up Node.js
      uses: actions/setup-node@v2
      with:
        node-version: '14'
    - run: npm install
    - run: npm test

Deno Action with Dax:

In Deno, you might set up an action to run tests whenever a specific event occurs, like a push to a specific branch.


import $ from "https://deno.land/x/dax/mod.ts";

async function runTests() {
  await $`deno test`;
}

Deno.action("run-tests", async () => {
  await runTests();
});

You would then trigger this action in response to the desired event (e.g., a push to your repository).

Example 2: Building and Deploying a Deno Application

GitHub Action:

A GitHub Action for building and deploying a Deno application may look like this:

name: Deploy

on:
  push:
    branches:
      - main

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Set up Deno
      uses: denolib/setup-deno@v1
    - run: deno compile --output=./myapp src/main.ts
    - name: Deploy
      run: scp myapp user@server:/path/to/deploy

Deno Action with Dax:

In Deno Deploy with dax, the equivalent action might look like this:

import $ from "https://deno.land/x/dax/mod.ts";

async function buildAndDeploy() {
  // Compile the application
  await $`deno compile --output=./myapp src/main.ts`;

  // Deploy the application (example using scp)
  await $`scp myapp user@server:/path/to/deploy`;
}

Deno.action("deploy-application", async () => {
  await buildAndDeploy();
});

In this Deno action, you compile the Deno application and then deploy it to a server using secure copy (scp).