Skip to content
This repository was archived by the owner on Sep 3, 2024. It is now read-only.

Commit b267af3

Browse files
committed
🎉 code published
1 parent 7cef14c commit b267af3

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

README.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Github to Gitlab mirror repository in PHP
2+
3+
The idea here is to mirror the repository using crontab every minute.
4+
- As you all know about [Gitlab Mirror Issue](https://gitlab.com/gitlab-com/support-forum/-/issues/5331).
5+
- Now in new premium update gitlab had stopped the mirroring of external repository making it's a [premium feature](https://about.gitlab.com/releases/2020/03/12/free-period-for-cicd-external-repositories/), so this is a replication of that feature.
6+
7+
## Installation
8+
9+
Just make sure you have self hosted server or local linux environment.
10+
11+
Also make sure you have Github and Gitlab ssh installed.
12+
13+
* ##### Set `crontab -e`
14+
15+
```bash
16+
* * * * * sh /path/to/mirror.sh >/dev/null 2>&1
17+
```
18+
* Edit mirror.php and set appropriate git ssh config for repository on line number 19 and 22
19+
20+
## Any cause of failure on self-hosted server:
21+
22+
- The public ssh key of www-data should have read and write access to source repository.
23+
- If the SSH server is not trusted the connection will fail.
24+
- To avoid non trusted connection to server, run `ssh -T [email protected]` and `ssh -T [email protected]` at least once to get its fingerprint into the local SSH configuration or hosted server ssh configuration.
25+
26+
* More info on duplicating the repository [Duplicating Repository](https://help.github.com/en/github/creating-cloning-and-archiving-repositories/duplicating-a-repository)
27+
28+
## Contributing
29+
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
30+
31+
## License
32+
[MIT](https://choosealicense.com/licenses/mit/)

mirror.php

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
/**
3+
* Github to Gitlab mirror
4+
*
5+
* Author: Deepender Choudhary
6+
*
7+
* License Mit https://choosealicense.com/licenses/mit
8+
*/
9+
10+
ini_set('max_execution_time', INF); //SET EXECUTION TIME HERE IT'S INIFINITE
11+
12+
date_default_timezone_set('Asia/Kolkata'); // SET Your Timezone
13+
14+
$__DIR = str_replace('\\', '/', realpath(__DIR__ .'/')); //SCRIPT PATH
15+
16+
define('BASEPATH', $__DIR);
17+
18+
//set here which repository to fetch
19+
define('SOURCE_REPOSITORY', '[email protected]:example/example.git');
20+
21+
//set here which repository to push
22+
define('TARGET_REPOSITORY', '[email protected]:user/example.git');
23+
24+
// local git cache directory
25+
define('LOCAL_CACHE', BASEPATH.'/git-mirror-cache/repo.git');
26+
27+
define('GIT_LOGS', BASEPATH.'/gitlogs');
28+
29+
if (!file_exists(LOCAL_CACHE)) {
30+
mkdir(LOCAL_CACHE);
31+
}
32+
33+
if (!file_exists(GIT_LOGS)) {
34+
mkdir(GIT_LOGS);
35+
}
36+
37+
chdir(LOCAL_CACHE);
38+
39+
$commands = array();
40+
41+
if (!is_dir(sprintf('%s/%s', LOCAL_CACHE, 'refs'))) {
42+
$commands[] = sprintf('git clone --bare %s %s', SOURCE_REPOSITORY, LOCAL_CACHE);
43+
} else {
44+
$commands[] = sprintf('git fetch --prune origin');
45+
}
46+
47+
$commands[] = sprintf('git push --mirror %s', TARGET_REPOSITORY);
48+
49+
$out = []; $i =1;
50+
foreach ($commands as $command) {
51+
$out[$i] = "Executing $command ";
52+
$tmp = pipeExecute($command.' 2>&1');
53+
$i++;
54+
$out[$i] = $tmp;
55+
$i++;
56+
}
57+
58+
$currentTime = date( 'd-m-Y--h-i-s-a', time () );
59+
60+
file_put_contents(BASEPATH.'/gitlogs/git-'.$currentTime.'.txt', var_export($out, true)."\n\n"); //git push mirror array logs
61+
62+
/**
63+
* Execute cmds in shell
64+
* @return array
65+
*/
66+
function pipeExecute($cmd, $input='') {
67+
$proc = proc_open($cmd, array(
68+
0 => array('pipe','r'),
69+
1 => array('pipe','w'),
70+
2 => array('pipe','w')
71+
), $pipes);
72+
73+
fwrite($pipes[0], $input);
74+
fclose($pipes[0]);
75+
76+
$stdout = stream_get_contents($pipes[1]);
77+
fclose($pipes[1]);
78+
79+
$stderr = stream_get_contents($pipes[2]);
80+
fclose($pipes[2]);
81+
82+
$rtn = proc_close($proc);
83+
84+
return array(
85+
'stdout'=>$stdout,
86+
'stderr'=>$stderr,
87+
'return'=>$rtn
88+
);
89+
}

mirror.sh

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/php
2+
3+
php "$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"'/mirror.php';

0 commit comments

Comments
 (0)