Skip to content

Commit 35532b0

Browse files
committed
fix(actions): use SSH_DOMAIN for server URL when different from main domain
When SSH_DOMAIN is configured to use a different hostname than the main AppURL domain, Gitea Actions should provide the SSH domain in the server_url context to ensure checkout actions connect to the correct SSH hostname. This fixes an issue where workflows would fail with 'Host key verification failed' because checkout actions were trying to connect to the wrong SSH hostname when SSH_DOMAIN was configured differently from the main domain. Fixes: Actions workflows failing when SSH_DOMAIN != main domain Before: server_url always used AppURL domain (e.g., git.example.com) After: server_url uses SSH_DOMAIN when different (e.g., git-ssh.example.com)
1 parent 51ff787 commit 35532b0

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

services/actions/context.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ package actions
66
import (
77
"context"
88
"fmt"
9+
"net/url"
910
"strconv"
11+
"strings"
1012

1113
actions_model "code.gitea.io/gitea/models/actions"
1214
"code.gitea.io/gitea/models/db"
@@ -20,6 +22,38 @@ import (
2022
"github.com/nektos/act/pkg/model"
2123
)
2224

25+
// getServerURLForActions returns the server URL to use in Actions context.
26+
// If SSH_DOMAIN is configured differently from the main domain, this returns
27+
// a URL with the SSH domain to ensure checkout actions use the correct SSH hostname.
28+
func getServerURLForActions() string {
29+
// Parse the main AppURL to get the scheme and port
30+
appURL, err := url.Parse(setting.AppURL)
31+
if err != nil {
32+
return setting.AppURL // fallback to original if parsing fails
33+
}
34+
35+
// Get the main domain from AppURL
36+
mainDomain := appURL.Hostname()
37+
38+
// If SSH_DOMAIN is different from the main domain, use SSH_DOMAIN for the server URL
39+
// This ensures that checkout actions will use the correct SSH hostname
40+
if len(setting.SSH.Domain) > 0 && setting.SSH.Domain != mainDomain {
41+
// Create a new URL with the SSH domain but keep the same scheme and port as AppURL
42+
sshURL := &url.URL{
43+
Scheme: appURL.Scheme,
44+
Host: setting.SSH.Domain,
45+
}
46+
if appURL.Port() != "" {
47+
sshURL.Host = setting.SSH.Domain + ":" + appURL.Port()
48+
}
49+
sshURL.Path = strings.TrimSuffix(appURL.Path, "/") + "/"
50+
return sshURL.String()
51+
}
52+
53+
// If SSH_DOMAIN is not configured or is the same as main domain, use AppURL
54+
return setting.AppURL
55+
}
56+
2357
type GiteaContext map[string]any
2458

2559
// GenerateGiteaContext generate the gitea context without token and gitea_runtime_token
@@ -77,7 +111,7 @@ func GenerateGiteaContext(run *actions_model.ActionRun, job *actions_model.Actio
77111
"run_number": strconv.FormatInt(run.Index, 10), // string, A unique number for each run of a particular workflow in a repository. This number begins at 1 for the workflow's first run, and increments with each new run. This number does not change if you re-run the workflow run.
78112
"run_attempt": "", // string, A unique number for each attempt of a particular workflow run in a repository. This number begins at 1 for the workflow run's first attempt, and increments with each re-run.
79113
"secret_source": "Actions", // string, The source of a secret used in a workflow. Possible values are None, Actions, Dependabot, or Codespaces.
80-
"server_url": setting.AppURL, // string, The URL of the GitHub server. For example: https://github.com.
114+
"server_url": getServerURLForActions(), // string, The URL of the GitHub server. For SSH operations, this uses SSH_DOMAIN if configured differently from the main domain.
81115
"sha": sha, // string, The commit SHA that triggered the workflow. The value of this commit SHA depends on the event that triggered the workflow. For more information, see "Events that trigger workflows." For example, ffac537e6cbbf934b08745a378932722df287a53.
82116
"triggering_actor": "", // string, The username of the user that initiated the workflow run. If the workflow run is a re-run, this value may differ from github.actor. Any workflow re-runs will use the privileges of github.actor, even if the actor initiating the re-run (github.triggering_actor) has different privileges.
83117
"workflow": run.WorkflowID, // string, The name of the workflow. If the workflow file doesn't specify a name, the value of this property is the full path of the workflow file in the repository.

0 commit comments

Comments
 (0)