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
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:
- Building and packaging the application or model.
- Deploying to a test/staging environment.
- Running a series of unit and integration tests in that environment.
- Releasing the update to production if tests pass.
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:
- Build Stage: Builds a Docker container image of the application.
- Test Stage: Runs Python unit tests in the
tests/directory. - 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:
- Create a
Jenkinsfilein 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
kubectlinstalled. kubectlconfigured to connect to your Kubernetes cluster.
- With Docker, Python, and
- Dockerfile in project root
tests/directory with Python tests.deployment.yamlwith Kubernetes configuration.- Should define a deployment named
myapp. - Should reference the Docker image we build (including the container registry if applicable).
- Should define a deployment named
- Jenkins credentials configured for Docker (if pushing to a private registry).
- Jenkins needs to authenticate with our Kubernetes cluster.
To deploy:
- Push code with
Jenkinsfileto your repository. - Create a new Pipeline job in Jenkins.
- Configure the job to pull from your repository.
- 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.
-
Create a file named
.github/workflows/deploy.ymlin 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 testsdeployment.yamlwith Kubernetes configuration (assumes deployment is namedmyapp)- Kubernetes cluster access configured in GitHub Secrets (e.g., using
kubeconfigor service account token to authenticatekubectl)
To deploy:
- Push code with the workflow file to the repository.
- GitHub Actions will automatically detect and run the workflow.
- Monitor progress in the Actions tab of the repository.
- 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
Teststep’sruncommand: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 thepytesttesting framework within the container’s environment. This assumes our Docker image includes Python andpip.- If
pytestor other testing dependencies are already in our Docker image, this installation step can be removed.python -m pytest tests/: Runs thepytestcommand to execute tests located in thetests/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.
Common Rollback Strategies
-
Reverting Code: Roll back the codebase to the last known stable version.
- Works well for small, incremental updates.
- Requires strong version control practices.
-
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:
- Deploy changes to the green environment.
- Run tests and validations.
- Switch traffic to the green environment.
- 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:
- Start with a small percentage of replicas (e.g., 1 out of 10).
- 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
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. ↩︎
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.