Forgejo

Forgejo is a self-hosted Git service with a functionality similar to GitHub, GitLab and BitBucket. It’s a hard-fork of Gitea and uses the same MIT licence. Like most applications written in Go it’s easy to install.


Note

For this guide you should be familiar with the basic concepts of

Prerequisites

You’ll need your MySQL credentials. Get them with my_print_defaults:

[isabell@stardust ~]$ my_print_defaults client
--default-character-set=utf8mb4
--user=isabell
--password=MySuperSecretPassword
[isabell@stardust ~]$

We need a database:

[isabell@stardust ~]$ mysql --execute "CREATE DATABASE ${USER}_forgejo"
[isabell@stardust ~]$

We can use the uberspace or your own domain:

[isabell@stardust ~]$ uberspace web domain list
isabell.uber.space
[isabell@stardust ~]$

Installation

Download

Check current version of Forgejo at releases page:

[isabell@stardust ~]$ mkdir ~/forgejo
[isabell@stardust ~]$ wget -O ~/forgejo/forgejo-7.0.3 https://codeberg.org/forgejo/forgejo/releases/download/v7.0.3/forgejo-7.0.3-linux-amd64
[...]
Saving to: ‘/home/isabell/forgejo/forgejo-7.0.3’

100%[=======================================================>] 107.083.600 57.6MB/s   in 1.8s

2024-05-31 15:12:52 (57.6 MB/s) - ‘forgejo-7.0.3’ saved [107083600/107083600]

[isabell@stardust ~]$

Set permissions

Make the downloaded binary executable:

[isabell@stardust ~]$ chmod u+x ~/forgejo/forgejo-7.0.3
[isabell@stardust ~]$ ln -fs ~/forgejo/forgejo-7.0.3 ~/forgejo/forgejo
[isabell@stardust ~]$

Configuration

We will need to create some random characters as a security key for the configuration:

[isabell@stardust ~]$ ~/forgejo/forgejo generate secret SECRET_KEY
<RANDOM_64_CHARACTERS_FROM_GENERATOR>
[isabell@stardust ~]$

Copy or save the output for later.

Forgejo configuration file

Create a custom directory for your configurations:

[isabell@stardust ~]$ mkdir --parents ~/forgejo/custom/conf/
[isabell@stardust ~]$

Create a config file ~/forgejo/custom/conf/app.ini with the content of the following code block:

Note

Replace isabell with your username, fill the database password PASSWD = with yours and enter the generated random into SECRET_KEY =.

[server]
DOMAIN               = isabell.uber.space
ROOT_URL             = https://%(DOMAIN)s
OFFLINE_MODE         = true ; privacy option.
LFS_START_SERVER     = true ; Enables Git LFS support

[database]
DB_TYPE  = mysql
NAME     = isabell_forgejo
USER     = isabell
PASSWD   = <MySQL_PASSWORD>

[security]
INSTALL_LOCK        = true
MIN_PASSWORD_LENGTH = 8
PASSWORD_COMPLEXITY = lower ; This allows well to remember but still secure passwords
SECRET_KEY          = <RANDOM_64_CHARACTERS_FROM_GENERATOR> ; the before generated security key

[service]
DISABLE_REGISTRATION       = true ; security option, only admins can create new users.
SHOW_REGISTRATION_BUTTON   = false
REGISTER_EMAIL_CONFIRM     = true
DEFAULT_ORG_VISIBILITY     = private ; [public, limited, private]
DEFAULT_KEEP_EMAIL_PRIVATE = true
NO_REPLY_ADDRESS           = noreply.isabell.uber.space

[mailer]
ENABLED     = true
PROTOCOL    = sendmail ; [smtp, smtps, smtp+starttls, smtp+unix, sendmail, dummy]
FROM        = isabell@uber.space

Note

This config block contains a secure and convenient basic configuration. You may change it depending on your needs and knowledge. See the Forgejo documentation and the Forgejo configuration sample for more configuration possibilities.

Database initialization

Migrate the database configurations:

[isabell@stardust ~]$ ~/forgejo/forgejo migrate
2024/05/31 15:20:20 cmd/migrate.go:33:runMigrate() [I] AppPath: /home/isabell/forgejo/forgejo
2024/05/31 15:20:20 cmd/migrate.go:34:runMigrate() [I] AppWorkPath: /home/isabell/forgejo
2024/05/31 15:20:20 cmd/migrate.go:35:runMigrate() [I] Custom path: /home/isabell/forgejo/custom
2024/05/31 15:20:20 cmd/migrate.go:36:runMigrate() [I] Log path: /home/isabell/forgejo/log
2024/05/31 15:20:20 cmd/migrate.go:37:runMigrate() [I] Configuration file: /home/isabell/forgejo/custom/conf/app.ini
2024/05/31 15:20:20 ...2@v2.27.1/command.go:272:Run() [I] PING DATABASE mysql
2024/05/31 15:20:20 ...dels/db/collation.go:178:preprocessDatabaseCollation() [W] Current database has been altered to use collation "utf8mb4_bin"
[isabell@stardust ~]$

Forgejo admin user

Set your admin login credentials:

[isabell@stardust ~]$ ADMIN_USERNAME=AdminUsername
[isabell@stardust ~]$ ADMIN_PASSWORD='SuperSecretAdminPassword'
[isabell@stardust ~]$ ~/forgejo/forgejo admin user create --username ${ADMIN_USERNAME} --password ${ADMIN_PASSWORD} --email ${USER}@uber.space --admin
New user 'AdminUsername' has been successfully created!
[isabell@stardust ~]$

Note

Forgejo does not allow admin as name, of course you should choose and replace the password.

Finishing installation

Service for Forgejo

