Continuous Deployment

Continuous Deployment (CD) is the practice of automatically deploying code changes to production after passing all the necessary tests and validations in the CI pipeline.

This Lesson Will Cover
  • 🚀 Continuous Deployment fundamentals
  • 🛠️ Popular CD tools like Jenkins and GitHub Actions
  • 🔄 Deployment strategies and rollback procedures
  • 🎭 Blue-Green and Canary deployment patterns
Continuous Deployment Fundamentals Mind Map
Topics in CD, including stages, benefits, requirements, and risks.

Why do Continuous Deployment?

Continuous Deployment automates the release process, ensuring that every validated code change is delivered to production seamlessly.

For AI applications, this might include:

  • Deploying new versions of ML models
  • Updating APIs or data pipelines
  • Ensuring compatibility across systems

A typical CD pipeline involves:

  1. Building and packaging the application or model.
  2. Deploying to a test/staging environment.
  3. Running a series of unit and integration tests in that environment.
  4. Releasing the update to production if tests pass.
Continuous Deployment Pipeline Flowchart Example
Continuous deployment pipeline flowchart example, including builds, tests and deploys with an automated rollback strategy.

Tools for Continuous Deployment

In this section, we look at a couple popular tools for implementing CD in AI/ML applications.

Jenkins

  • Automation server with an extensive plugin ecosystem
  • Supports complex deployment pipelines
  • Can integrate with ML model training and deployment
Example: Basic CI/CD workflow with Jenkins

This pipeline has three main stages:

  1. Build Stage: Builds a Docker container image of the application.
  2. Test Stage: Runs Python unit tests in the tests/ directory.
  3. Deploy Stage: Deploys the application to a Kubernetes cluster.

The pipeline automates the entire process, ensuring code changes are properly tested before reaching production.

To implement this pipeline:

  1. Create a Jenkinsfile in the root of the project:
pipeline {
   agent any
   stages {
       stage('Build') {
           steps {
               sh 'docker build -t myapp .'
               // After building, we would typically need to push this image to a container registry
               // e.g., sh 'docker push your-docker-registry/myapp'
           }
       }
       stage('Test') {
           steps {
               sh 'python -m pytest tests/'
           }
       }
       stage('Deploy') {
           steps {
               sh 'kubectl apply -f deployment.yaml'
               sh 'kubectl rollout status deployment/myapp'
           }
       }
   }
}

Requirements:

  • Jenkins server
    • With Docker, Python, and kubectl installed.
    • kubectl configured to connect to your Kubernetes cluster.
  • Dockerfile in project root
  • tests/ directory with Python tests.
  • deployment.yaml with Kubernetes configuration.
    • Should define a deployment named myapp.
    • Should reference the Docker image we build (including the container registry if applicable).
  • Jenkins credentials configured for Docker (if pushing to a private registry).
    • Jenkins needs to authenticate with our Kubernetes cluster.

To deploy:

  1. Push code with Jenkinsfile to your repository.
  2. Create a new Pipeline job in Jenkins.
  3. Configure the job to pull from your repository.
  4. Jenkins will automatically detect and run the pipeline stages.

GitHub Actions

We saw an example of GitHub Actions in the previous lesson.

Here’s a rundown of this (free) serverless offering:

  • Cloud-native CI/CD solution
  • Easy integration with GitHub repositories
  • Marketplace with pre-built actions
  • Supports matrix builds and parallel jobs
Example: Basic CI/CD workflow with GitHub Actions

This pipeline is very similar to the Jenkins example, but now implemented with GitHub Actions.

  1. Create a file named .github/workflows/deploy.yml in the root of the project:

    name: Deploy to Production
    on:
      push:
        branches: [main]
    
    jobs:
      deploy:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v3
          - name: Build
            run: docker build -t myapp .
          - name: Test # Run tests inside the Docker container
            run: |
              docker run --rm -v $(pwd):/app -w /app myapp \
                pip install pytest && \
                python -m pytest tests/
          - name: Deploy
            run: |
              kubectl apply -f deployment.yaml
              kubectl rollout status deployment/myapp
    

Requirements:

  • GitHub repository
  • Dockerfile in project root
  • tests/ directory with Python tests
  • deployment.yaml with Kubernetes configuration (assumes deployment is named myapp)
  • Kubernetes cluster access configured in GitHub Secrets (e.g., using kubeconfig or service account token to authenticate kubectl)

