This will be quite useful in increasing code quality and be certain about best practices observed across a development process. In this article, we’ll look at how to set up a GitLab CI/CD pipeline for automating code reviews of Python code using the Ollama large language model. This setup reviews all the Python files in your repository, but it’s easily adapted to other use cases and file types.
Why Automate Code Reviews?
This not only helps in saving time but also brings consistency across the code base. When you integrate an AI model like Ollama into your CI/CD pipeline, on the go, you get feedback regarding the possible issues with the code and improve it by following best practices—all of this without a single line of manual interference.
Setting Up Your GitLab CI/CD Pipeline
Below is the configuration for your .gitlab-ci.yml
file. It will:
- Discover all Python files in the repository.
- Run the Ollama container and start it.
- Review each Python file and execute it to provide suggestions.
- Save the results of review as an artifact for further investigation.
stages:
- review
code_review:
stage: review
image: docker:latest
services:
- docker:dind
variables:
DOCKER_DRIVER: overlay2
script:
- echo "Running code review job"
- echo "Finding all Python files in the repository..."
# Find all Python files in the repository
- |
FILES=$(find . -name "*.py" || true)
echo "Found Python files: $FILES"
# Exit early if no Python files are found
if [ -z "$FILES" ]; then
echo "No Python files to check."
exit 0
fi
# Clean the review.md file before starting
- echo "" > review.md
# Pull the Ollama image and start the container
- echo "Pulling and starting Ollama container..."
- docker run -d -v ollama:/root/.ollama --name ollama ollama/ollama
# Review each Python file found in the repository
- |
for FILE in $FILES; do
content=$(cat "$FILE")
prompt="\n Review this code, provide suggestions for improvement, coding best practices, improve readability, and maintainability. Remove any code smells and anti-patterns. Provide code examples for your suggestion. Respond in markdown format. If the file does not have any code or does not need any changes, say 'No changes needed'."
# Run the ollama model
suggestions=$(docker exec ollama ollama run codellama "Code: $content $prompt")
# Append the suggestions to review.md
echo "## Review for $FILE" >> review.md
echo "" >> review.md
echo "$suggestions" >> review.md
echo "" >> review.md
echo "---" >> review.md
echo "" >> review.md
done
echo "All Python files were reviewed and the suggestions are in review.md."
artifacts:
paths:
- review.md
Optimizing with Your Own GitLab Runner
Use Your Own GitLab Runner
Using the default docker:latest
image inside your CI/CD pipeline can be big regarding both time overhead and resources. Every time you run the pipeline, it fetches the Docker image from Docker Hub, slowing down pipeline execution speed. Set up and use a self-managed GitLab runner, and you will be able to:
- Save time on job setup – avoid repetitive downloading of Docker images.
- Configure your environment as per your specific needs, including the pre-installation of your desired dependencies, such as Docker and Ollama.
- Have stable performance with your own runner and be able to further customize your CI environment to suit your team’s needs.
Improving File Detection
Although the example job reviews all Python files in the repository, you will most likely want to adjust the logic so that it will deal only with the files included in the commit or merge request that triggered the job. This will reduce the processing time and let the AI model deal with only the latest changes.
Detecting Changed Files in a Commit
You can leverage the file detection logic as shown below to return the full list of files changed in the commit, which triggered this job.
FILES=$(git diff --name-only --diff-filter=ACM $CI_COMMIT_BEFORE_SHA $CI_COMMIT_SHA | grep '\.py$' || true)
Handling Other Use Cases and File Types
The prompt
can then be tuned to other file types or special cases like reviewing configuration files, documentations, or scripts.
For Config Files (e.g., YAML, JSON):
prompt="\n Review this configuration file for best practices, errors, and potential improvements. Respond in markdown format."
For Markdown Files:
prompt="\n Review this markdown file for readability, grammar, and formatting issues. Suggest improvements in markdown format."
Or, adjust the prompt based on your file type, or for the particular needs of your team, to extend this pipeline for reviewing other important parts of your codebase.
Conclusion
Have a full-featured CI/CD pipeline that involves automated code reviews with GitLab and Ollama, drastically improving code quality and making your development process much easier. You can further increase performance and make the reviews relevant only to changed files by configuring your own GitLab runner and tailoring this setup’s file detection logic. This is an entirely flexible system that can easily be adapted for a whole range of file types and review situations, thus becoming an incredibly useful addition to any DevOps workflow.
This pipeline will be configured in a way that each commit is reviewed and thus ensures better, cleaner, and more maintainable code for the long term.