Skip to content

Commit 73c01ed

Browse files
committed
ssh support for submodule
Signed-off-by: Jean-Philippe Morin <animationjpm@gmail.com>
1 parent 6b4aba7 commit 73c01ed

File tree

2 files changed

+78
-5
lines changed

2 files changed

+78
-5
lines changed

README.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,32 @@ Tracks the commits in a [git](http://git-scm.com/) repository.
6464
* `fetch_tags`: *Optional.* If `true` the flag `--tags` will be used to fetch
6565
all tags in the repository. If `false` no tags will be fetched.
6666

67-
* `submodule_credentials`: *Optional.* List of credentials for HTTP(s) auth when pulling/pushing private git submodules which are not stored in the same git server as the container repository.
68-
Example:
67+
* `submodule_credentials`: *Optional.* List of credentials for HTTP(s) or SSH auth when pulling git submodules which are not stored in the same git server as the container repository or are protected by a different private key.
68+
* http(s) credentials
69+
* `host` : The host to connect too. Note that `host` is specified with no protocol extensions.
70+
* `username` : Username for HTTP(S) auth when pulling submodule.
71+
* `password` : Password for HTTP(S) auth when pulling submodule.
72+
* ssh credentials
73+
* `private_key` : Private key for SSH auth when pulling submodule.
74+
* `private_key_passphrase` : *Optional.* To unlock `private_key` if it is protected by a passphrase.
6975

70-
```
76+
```yaml
7177
submodule_credentials:
78+
# http(s) credentials
7279
- host: github.com
7380
username: git-user
7481
password: git-password
82+
# ssh credentials
83+
- private_key: |
84+
-----BEGIN RSA PRIVATE KEY-----
85+
MIIEowIBAAKCAQEAtCS10/f7W7lkQaSgD/mVeaSOvSF9ql4hf/zfMwfVGgHWjj+W
86+
<Lots more text>
87+
DWiJL+OFeg9kawcUL6hQ8JeXPhlImG6RTUffma9+iGQyyBMCGd1l
88+
-----END RSA PRIVATE KEY-----
89+
private_key_passphrase: ssh-passphrase # (optionnal)
7590
- <another-configuration>
7691
```
7792

78-
Note that `host` is specified with no protocol extensions.
79-
8093
* `git_config`: *Optional.* If specified as (list of pairs `name` and `value`)
8194
it will configure git global options, setting each name with each value.
8295

assets/in

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,67 @@ if [ "$submodules" != "none" ]; then
176176
continue
177177
fi
178178

179+
set +e
179180
git submodule update --init --no-fetch $depthflag $submodule_parameters "$submodule_path"
181+
code=$?
182+
set -e
183+
184+
if [[ ${code} -eq 1 ]]; then
185+
186+
credentials=$(jq '.source.submodule_credentials // [] | [.[] | select(has("private_key"))]' <<< ${payload})
187+
credentials_length=$(jq 'length' <<< ${credentials})
188+
189+
if [[ ${credentials_length} -gt 0 ]]; then
190+
191+
echo "Could not read from remote submodule repository with current credentials. Retry with submodule ssh credentials."
192+
193+
# kill main ssh-agent (if exist)
194+
kill $SSH_AGENT_PID > /dev/null || true
195+
trap - EXIT
196+
197+
for ((i = 0 ; i < ${credentials_length} ; i++)); do
198+
199+
creds=$(jq --argjson i $i '.[$i]' <<< ${credentials})
200+
private_key=$(jq -r '.private_key' <<< ${creds})
201+
passphrase=$(jq -r '.private_key_passphrase // empty' <<< ${creds})
202+
203+
private_key_path="${TMPDIR}/git-resource-submodule-private-key-$i"
204+
echo "${private_key}" > ${private_key_path}
205+
chmod 0600 ${private_key_path}
206+
207+
# short-lived ssh-agent
208+
eval $(ssh-agent) >/dev/null 2>&1
209+
trap "kill $SSH_AGENT_PID" EXIT
210+
SSH_ASKPASS_REQUIRE=force SSH_ASKPASS=$(dirname $0)/askpass.sh GIT_SSH_PRIVATE_KEY_PASS="$passphrase" DISPLAY= ssh-add $private_key_path > /dev/null
211+
212+
set +e
213+
git submodule update --init --no-fetch $depthflag $submodule_parameters "$submodule_path" 2> /dev/null
214+
code=$?
215+
set -e
216+
217+
# kill short-lived ssh-agent
218+
ssh-agent -k > /dev/null || true
219+
trap - EXIT
220+
221+
if [[ ${code} -eq 0 ]]; then
222+
break;
223+
fi
224+
225+
done
226+
227+
# restore main ssh-agent (if needed)
228+
load_pubkey "${git_config_payloadd}"
229+
230+
fi
231+
232+
if [[ ${code} -ne 0 ]]; then
233+
echo $'\e[31m'"warning: failed to clone submodule: $submodule_path"$'\e[0m'
234+
exit ${code}
235+
fi
236+
237+
elif [[ ${code} -ne 0 ]]; then
238+
exit ${code}
239+
fi
180240

181241
if [ "$depth" -gt 0 ]; then
182242
git config --unset "submodule.${submodule_name}.update"

0 commit comments

Comments
 (0)