How to set up a CI/CD Pipeline with DigitalOcean, and CTO.ai
Continuous Integration and Continuous Delivery automate the software development process from the initial code commit all the way through the deployment. Resources and workloads configured on DigitalOcean can be built, tested, and deployed to production using automated workflows. CI/CD Pipelines enable teams to deploy better-quality code and software faster. You can configure your automated workflows on DigitalOcean to run when a resource is created or provisioned. This process will run a series of tasks to ensure every commit and feature change is safe to merge into your main production environment branch.
CTO.ai Pipelines are easy to write and share, and they support environment builds to run concurrent jobs across your resources deployed and hosted in DigitalOcean. With CTO.ai Pipelines, you can configure and trigger a job on your event to automate any DigitalOcean workflow in your team. CTO.ai Pipeline gives you real-time feedback with output streaming logs that you can filter and share your log output with your team members.
In this tutorial, you'll learn how to configure and run CI/CD Pipelines on your DigitalOcean resources using CTO.ai.
Prerequisites
- CTO.ai Account and CTO.ai CLI installed
- Docker installed on your local machine
- Access to a DigitalOcean Account & CLI installed on your machine
- Terraform Account
Set up DigitalOcean Infrastructure
The DigitalOcean workflow is open-source on GitHub, and you can install it by cloning the repository.
git clone https://github.com/workflows-sh/do-k8s-cdktf.git
cd do-k8s-cdktf
Before starting, you must create your API and SPACES keys on DigitalOcean. These keys will be required for your workflow when writing your ops.yml
file. In setting up the DigitalOcean Infrastructure, we’ll create five keys, the DigitalOcean Token from the Application API, the Terraform Token from your Terraform workspace, DigitalOcean Spaces Access, and the Secret key from the spaces token on the DigitalOcean UI, and the GitHub Token from GitHub.
CTO.ai will use these API keys to authenticate, request, and create your DigitalOcean Workflow.
- Next, after creating your API keys and Tokens, add them to your secret vault on CTO.ai.
- Next, you can set up your DigitalOcean Infrastructure using
ops run -b .
and set up your DigitalOcean resources. You can see all the stacks and packages created when you set up your DigitalOcean Infrastructure in the infrastructure reposrc
directory.
Configure Sample App with Ops.yml file
To build, run, and deploy your DigitalOcean resources to your production environment using CI/CD Pipelines, you must write and configure the ops.yml
file in your code repository on GitHub. In the ops.yml
file, we have a series of commands starting with the setup commands and the environment variables and secrets.
- Paste the configurations above in your
ops.yml
file in your code repository directory.
version: "1"
pipelines:
- name: sample-expressjs-pipeline-do-k8s-cdktf:0.2.2
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:<github_org>/<github_repo>:pull_request.opened"
- "github:<github_org>/<github_repo>: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}":x-oauth-basic@github.com/$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
- Replace your GitHub org and GitHub repo in the event section.
- In the jobs section, we specify the packages we will install for the pipeline. Jobs are fully automated steps that define what to do in your application. Jobs also lets you install and manage packages to be installed in your CTO.ai DigitalOcean workflow.
- In the steps section, we define the required commands for our pipelines. You can create subdirectories, export your given path, run the commands to install the DigitalOcean dependencies, clone the GitHub repository, and fetch the GitHub sha. In the steps above, I’m configuring the CI/CD Pipeline to deploy our resources from DigitalOcean when the GitHub events are triggered. The
docker build -f Dockerfile -t registry.digitalocean.com/$ORG/$REPO-$STACK_TYPE:$REF .
command will build the Docker image the Dockerfile with the org repo stack and ref we specified in the static environment section, and thedocker push registry.digitalocean.com/$ORG/$REPO-$STACK_TYPE:$REF
command step will push the layers of the Docker image to the CTO.ai registry using the org, repo stack and ref we specified in the static env. - After configuring your
ops.yml
file save it and push the changes to your repository.
Trigger Pipelines with Event Triggers
When you’re done configuring your ops.yml
file, you can trigger your Pipelines using events on your GitHub repository. We configured our CI/CD Pipelines to run when a pull request is opened or merged.
- In your GitHub repository, make some changes and create a pull request. Once you open your pull request, you’ll see that your CI/CD Pipelines will start building and deploying your DigitalOcean resources to CTO.ai.
- Back in your CTO.ai Pipeline dashboard, you can see your Pipeline logs with a detailed overview of your VERSION, RUN ID, TAG, AND STATUS.
- View your Pipeline logs to track performance and access your test results with the job output on every deployed DigitalOcean resource.
- You can combine other events like
pull_request.merged
,create.tag
,pull_request.closed
to run and trigger your CI/CD Pipeline. For example, when you create a tag on your repository like this, your CI/CD Pipeline will trigger and deploy your resources from DigitalOcean.
Wow! Can I see more?
Of course, you can! If you’d like to learn how to configure and build CI/CD Pipelines on your DigitalOcean resources, we have a starter project pack for you on GitHub to get coding.
Running tasks and deploying resources from DigitalOcean can now be done automatically and easily using CTO.ai Pipelines. With our workflow integration, you can test changes, check code quality, generate Pipeline results logs quickly, and deploy them to your production environments with confidence. Get started for free.