Pip Cache not working for next steps
Asked by Vitor Oliveira:

I've configured a pipeline with python build and deployment, but my pip cache is not working

Here is my bitbucket-pipelines.yml

```
definitions: 
  steps: 
    - step: &pip-install 
      name: Install requirements 
      image: python:3.8.2 
      caches: 
        - pip 
      script: 
        - pip install -r requirements.txt --trusted-host pypi.org --trusted-host files.pythonhosted.org --trusted-host pypi.python.org 

    - step: &cdk-deploy 
      caches: 
        - pip 
      script: 
        - pipe: docker://bancosmartbank/bitbucket-pipe-cdk-pix-stack:1.7 
          variables: 
            AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID 
            AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY 
            AWS_REGION: $AWS_REGION 
            ENV: $BITBUCKET_DEPLOYMENT_ENVIRONMENT 
            COMMAND: deploy 
            SERVICE: pix-stack-* 

pipelines: 
  branches:
    feature/*: 
      - step: *pip-install 
      - step: 
          <<: *cdk-deploy 
          name: Deploy Test 
          deployment: dev
```

After pip install -r requirements.txt the console shows me that the cache was found

    Assembling contents of new cache 'pip'
    Cache "pip": Compressing
    Cache "pip": Compressed in 1 seconds
    Cache "pip": Uploading 35.8 MiB
    Cache "pip": Uploaded in 2 seconds

But on next step I've gotten this error

    CDK BootstrapTraceback (most recent call last):  
    File "http://app.py", line 3, in <module>    
    from aws_cdk import core
    ModuleNotFoundError: No module named 'aws_cdk'
    Subprocess exited with error 1?


Answers:
author: Theodora Boudale
body: Hi [@Vitor Oliveira](/t5/user/viewprofilepage/user-id/4319757) ,

If you check the "*Build setup* " in the Pipelines log of the second step, can you see a message about the cache being downloaded?  
This is to confirm whether the cache is indeed being downloaded in the second step, or not.

If the cache is successfully downloaded for the second step, it will still not be available in the pipe.  
When you execute a pipe, you have another Docker container (for the pipe) running inside the Docker container of the step (Docker-in-Docker).  
The cache is available in the latter container (of the step), but not in the Docker container of the pipe.

In order to copy the cache from the step's container to the pipe's container, you can do the following:

**1st** In the bitbucket-pipelines.yml file, before the command with the pipe, add the following commands:

```
- cp -R ~/.cache ./
- chown -R 165536:165536 $BITBUCKET_CLONE_DIR
```

The first command will copy the cache directory from the root directory, to the clone directory (of the step container).  
The second command will assign the permissions of the step's container to the pipe's container, which is needed for the 2nd step below.

**2nd** Now the cache will be present in the pipe's Docker container, but in the clone directory.  
We'll need to move it from there to the root directory of the pipe's container.

To do this, you can edit the pipe.sh file of the pipe and add the command

```
cp -R ./.cache ~/
```

This will copy the cache from the clone directory of the pipe's container to the root directory, which is where the cache should be located.

This should work, assuming that the cache is downloaded for the second step, and also assuming that the error you get is because the cache is missing.

Please feel free to let us know if this works for you. In case you're still facing issues, my suggestion would be to open a ticket with Bitbucket Cloud support team via <https://support.atlassian.com/contact/#/>, you can link this post and also provide the URL of a build that fails with this error. If there is an open ticket, the engineer working on your case will be able to access the build and logs, which will allow for faster troubleshooting.

Please feel free to let me know if you have any questions.

Kind regards,  
Theodora


Tags: bitbucket-pipelines

bitbucket-cloud

cloud

python