How do I use the cache section in a pipeline as code?

Modified on Wed, 21 Jun 2023 at 04:20 PM

The cache section in a pipeline as code allows you to cache specific files or directories between job executions, which can significantly improve the performance of your pipeline by reducing the need to fetch dependencies or rebuild artifacts from scratch. Caching is especially useful when you have dependencies that don't change frequently or require a significant amount of time to download or build.


To use the cache section in your pipeline as code, follow these steps:


Specify the files or directories you want to cache using the paths keyword. For example:


job:
  script:
    - echo "Running job"
  cache:
    paths:
      - vendor/
      - node_modules/


In the above example, the vendor/ and node_modules/ directories will be cached between job executions.


Optionally, you can define a unique key for the cache entry. The key allows you to differentiate cache entries based on specific criteria. For example, you might want to use different cache keys based on the version of your dependencies or the branch being built. Here's an example:


job:
  script:
    - echo "Running job"
  cache:
    key: "${CI_COMMIT_REF_SLUG}-${CI_JOB_NAME}"
    paths:
      - vendor/


In this case, the cache key is generated based on the commit ref slug and the job name. This ensures that each combination of ref and job has its own cache entry.


Optionally, you can specify a policy for the cache. The available cache policies are pull-push, pull, and push. The default is pull-push, which means the cache will be fetched from the previous job and pushed to the next job. If you only want to fetch the cache without pushing changes, use pull. If you only want to push changes to the cache without fetching, use push. 

Here's an example:


job:
  script:
    - echo "Running job"
  cache:
    policy: pull
    paths:
      - vendor/


By using the cache section in your pipeline as code, you can dramatically improve the performance of your jobs by reusing previously cached dependencies or artifacts. This reduces the time spent on downloading, building, or preparing resources, resulting in faster and more efficient pipeline executions.


Note that the availability and behavior of the cache section may depend on the version of GitLab you are using. Make sure to refer to the GitLab documentation or release notes for the specific version you are working with to ensure compatibility and understand any limitations or special considerations.


Using the cache section in your pipeline as code is a powerful technique to optimize your CI/CD workflows, reduce build times, and improve overall development efficiency.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select atleast one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article