Repository Mirror with OneDev
Tags: devops, git, github
Published: 2022-05-04 07:27:50

# Introduction

It is often desirable to mirror repositories between OneDev and other hosting services. For instance you may want to push changes to a copy at GitHub for public access, or want to pull back any changes contributed by others at GitHub.

[OneDev](https://onedev.io) 7.1 made this task trivial with the new repository sync steps. Let’s see how to achieve this.

### Preparation

First of all, start OneDev by running below command:

```
$ docker run -t --rm -v /var/run/docker.sock:/var/run/docker.sock -v $(pwd)/onedev:/opt/onedev -p 6610:6610 -p 6611:6611 1dev/server
```

Point your browser to _localhost:6610_ to setup OneDev, and create a test project.

Then create an empty repository at GitHub side to be used as mirror of our test repository, assume the url is: _https://github.com/youraccount/test_

### Push Changes to Mirror

Now we configure OneDev to push changes to GitHub mirror as long as there is a change in branch or tag:

1. Login to your GitHub account and generate a personal access token able to push to the mirror repository

2. At OneDev side, create a job secret in the test project and use access token above as its value:

    ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s05g69x60sdob6kgwzqm.png)

3. Define build spec to add a job say _Push to GitHub_, and add a step of type Repository _Sync / Push to Remote_. For _password / access token_ field, choose the job token defined above:

    ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kgypo4gp5udi9z2m4yaf.png)

4. Add triggers to run job automatically when branch is updated or tag is created:

    ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tkq1hl0nopc1w81qxaup.png)

Now save the build spec and the job will run automatically to push changes to GitHub mirror. Check at GitHub side, and you will see it contains newly created file _.onedev-buildspec.yml_. Experiment with other changes such as creating new branch/tag to see the result.

### Pull Changes from Mirror

Our GitHub mirror may accept pull requests from contributors, so let’s set up OneDev to pull changes from it:

1. Continue to edit build spec above, add a job say _Pull from GitHub_, and add a step of type _Repository Sync / Pull from Remote_ like below:


    ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l67ziaithai8ypch2ffa.png)

Here we use the same job token defined above to access GitHub mirror (access token can be left empty if the mirror is publicly accessible).

2. Add a job trigger of cron type to run job on scheduled time, for instance, on 1:00AM every night:

    ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5ytan9e8yo6gaynhx7k0.png)

3. If your OneDev instance can be accessed publicly, you can configure GitHub to trigger this pull job upon mirror changes. This can be done by creating a webhook at GitHub side like below:

    ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/41zk3bdao6ud0mb2t7sn.png)

The payload url is defined as:
```
http://onedev.example.com/api/trigger-job?project=test&job=Pull%20from%20GitHub&branch=master&access-token=<OneDev access token>
```
Here _onedev.example.com_ is dns name accessing your OneDev instance. Job name is url encoded as it contains space. _<OneDev access token>_ should be replaced by access token of a OneDev user with permission to run the job like below::

    ![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rkjcdnaopfq3oych4xea.png)

For production use, it is important to enable https protocol for payload url to avoid leaking of access token.

Save the webhook and OneDev should be pulling from GitHub mirror whenever it is changed.

### Conclusion

We’ve completed setup of a two-way sync of OneDev repository and its GitHub mirror. Since it is just an ordinary CI job, we can subscribe certain build query to be notified of sync failures, check build log to investigate sync failures, or even configure job resource setting to control resource usage of multiple sync jobs, etc.

Thanks for reading!