How to push LFS files using Bitbucket Pipelines?
Platform Notice: Cloud Only - This article only applies to Atlassian apps on the cloud platform.
Summary
This article focuses on the process of pushing LFS files to a Bitbucket repository from Bitbucket Pipelines.
Diagnosis
If you encounter an error while attempting to push an LFS file from a Bitbucket Pipeline, consider the following sample YAML configuration:
image: atlassian/default-image:4
clone:
lfs: true
pipelines:
default:
- step:
name: Push LFS file from Bitbucket Pipeline
script:
- echo "Hello World" >> Hello.json
- apt-get update
- apt-get upgrade -y
- apt-get install -y git-lfs
- git add . && git commit -m "[skip ci] LFS Push"
- git pushIn the above configuration, we are doing the following -
Creating a JSON file
Installing LFS
Pushing the JSON file in Bitbucket repository as an LFS object
We assume that json files are marked as LFS in .gitattributes.
However, the above configuration will fail with the following error -
git push
fatal: could not read Username for 'https://bitbucket.org': No such device or address
fatal: could not read Username for 'https://bitbucket.org': No such device or address
fatal: could not read Username for 'https://bitbucket.org': No such device or address
Git credentials for https://bitbucket.org/%7B%7D/%7B<repo_UUID>%7D/info/lfs/object/verify?upload_id=<upload-ID> not found.
error: failed to push some refs to 'http://bitbucket.org/<workspace-ID>/<repo-slug>'Cause
The reason for the failure is that the default authentication method does not allow the LFS push. You will also notice that the workspace ID is not filled in the URL in the error message -
https://bitbucket.org/%7B%7D/%7B%7D/%7B<repo_UUID>%7D/info/lfs/object/verify?upload_id= which when decoded translates to -
https://bitbucket.org/{}/{repo-UUID}/info/lfs/object/verify?upload_id=<ID>
You can find more details about this issue from BCLOUD-23180.
Solution
To resolve this issue, consider using an alternative authentication method. Two viable options are outlined below:
Utilize your Bitbucket username (or the static username
x-bitbucket-api-token-auth) and an API token with a scope of at least write:repository:bitbucket for authentication. Modify your git push command as follows:git push https://$username:$api_token@bitbucket.org/<workspace-id>/<repo-ID>.gitThis approach leverages specific Bitbucket username and API token values that can be provided through user-defined variables.
SSH-based authentication also serves as a valid solution. Adjust your repository's remote URL to point towards the SSH URL with this command:
git remote set-url origin "$BITBUCKET_GIT_SSH_ORIGIN"
Was this helpful?