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.

1 Like

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.

Glad to hear that you have a solution :slight_smile:

1 Like

Hello, I am trying to automate gitea installation, i am using gitea on kubernetes.

I have deployed Gitea yamls on kubernetes and succeeded. I am running Gitea pod, service and an ingress, able to hit the Gitea ELB. The page which am seeing is where i still need to manually click on Install Button which i dont want because i need to integrate this with Jenkins.

I tried the steps given above as @lunny said i updated INSTALL_LOCK=true but now i need to create the user and password. I am passing the following command but getting an error

kubectl ex/app/gitea/gitea admin user create --username gitadmin --password gitadmin --email git@git.com -c /data/gitea/conf/app.ini …s/setting/setting.go:960:loadFromConf() [F] Gitea is not supposed to be run as root. Sorry. If you need to use privileged TCP ports please instead use setcap and the cap_net_bind_service permission

May i know how to fix it?

For the record, here are the bare minimum commands to install Gitea 1.16.7 unattended with docker 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.16.7
7b644d61d3dcdf2bada52071f8648a7c2423297188408efe9812a27ebe63b904
$ docker exec --user 1000 gitea gitea admin user create --admin --username root --password admin1234 --email root@example.com
New user 'root' has been successfully created!

1 Like

Hello dachary,

Thanks for the reply. As am running the command inside the pod, the given command doesnt work.

docker exec --user 1000 gitea gitea admin user create --admin --username root --password admin1234 --email root@example.com

docker command doesnt work inside the pod where the is no docker installed. do i need to tweak the command which suits pod env? am not getting the exact command, i just tried running the below one
–user 1000 gitea gitea admin user create --admin --username root --password admin1234 --email root@example.com, it did not work, got an error saying command not found.

am trying to run this command manually from the pod, if this works, i can automate it.

I know nothing about k8s unfortunately. If I was in your position I would try to find the equivalent of –user 1000 for k8s.

Hello dachary,

I got the solution. I just had to switch the user thats all i had to do.

I did su - git and ran the below command
/app/gitea/gitea admin user create --username gitadmin --password gitadmin --email git@git.com -c /data/gitea/conf/app.ini


Thank you very much !!

1 Like

@dachary: Wouldn’t it be possible to add the command execution to the Docker image, perhaps triggered, and controlled by environment variables, like GITEA_INITIAL_ADMIN_USERNAME, and GITEA_INITIAL_ADMIN_PASSWORD? Would avoid a lot of user questions.

MySQL has a similar thing with MYSQL_ROOT_PASSWORD, or MYSQL_RANDOM_ROOT_PASSWORD, see Docker Hub.

1 Like

Or maybe just as an addition to the documentation, if that’s a FAQ?

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

$ docker run --name gitea -p 3000:3000 -e GITEA__security__INSTALL_LOCK=true -d gitea/gitea:1.16.8
7b644d61d3dcdf2bada52071f8648a7c2423297188408efe9812a27ebe63b904
$ docker exec --user 1000 gitea gitea admin user create --admin --username root --password admin1234 --email root@example.com
New user 'root' has been successfully created!