Introduction

Continuous integration and continuous deployment (CI/CD) have emerged as delivery and release concepts that optimize software development processes. In this blog, we will delve deep into automating Docker deployments on DigitalOcean using CTO.ai. The Developer Control Plane provided by the CTO.ai platform integrates seamlessly with various platforms, including Docker and cloud services like DigitalOcean.


We provide a DigitalOcean Kubernetes workflow, including a complete deployment environment using DigitalOcean as the infrastructure provider. Comprising configurations for Kubernetes, Container Registry, Postgres, Spaces, Load Balancers SSL (via LetsEncrypt), and Project Resource Management—all built using CDKTF & Terraform Cloud—this workflow makes it easier to get started using DigitalOcean through the CTO.ai Developer Control Pane.

Pre-requisites

  • Accounts for CTO.ai, Terraform Cloud, DigitalOcean, and GitHub
  • Working knowledge of Docker

Setting Up Your DigitalOcean Infrastructure with CTO.ai Workflow

The repo also includes a PaaS workflow integration with CTO.ai to streamline the Developer Experience when using  the deployed infrastructure. This includes interactive workflows for managing your environment from Slack (or our CLI), as well as a  full CI/CD pipeline and Preview Environments to help you deliver software more effectively.

To get started with this workflow, clone and cd into the repo:

Next, clone and cd into the repo:

git clone [email protected]:workflows-sh/do-k8s-cdktf.git


cd do-k8s-cdktf

Create API, Spaces, and Token Key

Before you can deploy this workflow, you’ll need API tokens to access the integrated tools and cloud providers. Create the following API tokens and add them to your Secrets Store on the CTO.ai platform:

  • DO_TOKEN: Personal access token for DigitalOcean
  • DO_SPACES_ACCESS_KEY_ID and DO_SPACES_SECRET_ACCESS_KEY: Access key and secret for DigitalOcean Spaces.
  • TFC_TOKEN: API token generated by Terraform Cloud.
  • GITHUB_TOKEN: Personal access token for GitHub, scoped with Administration permissions for the relevant repos.

Begin building and deploying your DigitalOcean Infrastructure by entering the ops run . command.Find the Command workflow called setup, which is designed to configure your infrastructure by using the `run` command in our Slack App or ops CLI. Running this command will prompt you to set up your cloud-native infrastructure on DigitalOcean.

  • Enter the name of your environment; you can use dev, staging, or prod for the name of your environment.
  • Enter the name of your application and hit enter.

The CLI will start deploying your DigitalOcean infrastructure via Terraform and CTO.ai. Back in your DigitalOcean dashboard, you will see your resources deployed.

Configuring Your Docker Project

Create a Dockerfile and ops.yml file in your project repository, using our public sample repository as guidance for defining your own application and services.

In your ops.yml file, replace the event values with your own values and define your jobs. Be sure to specify the Docker commands necessary to build and deploy your application container image.

version: "1"
pipelines:
  - name: sample-expressjs-pipeline-do-k8s-cdktf:0.2.4
    description: Build and Publish an image in a DigitalOcean Container Registry
    env:
      static:
        - DEBIAN_FRONTEND=noninteractive
        - STACK_TYPE=do-k8s-cdktf
        - ORG=cto-ai
        - GH_ORG=workflows-sh
        - REPO=sample-expressjs-do-k8s-cdktf
        - BIN_LOCATION=/tmp/tools
      secrets:
        - GITHUB_TOKEN
        - DO_TOKEN
    events:
      - "github:workflows-sh/sample-expressjs-do-k8s-cdktf:pull_request.opened"
      - "github:workflows-sh/sample-expressjs-do-k8s-cdktf:pull_request.synchronize"
      - "github:workflows-sh/sample-expressjs-do-k8s-cdktf:pull_request.merged"
    jobs:
      - name: sample-expressjs-build-do-k8s-cdktf
        description: Build step for sample-expressjs-do-k8s-cdktf
        packages:
          - git
          - unzip
          - wget
          - tar
        steps:
          - mkdir -p $BIN_LOCATION
          - export PATH=$PATH:$BIN_LOCATION
          - ls -asl $BIN_LOCATION
          - DOCTL_DL_URL='https://github.com/digitalocean/doctl/releases/download/v1.79.0/doctl-1.79.0-linux-amd64.tar.gz' # Update to latest doctl binary here by providing URL
          - wget $DOCTL_DL_URL -O doctl.tar.gz
          - tar xf doctl.tar.gz -C $BIN_LOCATION
          - doctl version
          - git version
          - git clone https://"${GITHUB_TOKEN}":[email protected]/$GH_ORG/$REPO && cd $REPO
          - git fetch -a && git checkout "${REF}"
          - doctl auth init -t $DO_TOKEN
          - doctl registry login
          - CLEAN_REF=$(echo "${REF}" | sed 's/[^a-zA-Z0-9]/-/g' )
          - docker build -f Dockerfile -t one-img-to-rule-them-all:latest .
          - docker tag one-img-to-rule-them-all:latest registry.digitalocean.com/$ORG/$REPO:$CLEAN_REF
          - docker push registry.digitalocean.com/$ORG/$REPO:$CLEAN_REF
          - curl -L https://github.com/docker/compose/releases/download/1.29.2/docker-compose
          - chmod +x ~/docker-compose 
services:
  - name: sample-expressjs-service-do-k8s-cdktf:0.1.6
    description: Preview of image built by the pipeline
    run: node /ops/index.js
    port: [ '8080:8080' ]
    sdk: off
    domain: ""
    env:
      static:
        - PORT=8080
    events:
      - "github:workflows-sh/sample-expressjs-do-k8s-cdktf:pull_request.opened"
      - "github:workflows-sh/sample-expressjs-do-k8s-cdktf:pull_request.synchronize"
      - "github:workflows-sh/sample-expressjs-do-k8s-cdktf:pull_request.merged"
    trigger:
      - build
      - publish
      - start

Deploying to CTO.ai

When you are done, commit and push your ops.yml file to your repository on GitHub. When the Event Triggers action is successful, it will deploy your Docker images and automatically set up your CI/CD pipeline.

  • Here, we can see our Docker image pipeline being triggered from the repository we used earlier. You can see your image logs and get specific insights and metrics into how they are built.


Conclusion

Using CTO.ai for automated deployments greatly enhances the deployment speed and reliability. Integrating your existing Docker-based infrastructure and workflow with DigitalOcean through the CTO.ai platform paves the way for a faster, automated, error-free deployment process, ensuring that your development to production pipeline is efficient and robust.

By following the steps outlined in this blog, developers can automate Docker deployments on DigitalOcean using CTO.ai, fostering a streamlined development lifecycle and faster deployments. Explore the DigitalOcean workflow repository and the sample-app-demo repository to learn more.