How to cache the PMD plugin in my pipeline instead of downloading everytime ?
Asked by vanessen munisamy:

I have read recently about cache in pipeline that help to cache dependencies and avoid downloading same files each time. Well I have such a case but do not know how to use the caching in my case.

Here is my dependency codes :  
1. check-only:  
- step:  
script:  
- source scripts/shell/pipeline-sgd.sh  
# Download PMD  
- wget <https://github.com/pmd/pmd/releases/download/pmd_releases%2F$PMD_VERSION/pmd-bin-$PMD_VERSION.zip>  
- unzip pmd-bin-$PMD_VERSION.zip  
...........................  
The part that I want to cache is the PMD zip download


Answers:
author: Patrik S
body: Hello [@vanessen munisamy](/t5/user/viewprofilepage/user-id/4884590) ,

Thank you for reaching out to Atlassian Community!

In order for the cache to work, it's also necessary that the tool/command you are using first checks if the dependency is already available locally before trying to download it again. Usually, when using development frameworks such as node/npm, those tools will first look in a default folder locally to check if the dependencies already exist, and if not, try to download them from the internet.

However, since you are directly using a **wget** command to download your dependency, we would need to add a condition for the download to just be actually executed when the file is not present in the cache. See the example step below :

```
pipelines:

  default:
    - step:
        name: test
        caches:
         - pmdcache
        script:
          - echo "testing"
          - if [ ! -f "./pmd_download_folder/pmd-bin-7.0.0-rc1.zip" ]; then wget https://github.com/pmd/pmd/releases/download/pmd_releases%2F7.0.0-rc1/pmd-bin-7.0.0-rc1.zip -O pmd_download_folder/pmd-bin-7.0.0-rc1.zip; fi
          - <rest of your commands>
          
definitions:
  caches:
    pmdcache: pmd_download_folder
```

In this example, we are defining a custom cache named **pmdcache** that will cache all the content of the path **pmd_download_folder**(this is relative to $BITBUCKET_CLONE_DIR).

Then, in the script section, we are checking if the dependency file - in this case pmd-bin-7.0.0-rc1.zip - already exists inside the **pmd_download_folder**(the folder where we configured the custom cache).

If the file exists, it would mean it was automatically restored from the cache of a previous build, then we won't need to download it again and the script will continue to the next command. However, if the file does not exist, it means the file is not on the cache (otherwise it would have been restored during Build Setup), then the **wget** command will be executed to download the file and save it inside the **pmd_download_folder** folder. Once the step is completed, bitbucket will look for that directory and cache any files within it. In the subsequent builds, the cached files will be automatically restored in the same path.

It's important to note that caches last for 7 days and can have up to 1GB compressed. After 7 days, your next pipeline run will download the dependency again from the internet, and it will be stored in the bitbucket cache for another 7 days, and so on.

For more details about caches, you can also refer to our official documentation below :

* <https://support.atlassian.com/bitbucket-cloud/docs/cache-dependencies/>

Hope that helps!

Thank you, [@vanessen munisamy](/t5/user/viewprofilepage/user-id/4884590) !

Patrik S


Tags: bitbucket-cloud

bitbucket-pipelines

cache

cloud

dependency