CTO.ai and Docker: A Beginner's Guide to Building a CI/CD Pipeline
Introduction
This guide will walk you through using CTO.ai and Docker to establish a CI/CD pipeline, automating your software delivery process.
What is CI/CD
CI/CD stands for Continuous Integration and Continuous Deployment:
- Continuous Integration (CI): This is an automated process for testing code changes to ensure they integrate smoothly with the existing codebase.
- Continuous Deployment (CD): This involves automatically deploying changes that have passed through CI to your production environment.
What is CTO.ai?
CTO.ai is a cloud-based platform that facilitates the CI/CD process. It runs your software tests and deploys your code automatically whenever there are changes.
What is Docker?
Docker is a tool for creating containers for your applications. Containers ensure that your application and all its dependencies work the same across various environments.
Setting Up a CI/CD Pipeline Using CTO.ai and Docker
Create a Dockerfile
A Dockerfile is a script containing instructions to build your Docker container. Here’s an example:
# Use a specific version of node based on Debian for better control and compatibility
FROM node:14-buster-slim
# Set work directory in the Docker image
WORKDIR /app
# Copy package.json and package-lock.json (or yarn.lock) to leverage Docker cache
COPY package*.json yarn.lock* /app/
# Install build tools and Python for node-gyp, then install node modules
# and remove build tools to keep the image small
RUN apt-get update && \
apt-get install -y python make g++ && \
npm install --production || yarn install --production && \
apt-get purge -y python make g++ && \
apt-get autoremove -y && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
# Copy the rest of the application
COPY . /app
# Expose the port the app runs on
EXPOSE 8080
# Command to run the application
CMD ["node", "app.js"]
You can also use the sample Dockerfile template in our opensource workflow repository .
The Dockerfile is used for an Express.js application and is built for both build performance (using Docker's caching mechanism) and image size (removing unnecessary build tools after installation).
Set Up CTO.ai
- Create a CTO.ai Account: Sign up and connect your GitHub repository with CTO.ai.
- Install the Ops CLI by running the
npm install -g @cto.ai/ops
in your local machine.
The CTO.ai ops CLI acts as the integration point between your command line workflows and the CTO.ai platform, providing a framework for implementing ChatOps-based workflows that integrate with your existing processes. It allows you to build, publish, and run Commands, Pipelines, and Services workflows from the comfort of your terminal, manage your Configs and Secrets stores, and invite members to your CTO.ai team. - Log in to your account using
ops account:signin
Running this command triggers a browser-based signin flow that allows you to log in to your account as your would on the web, as well as sign in with OAuth providers like GitHub. - In your GitHub repository, create a Config File called
ops.yml
: This file contains CTO.ai's instructions, steps, and commands for your project.
Example ops.yml:
version: "1"
pipelines:
- name: sample-expressjs-pipeline-do-k8s-cdktf:0.2.5
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
- tar xf doctl.tar.gz -C $BIN_LOCATION
- doctl version
- git version
- git clone https://oauth2:$GITHUB_TOKEN@github.com/$GH_ORG/$REPO
- cd $REPO && ls -asl
- 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
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
This configuration defines a job to build your application using Docker.
Automate Deployment
Automate your deployment process:
- Add Deployment Scripts: In the
ops.yml
file, we included commands for deploying your application, like pushing the Docker image to DigitalOcean registry. - Set Environment Variables: In the
ops.yml
file above, we also included secrets and sensitive information passwords, DigitalOcean tokens, and GitHub tokens.
Conclusion
With these steps, you've created a basic CI/CD pipeline using CTO.ai and Docker. This setup automates the build and test process, making your software delivery efficient.