Optimize Your CI/CD Pipeline
Get instant insights into your CI/CD performance and costs. Reduce build times by up to 45% and save on infrastructure costs.
name: 'Usage of setup-python GitHub Action'
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.10'
- run: python my_script.py
setup-python
Set up your GitHub Actions workflow with a specific version of Python
What is GitHub Actions setup-python?
Python Setup Action in our GitHub Actions workflows for enhancing our Python projects. The setup action makes it easy to install Python and PyPy, and integrates easily into our CI/CD pipeline.
How to Set Up PyPy in GitHub Actions?
Setting up PyPy can improve performance for certain Python applications. Here's how to configure it.
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: 'pypy3.9'
- run: python my_script.py
How to Set Up GraalPy in GitHub Actions?
GraalPy supports Python compatibility with additional performance optimizations. Here’s how to set it up.
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: 'graalpy-22.3'
- run: python my_script.py
The python-version
input is optional, but if one is not provided, the action will attempt to resolve the version through a .python-version file, or use the version from PATH
. For reliability, it is suggested to explicitly specify the Python version.
First, it will check the local tool cache for the corresponding version; if not found, it will download from GitHub Releases for Python and the official PyPy distribution site for PyPy.
How to Cache Package Dependencies in GitHub Actions?
By using GitHub Actions' caching mechanisms, one can very much reduce the build times for our Python projects. Here's a guide on how to cache package dependencies using the setup-python action.
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.9'
cache: 'pip' # activate caching for pip
- run: pip install -r requirements.txt
Note: The cache might not be used if requirements.txt was not updated frequently and there are newer versions of dependencies available, potentially increasing the total build time.
Best practices for caching with pip So that the cache is used effectively:
- Specify the exact versions of the dependencies in your requirements.txt file.
- Make manual updates to dependency versions if needed to keep compatibility and performance up to the mark.
How to setup cache for pipenv and poetry?
For Pipenv- or Poetry-based projects, you can also specify cache support to the cache system using the setup-python action. However, if you are going to use paths that are not in the default locations, you need to specify that.
Caching pipenv Dependencies
Here's an example of how to cache dependencies managed by pipenv:
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.9'
cache: 'pipenv'
- name: Install pipenv
run: curl https://raw.githubusercontent.com/pypa/pipenv/master/get-pipenv.py | python
- run: pipenv install
How to Cache poetry Dependencies in GitHub Actions?
For projects using poetry, here’s how you can enable caching:
steps:
- uses: actions/checkout@v4
- name: Install poetry
run: pipx install poetry
- uses: actions/setup-python@v5
with:
python-version: '3.9'
cache: 'poetry'
- run: poetry install
- run: poetry run pytest
How to Use File Paths List for Caching Dependencies in GitHub Actions?
Specify multiple file paths to ensure all relevant dependency files are cached:
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.9'
cache: 'pipenv'
cache-dependency-path: |
server/app/Pipfile.lock
__test__/app/Pipfile.lock
- name: Install pipenv
run: curl https://raw.githubusercontent.com/pypa/pipenv/master/get-pipenv.py | python
- run: pipenv install
How to Use Wildcard Patterns to Cache Dependencies in GitHub Actions?
Leverage wildcard patterns to target multiple dependency files across the project:
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.9'
cache: 'pip'
cache-dependency-path: '**/requirements-dev.txt'
- run: pip install -r subdirectory/requirements-dev.txt
How to Cache Dependencies for Projects Using setup.py?
For projects with a setup.py, here’s how to cache dependencies efficiently:
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.11'
cache: 'pip'
cache-dependency-path: setup.py
- run: pip install -e .
# Or pip install -e '.[test]' to install test dependencies
These methods will help us optimize our Python environment setup and speed up our builds in GitHub Actions.