@@ -179,6 +179,76 @@ func rewriteGitConfigCredentialHelper(configContent, credentialsPath string) str
179179 return strings .Join (lines , "\n " )
180180}
181181
182+ // setupSSH sets up SSH keys from an ssh-directory workspace.
183+ // SSH files are copied to c.internalDir/.ssh/ and GIT_SSH_COMMAND is set
184+ // with explicit flags so that git uses the custom SSH config without modifying $HOME.
185+ func (c * GitClone ) setupSSH () error {
186+ if c .Params .SSHDirectory == "" {
187+ return nil
188+ }
189+
190+ sshDir := c .Params .SSHDirectory
191+
192+ if _ , err := os .Stat (sshDir ); os .IsNotExist (err ) {
193+ l .Logger .Infof ("SSH directory not found: %s" , sshDir )
194+ return nil
195+ }
196+
197+ l .Logger .Infof ("Setting up SSH keys from %s" , sshDir )
198+
199+ destSSHDir := filepath .Join (c .internalDir , ".ssh" )
200+
201+ if err := os .MkdirAll (destSSHDir , 0700 ); err != nil {
202+ return fmt .Errorf ("failed to create .ssh directory: %w" , err )
203+ }
204+
205+ entries , err := os .ReadDir (sshDir )
206+ if err != nil {
207+ return fmt .Errorf ("failed to read SSH directory: %w" , err )
208+ }
209+
210+ for _ , entry := range entries {
211+ if entry .IsDir () {
212+ continue // Skip subdirectories
213+ }
214+
215+ srcPath := filepath .Join (sshDir , entry .Name ())
216+ destPath := filepath .Join (destSSHDir , entry .Name ())
217+
218+ if err := copyFile (srcPath , destPath , 0400 ); err != nil {
219+ return fmt .Errorf ("failed to copy SSH file %s: %w" , entry .Name (), err )
220+ }
221+ }
222+
223+ sshCmd := "ssh"
224+
225+ configPath := filepath .Join (destSSHDir , "config" )
226+ if fileExists (configPath ) {
227+ sshCmd += fmt .Sprintf (` -F "%s"` , configPath )
228+ } else {
229+ sshCmd += " -F /dev/null"
230+ }
231+
232+ for _ , entry := range entries {
233+ name := entry .Name ()
234+ if strings .HasPrefix (name , "id_" ) && ! strings .HasSuffix (name , ".pub" ) && ! entry .IsDir () {
235+ sshCmd += fmt .Sprintf (` -i "%s"` , filepath .Join (destSSHDir , name ))
236+ }
237+ }
238+
239+ knownHostsPath := filepath .Join (destSSHDir , "known_hosts" )
240+ if fileExists (knownHostsPath ) {
241+ sshCmd += fmt .Sprintf (` -o UserKnownHostsFile="%s"` , knownHostsPath )
242+ }
243+
244+ if err := os .Setenv ("GIT_SSH_COMMAND" , sshCmd ); err != nil {
245+ return err
246+ }
247+
248+ l .Logger .Infof ("SSH keys configured (GIT_SSH_COMMAND=%s)" , sshCmd )
249+ return nil
250+ }
251+
182252// fileExists checks if a file exists and is not a directory.
183253func fileExists (path string ) bool {
184254 info , err := os .Stat (path )
0 commit comments