To deploy:

  1. Push code with the workflow file to the repository.
  2. GitHub Actions will automatically detect and run the workflow.
  3. Monitor progress in the Actions tab of the repository.
  4. Check deployment status in the Kubernetes cluster.

Everything above should look familiar by this point, except for the Test step. Here we’re running pytest from our Docker image (i.e. in a Docker container), since that will have the proper runtime and dependencies that we want to test against.

Explanation of the Test step’s run command:

docker run --rm -v $(pwd):/app -w /app myapp \
  pip install pytest && \
  python -m pytest tests/
  • docker run --rm: Runs a Docker container and removes it after it finishes.
  • -v $(pwd):/app: Mounts the current GitHub Actions workspace directory ($(pwd)) into the container at /app.
    • This makes our project code accessible inside the container.
    • It’s only needed if we didn’t copy the app code into the image in the Dockerfile.
  • -w /app: Sets the working directory inside the container to /app, where the code is mounted.
  • myapp: Specifies the Docker image to run, which is the image built in the “Build” step.
  • pip install pytest && python -m pytest tests/: These are the commands executed inside the Docker container.
    • pip install pytest: Installs the pytest testing framework within the container’s environment. This assumes our Docker image includes Python and pip.
    • If pytest or other testing dependencies are already in our Docker image, this installation step can be removed.
    • python -m pytest tests/: Runs the pytest command to execute tests located in the tests/ directory (which is accessible because of the volume mount).

DevOps Deployment Strategies

When deployments fail or behave unexpectedly, we can roll back to a previous state.

DevOps Deployment Strategy Decision Flow
Flowchart comparing rollback strategies: Blue‑Green, Canary, and Rolling strategies.

Common Rollback Strategies

  1. Reverting Code: Roll back the codebase to the last known stable version.

    • Works well for small, incremental updates.
    • Requires strong version control practices.
  2. Rollback with Containers: Deploy the previous Docker image.

    • Ensure old images are retained in the container registry.

Blue-Green Deployments

Blue-Green Deployments are a DevOps pattern where two identical environments are maintained: one active (blue) and one idle (green). New updates are deployed to the green environment, and traffic is switched over once testing is complete.

Workflow:

  1. Deploy changes to the green environment.
  2. Run tests and validations.
  3. Switch traffic to the green environment.
  4. Retain the blue environment as a fallback.

Advantages:

  • Zero downtime during deployment.
  • Quick rollback by switching traffic back.

Canary Releases

Canary releases[1] allow gradual deployment of changes to a subset of users before rolling out to the entire user base. This minimizes risk by observing real-world behavior in a controlled way.

Workflow:

  1. Start with a small percentage of replicas (e.g., 1 out of 10).
  2. Gradually increase the number of replicas if no issues are detected.

Advantages:

  • Safer than full deployments.
  • Enables monitoring for specific metrics (e.g., latency, error rate).

Conclusion

A lot of the ideas in this lesson (and the previous one) are from The DevOps Handbook[2].

By the way, as we near the end of the AI Deployment section, I’d like to check in with you about what topics you found the most interesting. Remember what I said earlier about paying attention to what excites you and leaning into it?

For myself, I’m pretty interested in the generative AI stuff right now. But if you asked me a year ago, I would have told you that I’m most interested in the lesson coming up next.

Key Takeaways

🚀 Continuous Deployment automates the release process for validated code changes.

🛠️ Popular CD tools include Jenkins and GitHub Actions, each with its own workflow syntax.

🔄 Deployment strategies like Blue-Green and Canary releases help manage risk.

⏪ Rollback procedures are important for maintaining system reliability.

Next: MLOps

Now that we’ve covered a lot of fundamental DevOps concepts, we’re ready for MLOps.

Footnotes


  1. Canary releases get their name from the historical practice of coal miners using canary birds as early warning systems. Miners would carry cages with canaries into the mines—if dangerous gases like carbon monoxide were present, the canary would die before the levels became lethal to humans, giving miners time to evacuate. Similarly, canary deployments expose potential issues by testing changes on a small subset of users first. ↩︎

  2. I’ve got a video walkthrough of my notes on The DevOps Handbook. It’s paywalled, but since you bought this course, I’d be quite happy to share an unlisted YouTube video link with you. Ping me on Discord! ↩︎

Comments (0)

No comments yet.