diff --git a/docs/content/doc/advanced/environment-variables.en-us.md b/docs/content/doc/advanced/environment-variables.en-us.md index 288358f69dbe8..6f40cedaba85e 100644 --- a/docs/content/doc/advanced/environment-variables.en-us.md +++ b/docs/content/doc/advanced/environment-variables.en-us.md @@ -41,6 +41,7 @@ For documentation about each of the variables available, refer to the ## Gitea files - `GITEA_WORK_DIR`: Absolute path of working directory. +- `GITEA_APP_PATH`: Absolute path of gitea binary. (This is usually automatically detected and should therefore be only set in limited circumstances.) - `GITEA_CUSTOM`: Gitea uses `GITEA_WORK_DIR`/custom folder by default. Use this variable to change _custom_ directory. - `GOGS_WORK_DIR`: Deprecated, use `GITEA_WORK_DIR` @@ -51,6 +52,8 @@ For documentation about each of the variables available, refer to the - `USER`: System user that Gitea will run as. Used for some repository access strings. - `USERNAME`: if no `USER` found, Gitea will use `USERNAME` - `HOME`: User home directory path. The `USERPROFILE` environment variable is used in Windows. +- `SNAP`: Path to directory that snap uses to store Gitea's configuration. Snap defaults to using `$GITEA_APP_PATH`. +- `SNAP_REVISION`: If Gitea is installed using snap, revision number of snap in use. ### Only on Windows diff --git a/docs/content/doc/installation/from-source.en-us.md b/docs/content/doc/installation/from-source.en-us.md index 496111e956348..8730291790d8a 100644 --- a/docs/content/doc/installation/from-source.en-us.md +++ b/docs/content/doc/installation/from-source.en-us.md @@ -159,6 +159,7 @@ using the `LDFLAGS` environment variable for `make`. The appropriate settings ar - To set the `CustomPath` use `LDFLAGS="-X \"code.gitea.io/gitea/modules/setting.CustomPath=custom-path\""` - For `CustomConf` you should use `-X \"code.gitea.io/gitea/modules/setting.CustomConf=conf.ini\"` - For `AppWorkPath` you should use `-X \"code.gitea.io/gitea/modules/setting.AppWorkPath=working-path\"` +- For `AppPath` you should use `-X \"code.gitea.io/gitea/modules/setting.AppPath=/path/to/gitea/binary\"` - For `StaticRootPath` you should use `-X \"code.gitea.io/gitea/modules/setting.StaticRootPath=static-root-path\"` - To change the default PID file location use `-X \"code.gitea.io/gitea/modules/setting.PIDFile=/run/gitea.pid\"` diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 2133184cfc40d..c5a765ae67a0a 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -431,7 +431,14 @@ func IsProd() bool { } func getAppPath() (string, error) { - var appPath string + appPath := AppPath + if giteaAppPath, ok := os.LookupEnv("GITEA_APP_PATH"); ok { + appPath = giteaAppPath + } + if appPath != "" { + return appPath, nil + } + var err error if IsWindows && filepath.IsAbs(os.Args[0]) { appPath = filepath.Clean(os.Args[0]) @@ -446,9 +453,24 @@ func getAppPath() (string, error) { if err != nil { return "", err } + // Note: we don't use path.Dir here because it does not handle case // which path starts with two "/" in Windows: "//psf/Home/..." - return strings.ReplaceAll(appPath, "\\", "/"), err + appPath = strings.ReplaceAll(appPath, "\\", "/") + + // Fix issue 16209: When running in a snap and repositories are + // created, paths in the hooks include the snap revision. But as new + // revisions come out the older revisions are eventually deleted, and + // the hooks begin to fail. + // The code below replaces the snap revision with "current", which + // is a link to the current snap revision. + snapPath := os.Getenv("SNAP") + if snapPath != "" && strings.HasPrefix(appPath, snapPath) { + revision := os.Getenv("SNAP_REVISION") + appPath = strings.Replace(appPath, revision, "current", 1) + } + + return appPath, nil } func getWorkPath(appPath string) string { diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml index cd989dfbbf0a2..00bad182045e4 100644 --- a/snap/snapcraft.yaml +++ b/snap/snapcraft.yaml @@ -45,7 +45,7 @@ parts: source: . stage-packages: [ git, sqlite3, openssh-client ] build-packages: [ git, libpam0g-dev, libsqlite3-dev] - build-snaps: [ go, node/14/stable ] + build-snaps: [ go, node/16/stable ] build-environment: - LDFLAGS: "" override-pull: |