@@ -740,39 +740,9 @@ func publishToSSH(cfg *SSHPublishConfig, artifactsDir string, tmplData map[strin
740740 remoteDir := dirBuffer .String ()
741741
742742 // Check if known_hosts file exists and create it if it doesn't
743- knownHostsPath , err := helpers .ExpandPath ("~/.ssh/known_hosts" )
744- if err != nil {
745- return fmt .Errorf ("failed to expand known hosts path: %w" , err )
746- }
747-
748- if _ , err := os .Stat (knownHostsPath ); os .IsNotExist (err ) {
749- // Create ~/.ssh directory if it doesn't exist
750- sshDir := filepath .Dir (knownHostsPath )
751- if err := os .MkdirAll (sshDir , 0o700 ); err != nil {
752- return fmt .Errorf ("failed to create .ssh directory: %w" , err )
753- }
754-
755- // Create empty known_hosts file
756- if err := os .WriteFile (knownHostsPath , []byte {}, 0o600 ); err != nil {
757- return fmt .Errorf ("failed to create known_hosts file: %w" , err )
758- }
759-
760- // Run ssh-keyscan to add the server to known_hosts
761- cmd := exec .Command ("ssh-keyscan" , "-H" , cfg .Server )
762- output , err := cmd .Output ()
763- if err != nil {
764- return fmt .Errorf ("ssh-keyscan failed: %w" , err )
765- }
766-
767- // Append the output to the known_hosts file
768- f , err := os .OpenFile (knownHostsPath , os .O_APPEND | os .O_WRONLY , 0o600 )
769- if err != nil {
770- return fmt .Errorf ("failed to open known_hosts file: %w" , err )
771- }
772- defer f .Close ()
773-
774- if _ , err := f .Write (output ); err != nil {
775- return fmt .Errorf ("failed to write to known_hosts file: %w" , err )
743+ if ! cfg .InsecureIgnoreHostKey {
744+ if err := checkKnonwnHost (cfg .Server ); err != nil {
745+ return fmt .Errorf ("failed to check known_hosts file: %w" , err )
776746 }
777747 }
778748
@@ -1101,6 +1071,13 @@ func executeSSHDeploy(cfg *SSHDeployConfig) error {
11011071 return fmt .Errorf ("invalid SSH configuration: %w" , err )
11021072 }
11031073
1074+ // Check if known_hosts file exists and create it if it doesn't
1075+ if ! cfg .InsecureIgnoreHostKey {
1076+ if err := checkKnonwnHost (cfg .Server ); err != nil {
1077+ return fmt .Errorf ("failed to check known_hosts file: %w" , err )
1078+ }
1079+ }
1080+
11041081 // Create SSH client
11051082 var auth goph.Auth
11061083 var err error
@@ -1148,6 +1125,47 @@ func executeSSHDeploy(cfg *SSHDeployConfig) error {
11481125 return nil
11491126}
11501127
1128+ func checkKnonwnHost (server string ) error {
1129+ // Check if known_hosts file exists and create it if it doesn't
1130+ knownHostsPath , err := helpers .ExpandPath ("~/.ssh/known_hosts" )
1131+ if err != nil {
1132+ return fmt .Errorf ("failed to expand known hosts path: %w" , err )
1133+ }
1134+
1135+ if _ , err := os .Stat (knownHostsPath ); os .IsNotExist (err ) {
1136+ // Create ~/.ssh directory if it doesn't exist
1137+ sshDir := filepath .Dir (knownHostsPath )
1138+ if err := os .MkdirAll (sshDir , 0o700 ); err != nil {
1139+ return fmt .Errorf ("failed to create .ssh directory: %w" , err )
1140+ }
1141+
1142+ // Create empty known_hosts file
1143+ if err := os .WriteFile (knownHostsPath , []byte {}, 0o600 ); err != nil {
1144+ return fmt .Errorf ("failed to create known_hosts file: %w" , err )
1145+ }
1146+
1147+ // Run ssh-keyscan to add the server to known_hosts
1148+ cmd := exec .Command ("ssh-keyscan" , "-H" , server )
1149+ output , err := cmd .Output ()
1150+ if err != nil {
1151+ return fmt .Errorf ("ssh-keyscan failed: %w" , err )
1152+ }
1153+
1154+ // Append the output to the known_hosts file
1155+ f , err := os .OpenFile (knownHostsPath , os .O_APPEND | os .O_WRONLY , 0o600 )
1156+ if err != nil {
1157+ return fmt .Errorf ("failed to open known_hosts file: %w" , err )
1158+ }
1159+ defer f .Close ()
1160+
1161+ if _ , err := f .Write (output ); err != nil {
1162+ return fmt .Errorf ("failed to write to known_hosts file: %w" , err )
1163+ }
1164+ }
1165+
1166+ return nil
1167+ }
1168+
11511169func main () {
11521170 // Load environment variables from .env file, if it exists.
11531171 godotenv .Load ()
0 commit comments