To keep Forgejo up and running in the background, you need to create a service that takes care for it. Create a config file ~/etc/services.d/forgejo.ini for the service:

[program:forgejo]
directory=%(ENV_HOME)s/forgejo
command=%(ENV_HOME)s/forgejo/forgejo web
startsecs=30
autorestart=yes

After creating the configuration, tell supervisord to refresh its configuration and start the service:

[isabell@stardust ~]$ supervisorctl reread
SERVICE: available
[isabell@stardust ~]$ supervisorctl update
SERVICE: added process group
[isabell@stardust ~]$ supervisorctl status
SERVICE                            RUNNING   pid 26020, uptime 0:03:14
[isabell@stardust ~]$

Note

The status of forgejo must be RUNNING. If its not check the log output at ~/logs/supervisord.log and the configuration file ~/forgejo/custom/conf/app.ini.

Uberspace web backend

Note

forgejo is running on port 3000.

To make the application accessible from the outside, configure a web backend:

[isabell@stardust ~]$ uberspace web backend set / --http --port <port>
Set backend for / to port <port>; please make sure something is listening!
You can always check the status of your backend using "uberspace web backend list".
[isabell@stardust ~]$

Done. We can point our browser to https://isabell.uber.space/.

Installed files and folders are:

  • ~/forgejo/

  • ~/etc/services.d/forgejo.ini

Forgejo SSH setup

In order for users to be able to push to your Forgejo instance, they need to add their SSH keys via the menu in the upper right corner: -> Settings -> SSH/GPG Keys -> Add Key. Forgejo automatically writes an SSH key command for each added SSH key into the /home/isabell/.ssh/authorized_keys file. The key line is something similar like:

command="/home/isabell/forgejo/forgejo --config=/home/isabell/forgejo/custom/conf/app.ini serv key-1",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty,no-user-rc,restrict ssh-ed25519 AAAAC... youruser@yourhost

Warning

If you’re using the same SSH key for authentication to Uberspace and Forgejo, you need to modify the server /home/isabell/.ssh/authorized_keys file. Otherwise, you’ll lock yourself out from accessing your Uberspace via SSH. An alternative approach would be to add another SSH key in the Uberspace dashboard and use that for logging into Uberspace.

  • Be careful to keep the key number key-X, keep your key ssh-... and change the username isabell to yours.

  • Ensure that there is no second line propagating the same SSH key.

command="if [ -t 0 ]; then bash; elif [[ ${SSH_ORIGINAL_COMMAND} =~ ^(scp|rsync|mysqldump).* ]]; then eval ${SSH_ORIGINAL_COMMAND}; else /home/isabell/forgejo/forgejo --config=/home/isabell/forgejo/custom/conf/app.ini serv key-1; fi",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty,no-user-rc,restrict ssh-ed25519 AAAAC... youruser@yourhost

Note

You can still use the Uberspace dashboard to add other ssh keys for running default ssh sessions.

To interact with Forgejo at our local machine like git clone isabell@isabell.uber.space:forgejouser/somerepo.git we configure the /home/localuser/.ssh/config file for our local machine with the git ssh key.

Host isabell.uber.space
    User isabell
    IdentityFile ~/.ssh/id_your_git_key
    IdentitiesOnly yes

Backup

The Forgejo CLI (command line interface) has a build-in backup command to create a full backup in a zip file with the database, repos, config, log, data

Note

To restore a backup follow the backup and restore documentation

Execute the following command:

[isabell@stardust ~]$ ~/forgejo/forgejo dump
2024/05/31 15:39:13 ...s/setting/session.go:77:loadSessionFrom() [I] Session Service Enabled
[...]
2024/05/31 15:39:13 cmd/dump.go:265:runDump() [I] Dumping local repositories... /home/isabell/forgejo/data/forgejo-repositories
2024/05/31 15:39:13 cmd/dump.go:306:runDump() [I] Dumping database...
[...]
2024/05/31 15:39:13 cmd/dump.go:430:runDump() [I] Finish dumping in file forgejo-dump-1717162753.zip
[isabell@stardust ~]$

Updates

Note

Check the update feed or releases page regularly to stay informed about the newest version.

Manual updating

  • Do the Download and Set permissions steps from above.

  • Check if you have to modify the config file. (See documentation and the file sample.)

  • Do the database migration: ~/forgejo/forgejo migrate

  • Start the application supervisorctl restart forgejo

  • Check if the application is running supervisorctl status forgejo

Additional configuration

Forgejo using external renderer (optional)

Forgejo supports custom file renderings (i.e. Jupyter notebooks, asciidoc, etc.) through external binaries to provide a preview.
In this case we install an external rendering extension for AsciiDoc.
AsciiDoctors location will be here: ~/.gem/ruby/3.2.0/*/asciidoctor*
[isabell@stardust ~]$ gem install asciidoctor
Fetching asciidoctor-2.0.20.gem
WARNING:  You don't have /home/isabell/.gem/ruby/3.2.0/bin in your PATH,
        gem executables will not run.
Successfully installed asciidoctor-2.0.20
1 gem installed
[isabell@stardust ~]$

Note

Don’t be confused by the warning that the bin folder isn’t in the path. Uberspace is taking care of it. You can check with [isabell@stardust ~]$ which asciidoctor.

Now we have to append the config file ~/forgejo/custom/conf/app.ini with:

[markup.asciidoc]
ENABLED = true
FILE_EXTENSIONS = .adoc,.asciidoc
RENDER_COMMAND = "asciidoctor -e -a leveloffset=-1 --out-file=- -"
IS_INPUT_FILE = false

Tested with Forgejo 7.0.3, Uberspace 7.15.15

Written by: Marc Redwerkz <https://rdwz.one>