Skip to main content

Automate Optimization of Pull Requests

Codeflash can automatically optimize your code when new pull requests are opened.

To be able to scan new code for performance optimizations, Codeflash requires a GitHub action workflow to be installed which runs the Codeflash optimization logic on every new pull request. If the action workflow finds an optimization, it communicates with the Codeflash GitHub App through our secure servers and asks it to suggest new changes to the pull request.

This is the most useful way of using Codeflash, where you set it up once and all your new code gets optimized. So setting this up is highly recommended.

Prerequisites

  • You have a Codeflash API key. If you don't have one, you can generate one from the Codeflash Webapp. Make sure you generate the API key with the right GitHub account that has access to the repository you want to optimize.
  • You have completed local installation steps and have a Python project with a pyproject.toml file that is configured with Codeflash. If you haven't configured Codeflash for your project yet, you can do so by running codeflash init in the root directory of your project.

Add the Codeflash GitHub Actions workflow

Guided setup

To add the Codeflash GitHub Actions workflow to your repository, you can run the following command in your project directory:

codeflash init-actions

This will walk you through the process of adding the Codeflash GitHub Actions workflow to your repository.

All Set up!

Open a new PR to your GitHub project, and you will now see a new actions workflow for Codeflash run. If it finds an optimization, codeflash-ai bot will comment on your repo with the optimization suggestions.

Manual Installation (optional)

If you prefer to install the GitHub actions manually, follow the steps below -

Add the workflow file

Create a new file in your repository at .github/workflows/codeflash-optimize.yaml with the following contents:

.github/workflows/codeflash-optimize.yaml
name: Codeflash

on:
pull_request:
workflow_dispatch:

jobs:
optimize:
name: Optimize new code in this PR
if: ${{ github.actor != 'codeflash-ai[bot]' }}
runs-on: ubuntu-latest
env:
CODEFLASH_API_KEY: ${{ secrets.CODEFLASH_API_KEY }}
CODEFLASH_PR_NUMBER: ${{ github.event.number }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
# TODO: Replace the following with your project's Python installation method
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
# TODO: Replace the following with your project's dependency installation method
- name: Install Project Dependencies
run: |
python -m pip install --upgrade pip
# TODO: Replace the following with your project setup method
pip install -r requirements.txt
pip install codeflash
- name: Run Codeflash to optimize code
id: optimize_code
run: |
codeflash

You would need to fill in the #TODOs in the file above to make it work. Please commit this file to your repository.

Add your API key to your repository secrets

Go to your GitHub repository, click Settings, and click on Secrets and Variables -> Actions on the left sidebar.

Add the following secret: