DigitalOcean App Action

repository·main·Indexed 41 days ago

https://github.com/digitalocean/app_action

A GitHub Action suite for automating the deployment and management of DigitalOcean App Platform applications. It includes a `deploy` action for deploying from source via app specs or prebuilt container images, and a `delete` action for removing apps. Key features include support for in-repository app.yaml files, environment variable templating, ephemeral preview environments for Pull Requests, and surfacing of build and deploy logs.

Tokens
3.7K
Snippets
8
Records
14
Agent score
99%

What's inside DigitalOcean App Action

  1. Implement Preview Apps for Pull Requests

    main

    You can orchestrate per-PR app previews using the deploy_pr_preview: "true" input. This workflow typically involves:

    1. On Pull Request: Deploy the app with deploy_pr_preview: "true". If successful, post the live_url as a comment. If it fails, post the build_logs and deploy_logs.
    2. On PR Closed/Merged: Use the delete action with from_pr_preview: "true" to clean up the preview app.
    # Workflow to deploy preview
    name: App Platform Preview
    on:
      pull_request:
        branches: [main]
    jobs:
      test:
        runs-on: ubuntu-latest
        steps:
          - name: Checkout repository
            uses: actions/checkout@v4
          - name: Deploy the app
            id: deploy
            uses: digitalocean/app_action/deploy@v2
            with:
              deploy_pr_preview: "true"
              token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
          # ... (logic to post comment with ${{ fromJson(steps.deploy.outputs.app).live_url }})
    
    # Workflow to delete preview
    name: Delete Preview
    on:
      pull_request:
        types: [ closed ]
    jobs:
      closed:
        runs-on: ubuntu-latest
        steps:
          - name: delete preview app
            uses: digitalocean/app_action/delete@v2
            with:
              from_pr_preview: "true"
              ignore_not_found: "true"
              token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
  2. Upgrade from v1.x to v2.x

    main

    The v1 branch is no longer under active development. To upgrade to v2, you must change the action reference path to include the deploy keyword.

    Change from: digitalocean/app_action@v1.x.x

    Change to: digitalocean/app_action/deploy@v2.x.x

  3. Deploy an app using a prebuilt container image

    main

    To deploy an app using a container image (e.g., from GHCR), specify the image details in your app.yaml. It is strongly recommended to use image digests rather than tags to ensure the exact image version is deployed and to avoid inconsistencies with mutable tags like latest.

    # .do/app.yaml
    name: sample
    services:
    - name: sample
      image:
        registry_type: GHCR
        registry: YOUR_ORG
        repository: YOUR_REPO
        registry_credentials: ${GHCR_CREDENTIALS}
        digest: ${SAMPLE_DIGEST}
    
    # GitHub Action Workflow
    jobs:
      build-push-deploy-image:
        runs-on: ubuntu-latest
        steps:
          - name: Checkout repository
            uses: actions/checkout@v4
          - name: Log in to the Container registry
            uses: docker/login-action@v3.3.0
            with:
              registry: ghcr.io
              username: ${{ github.actor }}
              password: ${{ secrets.GITHUB_TOKEN }}
          - name: Build and push Docker image
            id: push
            uses: docker/build-push-action@v6.5.0
            with:
              context: .
              push: true
              tags: ghcr.io/${{ github.repository }}:latest
          - name: Deploy the app
            uses: digitalocean/app_action/deploy@v2
            env:
              SAMPLE_DIGEST: ${{ steps.push.outputs.digest }}
              GHCR_CREDENTIALS: ${{ secrets.GHCR_CREDENTIALS }}
            with:
              token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
  4. Update component images in v2 using environment variables

    main

    The images input from v1 is not supported in v2. Instead, you should use environment variable substitution within your App Spec or use specific environment variables to update an existing app's component images via the app_name input.

    To update an image reference for a specific component, use the following environment variable patterns:

    • To update the tag: IMAGE_TAG_<COMPONENT_NAME>
    • To update the digest: IMAGE_DIGEST_<COMPONENT_NAME>

    Naming Convention: Component names are converted to environment variable keys by converting them to uppercase and replacing dashes (-) with underscores (_). For example, a component named sample-service becomes SAMPLE_SERVICE.

    Example: Updating sample-service from tag v1 to v2.

    # App Spec
    name: sample-app
    services:
    - name: sample-service
      image:
        registry_type: GHCR
        registry: YOUR_ORG
        repository: YOUR_REPO
        tag: v1
    
    # GitHub Action Step
    - name: Deploy the app
      uses: digitalocean/app_action/deploy@v2
      env:
        IMAGE_TAG_SAMPLE_SERVICE: v2
      with:
        token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
        app_name: sample-app
  5. Deploy an app using an in-repository `app.yaml` with secrets

    main

    You can deploy an app by including an app.yaml in your repository. To pass sensitive information from GitHub Secrets to your App Platform service, use environment variable substitution in the app.yaml and pass the secret into the action's env context.

    Important: Avoid overriding APP_DOMAIN, APP_URL, or APP_ID in the action's environment to prevent interference with the platform's own variable expansion.

    # .do/app.yaml
    name: sample
    services:
    - name: sample
      envs:
      - key: SOME_SECRET
        value: ${SOME_SECRET_FROM_REPOSITORY}
        type: SECRET
      github:
        branch: main
        repo: digitalocean/sample-golang
    
    # GitHub Action Workflow
    jobs:
      deploy-app:
        runs-on: ubuntu-latest
        steps:
          - name: Checkout repository
            uses: actions/checkout@v4
          - name: Deploy the app
            uses: digitalocean/app_action/deploy@v2
            env:
              SOME_SECRET_FROM_REPOSITORY: ${{ secrets.SOME_SECRET_FROM_REPOSITORY }}
            with:
              token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
  6. Launch a preview app for Pull Requests

    main
    When deployPRPreview is enabled, the action performs a sanitization step on the AppSpec using utils.SanitizeSpecForPullRequestPreview. This ensures that the specification is safe and appropriate for a temporary preview environment generated from a GitHub Pull Request context.
  7. Deploy or update a DigitalOcean App Platform app

    main

    The deploy action automates the creation or update of a DigitalOcean App Platform application. It follows this logic:

    1. Spec Resolution: It determines the application specification (AppSpec) by either fetching the existing spec from a named app (appName) or reading a local file (appSpecLocation). If reading from a file, environment variables within the spec are expanded.
    2. Image Replacement: It automatically replaces container images in the spec (useful for CI/CD workflows where image tags change).
    3. Deployment:
      • If the app does not exist, it creates a new one using the provided projectID.
      • If the app exists, it updates the existing app and sets UpdateAllSourceVersions: true to ensure all components are refreshed.
    4. Monitoring: The action waits for the deployment to reach a terminal state (Active, Error, Canceled, or Superseded).
    5. Logs & Outputs:
      • It retrieves and surfaces build_logs and deploy_logs as GitHub Action outputs.
      • It provides the full application object as a JSON string in the app output.
      • If the app has components that produce a public URL (services, static sites, or functions), it waits for the liveURL to become available and prints it.
  8. Delete a DigitalOcean App Platform app

    main

    The delete action allows you to remove a DigitalOcean App Platform app via a GitHub Action. To identify the app to be deleted, you must provide either an app_id, an app_name, or enable from_pr_preview (which uses the repository context to generate a name).

    If you provide an app_name instead of an app_id, the action will attempt to find the app by that name. If the app is not found, the action will fail unless ignore_not_found is set to true.

    - name: Delete DigitalOcean App
      uses: digitalocean/app_action@v2
      with:
        token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
        app_id: 'your-app-id'
        ignore_not_found: true
  9. Use the `delete` action to remove apps

    main

    The delete action is used to remove an existing DigitalOcean App Platform app.

    - name: delete preview app
      uses: digitalocean/app_action/delete@v2
      with:
        from_pr_preview: "true"
        ignore_not_found: "true"
        token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
  10. Use the `deploy` action to deploy DigitalOcean App Platform apps

    main

    The deploy action allows you to deploy an app from source or an existing app spec. It supports using an in-repository app.yaml (defaults to .do/app.yaml) which can be templated with environment variables.

    Key features:

    • In-repository spec: Automatically picks up your app.yaml and applies updates on commit.
    • Templating: Supports environment variable substitution within the app.yaml file.
    • Log surfacing: Can print and output build and deploy logs.
    • Preview Mode: When deploy_pr_preview is enabled, it creates a temporary app instance tailored for Pull Requests (modifying domains, alerts, and repo references to avoid conflicts).
    - name: Deploy the app
      uses: digitalocean/app_action/deploy@v2
      with:
        token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
  11. Reference: `deploy` action inputs and outputs

    main

    Inputs

    • token: DigitalOcean Personal Access Token (Required).
    • app_spec_location: Location of the app spec file. Defaults to .do/app.yaml.
    • project_id: ID of the project to deploy the app to. If not given, the app will be deployed to the default project.
    • app_name: Name of the app to pull the spec from. If provided, the in-repository app spec is ignored.
    • print_build_logs: Print build logs to the GitHub Action log. Defaults to false.
    • print_deploy_logs: Print deploy logs to the GitHub Action log. Defaults to false.
    • deploy_pr_preview: Deploy the app as a PR preview. The app name is derived from the PR, and the spec is modified to exclude conflicting configurations like domains and alerts. Defaults to false.

    Outputs

    • app: A JSON representation of the entire app after the deployment.
    • build_logs: The build logs of the deployment.
    • deploy_logs: The deploy logs of the deployment.
  12. Reference: `delete` action inputs

    main

    Inputs

    • token: DigitalOcean Personal Access Token (Required).
    • app_id: ID of the app to delete.
    • app_name: Name of the app to delete.
    • from_pr_preview: Use this if the app was deployed as a PR preview. The app name will be derived from a combination of the repo name and the PR.
    • ignore_not_found: If true, the action will not fail if the app is not found.