How to push LFS files using Bitbucket Pipelines?
Platform Notice: Cloud - This article applies to Atlassian products on the cloud platform.
Summary
This knowledge base 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 push
In 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 HTTPS Username and App Password or Access Token for authentication. Modify your git push command as follows:
git push https://$username:$app_password@bitbucket.org/<workspace-id>/<repo-ID>.git ## or git remote set-url origin https://x-token-auth:<ACCESS TOKEN>@bitbucket.org/<workspace-id>/<repo-ID>.git
This approach leverages specific username and app password values that can be provided through 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"