Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Caching poetry on Bitbucket Pipelines

Matteo Verità
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
May 8, 2024

I'm trying to setup my bitbucket_pipelines.yml file to make it possible for all the steps to have Poetry already installed, so that running `poetry run [command]` should never return `bash: poetry: command not found`. So far I made it work with the following configuration:

# This is an example Starter pipeline configuration
# Use a skeleton to build, test and deploy using manual and parallel steps
# -----
# You can specify a custom docker image from Docker Hub as your build environment.

image: python:3.12

definitions:
caches:
poetry-cache:
key:
files:
- poetry.lock
path: .venv

pipelines:
default:
- parallel:
- step:
name: 'Build and Test'
caches:
- poetry-cache
script:
- pipe: atlassian/poetry-cli-setup:0.2.0
- ./setup-poetry.sh
- poetry config virtualenvs.in-project true
- poetry install
- poetry run pytest
- step:
name: 'Lint'
caches:
- poetry-cache
script:
- pipe: atlassian/poetry-cli-setup:0.2.0
- ./setup-poetry.sh
- poetry install
- poetry run ruff check

As you can see, the only way I found was to reinstall Poetry in each step. It's already good enough that `poetry install` is fast because it uses the cache and in fact ends up installing nothing, however I wonder if there's a better way for poetry itself to make it available to all steps without having to install it in each.

2 answers

1 accepted

2 votes
Answer accepted
Joshua Tang
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 12, 2024 edited

Hi Matteo,

We have just released a new version of poetry-cli-setup:0.2.2 with some changes to allow caching of the Poetry installation. Similar to how you're caching the virtual environment, you can define an extra cache for the Poetry installation:

definitions:
caches:
poetry-cli-cache:
key:
files:
- bitbucket-pipelines.yml
path: /opt/poetry

Then you can include this cache in your individual steps, where the Poetry setup step would reuse the cached installation:

- step:
name: 'Lint'
caches:
- poetry-cache
- poetry-cli-cache
script:
- pipe: atlassian/poetry-cli-setup:0.2.2
- ./setup-poetry.sh
- poetry install
- poetry run ruff check

This example is also documented on the README of the pipe: https://bitbucket.org/atlassian/poetry-cli-setup/src/master/

Hope this helps!

 

Kind regards,
Joshua

Matteo Verità
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 5, 2024 edited

Thank you @Joshua Tang! It worked splendidly.

And thanks @Ben for the additional help!

Like • Joshua Tang likes this
jbsilva
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 21, 2024

Could you provide a full example?
When I removed `./setup-poetry.sh` from my second step I got `bash: poetry: command not found`.

Ben
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
June 23, 2024

Hi @jbsilva 

Please refer to the pipe documentation for a full example, the error message you've received generally means that the command you are executing can't be executed as a setup step is missing (ie the bash script you removed):
https://bitbucket.org/atlassian/poetry-cli-setup/src/master/

Cheers!

- Ben (Bitbucket Cloud Support)

jbsilva
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
June 24, 2024

By full example I mean one with at least two steps.

Like • drxaero likes this
1 vote
Ben
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
May 9, 2024

Hi Matteo,

You can potentially use artifacts to achieve this - so basically you could figure out where poetry is installed in the context of your build, then compress it as a zip - pass it to subsequent steps as an artifact, unzip it and execute poetry commands:

Other than that, I have reached out to our pipes development team to figure out the best place to raise suggestions.

Cheers!

- Ben (Bitbucket Cloud Support)

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
TAGS
AUG Leaders

Atlassian Community Events