Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Manually clone repository in Pipeline using OAuth Token

Advitum
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
March 25, 2025

I am trying to optimise a build step in my pipeline by doing only a partial clone of the repository.

For context: I got some large media files in the repository which are not neede when building JS and CSS resources. So I am trying to exclude them from from being cloned and checked out.

In a related thread (https://community.atlassian.com/forums/Bitbucket-questions/Can-I-clone-the-repository-in-a-pipeline-only-once-if-the-runner/qaq-p/2584299) someone from the Atlassian Team suggests the following:

In this case, I believe the best approach would be to disable clone in your YAML entirely and just manually clone your repository in the first step.

Sadly, they do not elaborate on how exactly to go about manually cloning the repository.

I tried including the clone command from the pipeline log in my step script like this:

defitions:
steps:
- step: &build
name: Build
image: larsebert/php8-composer-dartsass:latest
script:
- git clone -n --filter=blob:none --depth=1 https://x-token-auth:$REPOSITORY_OAUTH_ACCESS_TOKEN@bitbucket.org/$BITBUCKET_REPO_FULL_NAME.git $BUILD_DIR
- cd $BUILD_DIR
- git sparse-checkout set --no-cone '/*' '!/webroot/media'
- git checkout --force $BITBUCKET_COMMIT
- composer install --no-scripts
- mkdir webroot/js && php bin/cake.php asset_compress build
- mkdir webroot/css && sass --no-source-map --style=compressed resources/scss:webroot/css

However, this results in the error message "remote: Invalid credentials" since apparently the variable `$REPOSITORY_OAUTH_ACCESS_TOKEN` is not available in the build script.

Is there any easy way to gain access to the repository?

By the way: The ability to do a sparse checkout like this using the pipeline clone options instead of doing a manual clone would be awesome. I imagine this would be a common use case.

1 answer

0 votes
Ben
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
March 26, 2025

Hi Advitum,

You don't actually need to disable the clone to specify the clone depth that is performed, you can simply specify the clone depth in your YAML configuration - for example:

pipelines:

   default:
       - step:
             clone:
                depth: 1
             script:
                 - echo "Hello world!"
If for whatever reason you do in fact need to disable the automatic clone - this is also handled in the clone options within your YAML:

 

pipelines:

   default:
       - step:
             clone:
                enabled: false
             script:
                 - echo "Hello world!"
Note that you will need to store the OAuth access token in a seperate repository variable and call that variable in your script instead, you can generate one manually by following our guide below:

Please let me know if you require further assistance.

Cheers!

- Ben (Bitbucket Cloud Support)

 

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PERMISSIONS LEVEL
Product Admin Site Admin
TAGS
AUG Leaders

Atlassian Community Events