App Platform: Automatically deploy pre-built container images on push

App Platform, DigitalOcean, GitHub, GitHub Actions

App Platform has supported running container images from DigitalOcean Container Registry (DOCR) from the very start, however, a much requested feature is the ability to autodeploy a container when the a new image was pushed to a specific tag. With the most recent release this feature has been added and is available for all!

Note: this feature is not supported for DockerHub.

How autodeploy works

When autodeploy is enabled for a DOCR Image component in App Platform the container registry is monitored and upon a new image push App Platform checks the image digest for a target tag and if it is different than the most recently deployed version that new image is deployed.

So this means that when pushing a tag you'll probably want to use a descriptive tag that represent the latest version of that image. For example, production, staging, dev are common.

Enabling autodeploy

UI: During creation

When creating an App or Component, select your DOCR repository and image, then check the Autodeploy checkbox.

A screenshot of the App Platform creation menu with autodeploy enabled.

UI: Exising image components

If you already have a DOCR Image deployed autodeploy can be enabled in the settings of that component.

A screenshot of the App Platform component settings menu with autodeploy enabled.

App Spec: Enabling within the spec

For power users, enabling autodeploy is as easy as adding a small yaml structure to your existing image spec:

deploy_on_push:
  enabled: true

for example:

name: docr-autodeploy-example
region: ams
services:
- http_port: 8080
  image:
    deploy_on_push:
      enabled: true
    registry_type: DOCR
    repository: go-info-webserver
    tag: latest
  instance_count: 1
  instance_size_slug: basic-xxs
  name: go-info-webserver
  routes:
  - path: /

An GitHub Actions example

As a fan of GitHub Actions, here's a simple workflow that does the following:

  1. Builds the docker image with two tags:
    1. the git commit short hash
    2. latest
  2. Installs the doctl commandline tool
  3. Authenticates with the registry attached to a DigitalOcean account
  4. Pushes the image with all tags

Paired with the app spec above which targest the latest image tag, when the underlying image changes (via digest checking) the new image will be deployed. If the digest has not changed no deployment will occur.

name: Push Docker Image to DOCR

on:
  push:
    branches: [ "master" ]
env:
  REGISTRY: "registry.digitalocean.com/jon"
  IMAGE_NAME: "go-info-webserver"
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/[email protected]
    - name: Build the Docker image
      run: docker build --file docker/Dockerfile --tag $(echo $REGISTRY)/$(echo $IMAGE_NAME):$(echo $GITHUB_SHA | head -c7) --tag $(echo $REGISTRY)/$(echo $IMAGE_NAME):latest .

    - name: Install doctl
      uses: digitalocean/action-[email protected]
      with:
        token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}

    - name: Auth with DOCR
      run: doctl registry login --expiry-seconds 1000

    - name: Push image to DOCR
      run: docker push $(echo $REGISTRY)/$(echo $IMAGE_NAME) --all-tags

When an image is pushed to a registry with a tag that has already been used, the new image will take that tag, untagging the old image.