Push to another repository in Bitbucket Cloud Pipelines
Platform Notice: Cloud Only - This article only applies to Atlassian products on the cloud platform.
Summary
The purpose of this KB article is to provide instructions on how to push to another Bitbucket Cloud repository when running a Pipelines build via both HTTPS and SSH protocols
Solution
Environment
Bitbucket Cloud Pipelines
Solution #1 - HTTPS
The simplest method of achieving this which is described below will be to leverage our repository access token authentication method. Access tokens are designed for use within a single application with limited permissions applied to them. If you would prefer, you can also utilise an App Password in place of the access token or make use of OAuth to achieve this as well.
Essentially, you would follow the steps below - whereRepoAis your source repository andRepoBis the target repository where you wish to push a commit.
Navigate toRepoBand selectRepository Settings > Access Tokensthen click theCreate Repository Access Token button
Give the token a name and ensure that it has theWRITElevel privilege scope ticked against theRepositoriesheading (as a minimum)
Click theCreate button and ensure that you copy the token, clone command and the git config command from the resulting dialogue box and store this somewhere (as you cannot access this information again)
Now that the token is created, navigate to RepoA and selectRepository Settings > Pipelines: Repository Variables then add the token you copied earlier as a secured variable here so that it can be referenced in your build
Access yourbitbucket-pipelines.ymlconfiguration onRepoAand make use of the token repository variable you configured before in your clone command to clone theRepoBrepository and perform whatever actions you wish to commit to it
In the below example - we are cloning RepoB using the access token variable, copying a folder from RepoA to RepoB, modifying the GIT config to reference the identity that was generated earlier in Step 3 as the commit author and then committing/pushing the changes:
1
2
3
4
5
6
7
8
9
10
11
WritetoRepo:
- step:
name: "Write to another repo"
script:
- git clone https://x-token-auth:$ACCESSTOKEN@bitbucket.org/testworkspace/RepoB.git
- cp -R testfolder RepoB
- cd RepoB
- git config user.email alphanumericstring@bots.bitbucket.org
- git add -A
- git commit -a -m "Committing folder to RepoB"
- git push
NOTE: You can either use the bots.bitbucket.org email address in the git config command to reflect in the commit history that it was committed by a bot, or you can simply use your own email address if you would prefer for your user to show here instead.
Solution #2 - SSH
If you would prefer to instead use SSH rather than HTTPS, this is also possible.
Essentially, you would follow the steps below - where RepoA is your source repository and RepoB is the target repository where you wish to push a commit.
Navigate to RepoB and select Repository Settings > Pipelines > SSH Keys - then click the Generate keys button - this is to generate a private/public key pair that can be authenticated against later on in the process
Once the keys have been generated, click the Copy public key button as you will now need to store the public key at the account level for authentication purposes, you can do so by navigating to Personal Settings > SSH Keys and pasting the public key here (you could also sign in as a bot account and paste it here if you'd prefer)
Now that the keys have been generated on RepoB, and the public key is stored at the account level, there needs to be a way for RepoA to use this key in Pipelines.
Navigate to RepoA and select Repository Settings > Pipelines: Repository Variables then add the public key you copied earlier as a secured variable here so that it can be referenced in your build securely
You now have a link between the two repositories so that they can communicate with one another, and a link to your user account so that it can be used to authenticate over SSH
In the below example - we are first decrypting the secured public key variable, storing the decrypted output as a public key in the clone directory, cloning RepoB using the public key, copying a folder from RepoA to RepoB, modifying the GIT config to recognise your username as the commit author and then committing/pushing the changes:
1
2
3
4
5
6
7
8
9
10
11
12
13
WritetoRepo:
- step:
name: "Write to another repo"
script:
- mkdir -p ~/.ssh
- (umask 077 ; echo -n $SSHKEY | base64 -w 0 > ~/.ssh/id_rsa.pub)
- git clone git@bitbucket.org:testworkspace/RepoB.git
- cp -R testfolder RepoB
- cd RepoB
- git config user.email emailaddress@domain.com
- git add -A
- git commit -a -m "Committing folder to RepoB"
- git push
NOTE: You may encounter a warning message when executing your clone command, this can be ignored - the host IP for Bitbucket Cloud changes frequently and the command will still complete successfully.
If you are encountering issues following this documentation, or have any other related questions - please raise a support ticket or raise a community support ticket for further assistance.
Was this helpful?