Restored cache is incomplete in Bitbucket pipeline

Still need help?

The Atlassian Community is here for you.

Ask the community


Platform Notice: Cloud Only - This article only applies to Atlassian products on the cloud platform.

 

Summary

When utilizing cache in a Bitbucket pipeline, it is important to note that occasional discrepancies may arise between the files uploaded to the cache and the files extracted for use in subsequent builds.

Diagnosis

  • In Bitbucket pipelines, the default user utilized for file extraction is the root user.
  • If an image is employed with a different user, certain files may only allow read and write permissions for the owner, specifically the non-root user. Consequently, this setup restricts the root user from accessing these files.

Example:

In the below example, we are using cimg/ruby docker image in the pipeline. This image has circleci as the default user setup. When the Cache files are generated during the installation process in the pipeline build, those files are getting generated using the same circleci user as the owner.

There are few files that have the associated permission set to rx------- (only owner can read those files), few files are  shown below:

During the build teardown process, cache files are compressed by the user circleci, and all files are then uploaded for caching purposes.

However, when these caches are later extracted during the build setup in subsequent steps, the files are always extracted using the root user (not the circleci user). As the files have read and write permissions only for the owner, the root user does not have access to these files. Consequently, these files are not extracted and therefore not available for use during the build process. This discrepancy in the number of files uploaded and extracted in the cache is due to this reason.

  • When cache is first generated with circleci user as the owner, we can see 880 files in the cache

  • When the same cache is being used in the next step, instead of 880 files, only 860 files are extracted.

Cause

This problem arises due to the fact that the image utilized during cache creation is associated with a non-root user. Consequently, in subsequent builds, a root user is responsible for extracting the files, leading to a noticeable discrepancy.

Solution

There exist two solutions to address the problem at hand.

  • One approach is to employ an image where the default user during the build process is set as root. An illustration of this concept can be observed in an alternative image such as bitnami/ruby which adopts root as the default user.

  • Another method involves specifying root as the user during runtime by utilizing the syntax provided below.

image:
  name: cimg/ruby
  run-as-user: 0




Last modified on Jun 18, 2024

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.