Unattended Gitea installation from the CLI

Bonjour,

I need to install Gitea within a CI in a fully automated way, not human interaction required. Here is how I’m currently doing it:

  • cat docker-compose.yml
version: '2'
services:
  web:
    image: gitea/gitea:1.14.2
    volumes:
      - ./data/gitea:/data
    ports:
      - "8080:3000"
      - "2222:22"
    restart: always
  • docker-compose up -d
  • curl --verbose --data-binary 'db_type=SQLite3&db_host=localhost%3A3306&db_user=root&db_passwd=&db_name=gitea&ssl_mode=disable&db_schema=&charset=utf8&db_path=%2Fdata%2Fgitea%2Fgitea.db&app_name=Gitea%3A+Git+with+a+cup+of+tea&repo_root_path=%2Fdata%2Fgit%2Frepositories&lfs_root_path=%2Fdata%2Fgit%2Flfs&run_user=git&domain=localhost&ssh_port=22&http_port=3000&app_url=http%3A%2F%2Flocalhost%3A8080%2F&log_root_path=%2Fdata%2Fgitea%2Flog&smtp_host=&smtp_from=&smtp_user=&smtp_passwd=&enable_federated_avatar=on&no_reply_address=&password_algorithm=pbkdf2&admin_name=gitea_admin&admin_passwd=admin123&admin_confirm_passwd=admin123&admin_email=admin@example.com' http://localhost:8080/

I suppose there is a much easier way to do that but I could not find a gitea admin to do the same.

Cheers

Here is documentation on how to create the app.ini using env vars Installation with Docker - Docs and of course there is a CLI tool in place for creating an admin use.

That does not make it unattended though, the curl command above is required to complete the installation, even when all variables are set in the environment.

The CLI command doesn’t need to be run by a human, but could be inserted into the entrypoint of the docker container.

But, I’m guessing you are probably looking for something more like what the helm chart does: gitea.com/gitea/helm-chart/ in which case I recommend using the helm-chart.

I think there is a misunderstanding. Which CLI command are you referring to exactly? I know of user create and that works fine, once gitea is installed. But which CLI do you call to install gitea?

To avoid any further misunderstanding, here is a script and complete output that illustrates my question:

$ cat > docker-compose.yml <<EOF
version: '2'
services:
  web:
    image: gitea/gitea:1.14.2
    ports:
      - "8080:3000"
      - "2222:22"
    restart: always
EOF
$ docker-compose up -d
$ docker-compose exec web gitea admin user create --admin --username gitea_admin2 --password admin1234 --email gitea_admin2@example.com
2021/06/19 08:55:34 ...dules/setting/git.go:101:newGit() [I] Git Version: 2.30.2, Wire Protocol Version 2 Enabled
2021/06/19 08:55:34 main.go:117:main() [F] Failed to run app with [gitea admin user create --admin --username gitea_admin2 --password admin1234 --email gitea_admin2@example.com]: CreateUser: no such table: user

The error CreateUser: no such table: user is because gitea is not yet installed. If I then run:

$  curl --silent --verbose --data-binary 'db_type=SQLite3&db_host=localhost%3A3306&db_user=root&db_passwd=&db_name=gitea&ssl_mode=disable&db_schema=&charset=utf8&db_path=%2Fdata%2Fgitea%2Fgitea.db&app_name=Gitea%3A+Git+with+a+cup+of+tea&repo_root_path=%2Fdata%2Fgit%2Frepositories&lfs_root_path=%2Fdata%2Fgit%2Flfs&run_user=git&domain=localhost&ssh_port=22&http_port=3000&app_url=http%3A%2F%2Flocalhost%3A8080%2F&log_root_path=%2Fdata%2Fgitea%2Flog&smtp_host=&smtp_from=&smtp_user=&smtp_passwd=&enable_federated_avatar=on&no_reply_address=&password_algorithm=pbkdf2&admin_name=gitea_admin&admin_passwd=admin123&admin_confirm_passwd=admin123&admin_email=admin@example.com' http://localhost:8080/ > /dev/null
*   Trying 127.0.0.1:8080...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
> POST / HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.68.0
> Accept: */*
> Content-Length: 628
> Content-Type: application/x-www-form-urlencoded
> 
} [628 bytes data]
* upload completely sent off: 628 out of 628 bytes
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Refresh: 1; url=http://localhost:8080/user/login
< Set-Cookie: i_like_gitea=efd53fcdc4090dd1; Path=/; HttpOnly
< Set-Cookie: gitea_awesome=gitea_admin; Path=/; Max-Age=604800; HttpOnly
< Set-Cookie: gitea_incredible=c7e0a6a2b7b4e4866e361672379aa5bb6e6dc9249589c0efb9b453d27e940819d56f4e090a0c74; Path=/; Max-Age=604800; HttpOnly
< Date: Sat, 19 Jun 2021 09:10:05 GMT
< Transfer-Encoding: chunked
< 
{ [4293 bytes data]
* Connection #0 to host localhost left intact
$  docker-compose exec web gitea admin user create --admin --username gitea_admin2 --password admin1234 --email gitea_admin2@example.com
2021/06/19 09:11:26 ...s/setting/setting.go:910:NewContext() [F] Expect user 'git' but current user is: root

The gitea admin user create command succeeded because gitea is now properly installed, thanks to the call to curl above.

Is there a gitea CLI command (or any other CLI really) I could run to install gitea instead of the above curl command ?

You can ignore the installation page if you change INSTALL_LOCK = true on app.ini. That means installation page just help you filled something into app.ini.

1 Like

Could you try again to confirm it has been resolved?

For the record, here are the bare minimum commands to install gitea unattended (thanks @lunny and @misc for the INSTALL_LOCK=true hint!) and create an admin user to login (user: root, password admin1234):

$ docker run --name gitea -p 8080:3000 -d gitea/gitea:1.14.2
$ docker exec --user git gitea touch /data/gitea/gitea.db
$ docker exec --user git gitea sed -i -e "s/^INSTALL_LOCK.*/INSTALL_LOCK=true/" /data/gitea/conf/app.ini
$ docker restart gitea
$ docker exec gitea gitea admin user create --admin --username root --password admin1234 --email admin@example.com

Note that without the touch /data/gitea/gitea.db it silently fails because the gitea.db created belongs to root.

Sadly discourse flags based on rules that aren’t fairly obvious to me, and so I can’t say why you are being flagged(it says you have been flagged by the community, but I have yet to receive any notification about any of the flags).

There is a migrate command which will run the database migrations and ensure there are the appropriate data tables there.

There is an env var that’ll allow you to set the INSTALL_LOCK config option. (I believe it is GITEA__security__INSTALL_LOCK)

1 Like

I’ve also manually elevated your “trust level” as it’s clear that you aren’t a spammer, hopefully that prevents some flags.

1 Like

But of course! That will make the minimal unattended install even simpler.

For the record, here are the bare minimum commands to install gitea unattended and create an admin user to login (user: root, password admin1234):

$ docker run --name gitea -p 8080:3000 -e GITEA__security__INSTALL_LOCK=true -d gitea/gitea:1.14.2
$ docker exec gitea gitea admin user create --admin --username root --password admin1234 --email admin@example.com
2021/07/10 05:44:10 ...dules/setting/git.go:101:newGit() [I] Git Version: 2.30.2, Wire Protocol Version 2 Enabled
New user 'root' has been successfully created!
1 Like

Glad to hear that you have a solution :slight_smile:

1 Like