summaryrefslogtreecommitdiff
path: root/extras/git-mirror.md
blob: 55aaaa64c8ea56cddedbe6bb0fb0e5fbfc23c87f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
GIT mirror
--

If you want simple & light self-hosted mirror of this or any other GIT repository, then you may use something 
like these examples. I assume Debian and nginx as webserver, but you can easily translate the idea to whatever 
Linux or webserver you like.

This uses gitweb as viewer and git-http-backend for HTTP clone support. Both of these tools are part of git project.

**Install dependencies**

```
apt install nginx git gitweb fcgiwrap
```

**Prepare GIT repositories and keep them up to date**

You want to use such script to first clone bare repositories and then periodically update them.

The `/your/path/vyos-build-git-mirror.sh` script:

```
#!/bin/bash
set -e

destination="/var/lib/git"
sources=(
    "https://github.com/dd010101/vyos-jenkins.git"
    "https://github.com/dd010101/vyos-build.git"
    "https://github.com/dd010101/vyos-missing.git"
)

for gitUrl in "${sources[@]}"
do
    directory=$(echo "$gitUrl" | grep -oP '([^/]+).git')
    fullPath="$destination/$directory"
    if [ -d "$fullPath" ]; then
        echo "Updating $gitUrl in $fullPath"
        git -C "$fullPath" remote update
    else
        echo "Cloning $gitUrl as $fullPath"
        mkdir -p "$fullPath"
        git -C "$fullPath" clone --mirror "$gitUrl" .
    fi
done
```

Run this first time to clone repositories:

```
chown -R www-data: /var/lib/git
su - www-data -s /bin/bash -c "/your/path/vyos-build-git-mirror.sh"
```

To keep repositories up to date execute this script periodically for example with CRON:

```
0 * * * * www-data /your/path/vyos-build-git-mirror.sh
```

**gitweb configuration**

We want to change `$projectroot` and also it's good idea to add `@git_base_url_list` so the gitweb shows
repository URL meant for cloning.

Modify `/etc/gitweb.conf`:

1) Update `$projectroot` to `/var/lib/git` (`$projectroot = "/var/lib/git"`) - if not already set.
2) Add `@git_base_url_list` with `https://git.some.tld` value (`@git_base_url_list = "https://git.some.tld"`).

**nginx vhost**

Replace `git.some.tld` with your domain and also adjust HTTPS certificates as needed. You may as well use whatever
configuration you like and just pickup location blocks to make git side work with your favorite nginx vhost.

```
server {
    listen 80;
    listen [::]:80;

    server_name git.some.tld;

    location / {
        return 301 https://$host$request_uri;
    }
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;

    ssl_certificate /etc/letsencrypt/live/git.some.tld/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/git.some.tld/privkey.pem;

    server_name git.some.tld;

    root /usr/share/gitweb;
    
    index index.cgi;

    location / {
        try_files $uri $uri/ =404;
    }

    location /index.cgi {
        include fastcgi_params;
        fastcgi_param SCRIPT_NAME $uri;
        fastcgi_param GITWEB_CONFIG /etc/gitweb.conf;
        fastcgi_pass unix:/var/run/fcgiwrap.socket;
    }

    location ~ \.git/ {
        client_max_body_size 0;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME /usr/lib/git-core/git-http-backend;
        fastcgi_param GIT_HTTP_EXPORT_ALL "";
        fastcgi_param GIT_PROJECT_ROOT /var/lib/git;
        fastcgi_param PATH_INFO $uri;
        fastcgi_param LANGUAGE en_US.UTF-8;
        fastcgi_pass unix:/var/run/fcgiwrap.socket;
    }
}
```

The `git-http-backend` will use your system language and will send messages in this language to whoever
interacts with it - like with git clone. English-speaking GIT server is more common and that's why I would
recommend using `en_US.UTF-8`. If you have other system locale then you can add `en_US.UTF-8` as secondary
via `dpkg-reconfigure locales` - pick your locale as default and `git-http-backend` can use english via `LANGUAGE`
variable.

This gives your `https://git.some.tld` for viewing and `git clone https://git.some.tld/repository.git` support.
The gitweb viewer shows URL for cloning for each repository in summary section.