How to integrate Gitlab Git repository into Redmine
Setting up the Redmine Git infrastructure
- Copy the access token to your clipboard and save it somewhere safe. You won’t be able to see it again.
The following is based on Redmine docker-compose config with Traefik & Let’s Encrypt
- Create a new directory to hold Git repositories for your Redmine instance.
mkdir redmine_git
- Add that to the
docker-compose.yml
so it’s mapped inside the container
services:
redmine:
# ...
volumes:
# ...
- './redmine_git:/git'
Cloning a Gitlab repository into Redmine
Repeat this for every repository you want to integrate into Redmine.
- In Gitlab, open the project and goto
Settings
->Access Tokens
- Create an
Access Token
with the following settings:- You can name it
Redmine
, but the name doesn’t matter, so feel free to choose any other name. - As
Role
, selectDeveloper
- As
Expiration Date
, I recommend deleting the value by clicking thex
icon, so the token never expires. Otherwise, you would have to regularly re-create tokens for all repositories. - As
Scopes
, selectread_repository
and nothing else.
- You can name it
- Now go to the project’s page on Gitlab and copy the Git URL, e.g.
https://gitlab.mydomain.com/myuser/MyProject.git
- Edit that URL and add
username:password@
afterhttps://
with username being the name of the token (e.g.Redmine
) and the password being the token you copied earlier.
https://Redmine:[email protected]/myuser/MyProject.git
- Now clone that URL as a mirror repository into the
redmine_git
directory
cd redmine_git
git clone --mirror https://Redmine:[email protected]/myuser/MyProject.git
- In redmine, navigate to the project you want to integrate the repository into, go into
Settings
->Repositories
and click onNew Repository
:
- Enter the path to the repository, e.g.
/git/MyProject.git
and a new identifier which may contain only lowercase letters, numbers, and underscores. This identifier is used to reference the repository in Redmine’s database.
- Click on
Create
and you’re done!
Setup automatic repository fetch
Redmine currently does not support fetching repositories automatically. You can set up a cronjob to fetch repositories every 5 minutes.
I recommend to use SystemD timers for this, as they are more reliable and generally more flexible than cronjobs.
In the directory where redmine_git
resides, create a new file setup-git-fetch.sh
, which sets up the fetch script and timer:
#!/bin/bash
export NAME=$(basename $(pwd))-gitfetch
cat >/etc/systemd/system/${NAME}.service <<EOF
[Unit]
Description=${NAME}
[Service]
Type=oneshot
ExecStart=/bin/bash -c 'cd redmine_git && for i in *; do [ -d "\$i" ] && cd "\$i" && git fetch --all; cd ..; done'
WorkingDirectory=$(pwd)
EOF
cat >/etc/systemd/system/${NAME}.timer <<EOF
[Unit]
Description=${NAME} timer
[Timer]
OnCalendar=*:0/2
Persistent=true
[Install]
WantedBy=timers.target
EOF
# Enable and start service
systemctl enable --now ${NAME}.timer
Run that script from the directory where redmine_git
resides:
sudo bash setup-git-fetch.sh
This script sets up a SystemD service and timer that fetches all repositories in redmine_git
every 2 minutes.