@@ -87,21 +87,47 @@ if [ "$USE_SSH" = "true" ]; then
87
87
88
88
[ -z " $PRIVATE_KEY " ] && (echo " missing PRIVATE_KEY var" | tee /dev/stderr) && exit 1
89
89
90
- echo " $PRIVATE_KEY " > /root/.ssh/codefresh
90
+ # it does not exist by default
91
+ mkdir -p ~ /.ssh
92
+ # copy private key to a file
93
+ echo " $PRIVATE_KEY " > ~ /.ssh/codefresh
94
+ # use this private key when using git with ssh
95
+ echo " IdentityFile ~/.ssh/codefresh" > ~ /.ssh/config
96
+
97
+ # set correct permissions for ssh agent
91
98
chmod 700 ~ /.ssh/
92
99
chmod 600 ~ /.ssh/*
93
100
94
101
# ssh://git@github.com:username/repo.git
95
102
# match "github.com" from ssh uri
96
- REPO=${REPO# " ssh://" }
97
- SSH_HOST=$( echo " $REPO " | cut -d " :" -f 1 | cut -d " @" -f 2)
103
+ SSH_REPO=${REPO# " ssh://" }
104
+
105
+
106
+ # was: git@host:1234:username/repo.git
107
+ # or: git@host:1234/repo.git
108
+ # or: git@host:username/repo.git
109
+ # became: `1234` (will be accepted by check)
110
+ # or: `username` (will be skipped by check)
111
+ SSH_PORT=$( echo " $SSH_REPO " | cut -d " :" -f 2 | cut -d " /" -f 1)
112
+
113
+ # we need to add port to ssh host in the known_hosts file
114
+ # otherwise it will ask to add host to known_hosts
115
+ # during git clone
116
+ SSH_PORT_PARAM=
117
+ SSH_PORT_LOG=' '
118
+ if [[ " $SSH_PORT " =~ ^[0-9]{1,5}$ ]]; then
119
+ SSH_PORT_PARAM=" -p $SSH_PORT "
120
+ SSH_PORT_LOG=" :$SSH_PORT "
121
+ fi
122
+
123
+ # was: git@github.com:username/repo.git
124
+ # became: github.com
125
+ SSH_HOST=$( echo " $SSH_REPO " | cut -d " :" -f 1 | cut -d " @" -f 2)
98
126
99
- echo " Adding " $SSH_HOST " to known_hosts"
127
+ echo " Adding " $SSH_HOST$SSH_PORT_LOG " to known_hosts"
100
128
101
- # removes all keys belonging to hostname from a known_hosts file
102
- ssh-keygen -R $SSH_HOST 2> /dev/null
103
129
# skip stderr logs that start with '#'
104
- ssh-keyscan -H $SSH_HOST > ~ /.ssh/known_hosts 2> >( grep -v ' ^#' >&2 )
130
+ ssh-keyscan $SSH_PORT_PARAM -H $SSH_HOST > ~ /.ssh/known_hosts 2> >( grep -v ' ^#' >&2 )
105
131
fi
106
132
107
133
mkdir -p " $WORKING_DIRECTORY "
0 commit comments