#!/bin/bash INSTALL_ENV="/data/gitea-init/install.env" trap 'echo -e "\033[31mgitea-admin.sh: Something went wrong\033[0m"; exit 1' ERR set -e source $INSTALL_ENV echo " Create admin password..." GITEA_ADMIN_PASSWORD=$(pwgen -s 20 1); echo "Save GITEA_ADMIN_PASSWORD token to $INSTALL_ENV..." cd /data/gitea-init bash env-gen-install.sh GITEA_ADMIN_PASSWORD $GITEA_ADMIN_PASSWORD echo " Create admin user..." docker exec -it --user git gitea gitea admin user create --username $GIT_USER --password $GITEA_ADMIN_PASSWORD --email $ADMIN_EMAIL --admin docker exec -it --user git gitea gitea admin user list echo " Create admin access token..." TOKEN=$(curl -s -X POST "https://$GITEA_API_HOST/api/v1/users/$GITEA_USER_NAME/tokens" \ -H "Content-Type: application/json" \ -d '{"name": "WRITE_USER_REPO", "scopes": ["write:repository", "write:user"]}' \ -u "$GITEA_USER_NAME:$GITEA_ADMIN_PASSWORD" | jq -r .sha1) if [[ -z "$TOKEN" || "$TOKEN" == "null" ]]; then echo "Failed to obtain admin token" exit 1 fi echo "Save GITEA_WRITE_USER_REPO token to $INSTALL_ENV..." cd /data/gitea-init bash env-gen-install.sh GITEA_WRITE_USER_REPO $TOKEN echo "Add public SSH key to gitea app..." SSH_PUBLIC_KEY=$(cat ~/.ssh/id_ed25519.pub) echo "Get SSH keys list from gitea API..." keys=$(curl -X GET -H "Authorization: token $TOKEN" "https://$GITEA_API_HOST/api/v1/user/keys") echo "Search SSH key in gitea..." existing_key_id=$(echo $keys | jq -r ".[] | select(.title == \"$SERVER_NAME\") | .id") echo "If already exist SSH key in gitea, remove it..." if [ -n "$existing_key_id" ]; then curl -X DELETE -H "Authorization: token $TOKEN" "https://$GITEA_API_HOST/api/v1/user/keys/$existing_key_id" echo "SSH key removed" fi echo "Add new SSH public key in gitea..." curl -X POST \ "https://$GITEA_API_HOST/api/v1/user/keys" \ -H "Authorization: token $TOKEN" \ -H "Content-Type: application/json" \ -d "{ \"title\": \"$SERVER_NAME\", \"key\": \"$SSH_PUBLIC_KEY\" }" trap - ERR echo "New Gitea user and api token created"