Copy a folder from another repository and a specific branch to publish it
Asked by mcamprecios:

Hello!

It's my first time dealing with Bitbucket pipelines, so I may be very lost here.

We have a repository the creates a \<build\> directory. **We would like to take this directory, from a specific branch, and add it to another repository, so it can be published as a Bitbucket public page.**

I de believe we want to do something like what follows, but I don't know where to start and I'm not sure this is the best approach to have it published.

```
image: atlassian/default-image:3

pipelines:
 default:
  - step:
   script:
    - git clone --branch development --single-branch https://xxxxxxxxx@bitbucket.org/<workspace-id>/<repo>.git
    - 'delete all files but the <build> directory'
    - 'copy contents from <build> to root'
    - 'delete <build> directory'
```

We would basically clone the whole repositoy, specifing the branch. Because it seems we can't just get the directory.

Does it make sense? Could you give some advice?

Thanks a lot in advance!


Answers:
author: Theodora Boudale
body: Hi [@mcamprecios](/t5/user/viewprofilepage/user-id/5057978) and welcome to the community!

I would like to ask for some clarification, so we can better help you:

You have in your post an example of a **bitbucket-pipelines.yml** file, let's say that this file is in repo A.

**(1)** You mention
> We have a repository that creates a \<build\> directory

Where exactly is this build directory? Is it in the source code of a different Bitbucket Cloud repo, let's say repo B?  
Or is it in the source code of repo A but only on a specific branch of repo A?

**(2)** Regarding your end goal, you mention
> and add it to another repository, so it can be published as a Bitbucket public page.

Do you want to then push the build directory in a Bitbucket Cloud repo where you are hosting a website, as per our doc [here](https://support.atlassian.com/bitbucket-cloud/docs/publishing-a-website-on-bitbucket-cloud/)?

If so, is the repository hosting the website repo A, repo B, or a completely different repo?

Just a heads up, I removed the workspace-id and repo name from your post to protect your privacy.

Kind regards,  
Theodora

comments: author: mcamprecios
body: Massive thanks in advance Theodora!

I have dig a little deeper and I may be able to explain it a bit better:

We have this structure:

```
- workspace

--- repository "front-end"
------ branch master
------ branch development (<build> is here)

--- repository "workspace.bitbucket.io"
```

We would like to publish the \<build\> folder of the branch "development", hosted in the repository "front-end", to the repostority "workspace.bitbucket.io". Every time there is a commit.

But at this point, I'm sure it is the best approach...

Maybe, if that is too complex, it would be better to just work from "workspace.bitbucket.io" and use the main branch for development. So it would be directly available at "workspace.bitbucket.io/build".

Does it make sense?


author: Theodora Boudale
body: Hi [@mcamprecios](/t5/user/viewprofilepage/user-id/5057978),

Thank you for the details, this is more clear now.

If you want the build to run on every commit to either master or development branch, you could define a different pipeline for each branch. The \<build\> directory already exists on development branch, but you would need some extra steps to make it available in builds that run on master branch.

I am sharing below an example bitbucket-pipelines.yml file:

```
image: atlassian/default-image:3

pipelines:
  branches:
    master:
      - step:
          clone:
            depth: full 
          script:
            - echo "Your build and test goes here..."
            - git checkout development
    development:
      - step: 
          script:
            - echo "Your build and test goes here..."
```

For builds that run on master, I have defined full-depth clone. This allows us to do **git checkout development** in the script, and when you checkout the development branch, the \<build\> directory will be available in the directory of your pipelines build. For builds that run on development branch, the \<build\> directory will be present anyway.

Now, for the second part of your request, publishing the \<build\> directory to the "workspace.bitbucket.io" repo, do you want to commit and push this directory to the source code of "workspace.bitbucket.io"?

If so, this should be possible by including commands in both scripts that

* clone the repository "workspace.bitbucket.io" in the directory of your pipelines build
* copy the \<build\> directory inside the clone of "workspace.bitbucket.io"
* commit the changes in the clone of "workspace.bitbucket.io" and push to "workspace.bitbucket.io"

Authentication needs to be set up, this could be either with the credentials of a Bitbucket account or with OAuth.

Is this what you are looking for, and would you like me to share more details on how to set up authentication and what commands should be used in the scripts?

Kind regards,  
Theodora


author: mcamprecios
body: Massive thanks Theodora for your detailed comments.

This gives us a clear idea of how to do it. We didn't anticipate all these steps though, so we will give it a second thought.

And I managed to publish it via FTP and got an understanding of how the pipelines work now.

Thanks again!


author: Theodora Boudale
body: You are very welcome [@mcamprecios](/t5/user/viewprofilepage/user-id/5057978)!

This does require quite a few steps indeed. If it is too troublesome, you could also work from "workspace.bitbucket.io" repo as you mentioned in your previous reply. Each static website will generate content from the main branch, but you can also work out of a development branch on this repo, which you can merge to the main branch when it is ready.

If you need anything further, please don't hesitate to reach out!


Tags: bitbucket-cloud

clone

cloud

publish