Skip to content

Commit 3b4a09c

Browse files
committed
Added url parameter to submodule_credentials
Signed-off-by: Jean-Philippe Morin <animationjpm@gmail.com>
1 parent 73c01ed commit 3b4a09c

File tree

3 files changed

+60
-61
lines changed

3 files changed

+60
-61
lines changed

README.md

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,29 +65,39 @@ Tracks the commits in a [git](http://git-scm.com/) repository.
6565
all tags in the repository. If `false` no tags will be fetched.
6666

6767
* `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
68+
* http(s) credentials:
6969
* `host` : The host to connect too. Note that `host` is specified with no protocol extensions.
7070
* `username` : Username for HTTP(S) auth when pulling submodule.
7171
* `password` : Password for HTTP(S) auth when pulling submodule.
72-
* ssh credentials
72+
* ssh credentials:
73+
* `url` : Submodule url, as specified in the `.gitmodule` file. Support full or relative ssh url.
7374
* `private_key` : Private key for SSH auth when pulling submodule.
7475
* `private_key_passphrase` : *Optional.* To unlock `private_key` if it is protected by a passphrase.
75-
76+
* exemple:
7677
```yaml
7778
submodule_credentials:
78-
# http(s) credentials
79+
# http(s) credentials
7980
- host: github.com
8081
username: git-user
8182
password: git-password
82-
# ssh credentials
83-
- private_key: |
83+
# ssh credentials
84+
- url: git@github.com:org-name/repo-name.git
85+
private_key: |
86+
-----BEGIN RSA PRIVATE KEY-----
87+
MIIEowIBAAKCAQEAtCS10/f7W7lkQaSgD/mVeaSOvSF9ql4hf/zfMwfVGgHWjj+W
88+
<Lots more text>
89+
DWiJL+OFeg9kawcUL6hQ8JeXPhlImG6RTUffma9+iGQyyBMCGd1l
90+
-----END RSA PRIVATE KEY-----
91+
private_key_passphrase: ssh-passphrase # (optionnal)
92+
# ssh credentials with relative url
93+
- url: ../org-name/repo-name.git
94+
private_key: |
8495
-----BEGIN RSA PRIVATE KEY-----
8596
MIIEowIBAAKCAQEAtCS10/f7W7lkQaSgD/mVeaSOvSF9ql4hf/zfMwfVGgHWjj+W
8697
<Lots more text>
8798
DWiJL+OFeg9kawcUL6hQ8JeXPhlImG6RTUffma9+iGQyyBMCGd1l
8899
-----END RSA PRIVATE KEY-----
89100
private_key_passphrase: ssh-passphrase # (optionnal)
90-
- <another-configuration>
91101
```
92102

93103
* `git_config`: *Optional.* If specified as (list of pairs `name` and `value`)
@@ -303,7 +313,7 @@ the case.
303313

304314
* `.git/commit_message`: For publishing the Git commit message on successful builds.
305315

306-
* `.git/commit_timestamp`: For tagging builds with a timestamp.
316+
* `.git/commit_timestamp`: For tagging builds with a timestamp.
307317

308318
* `.git/describe_ref`: Version reference detected and checked out. Can be templated with `describe_ref_options` parameter.
309319
By default, it will contain the `<latest annoted git tag>-<the number of commit since the tag>-g<short_ref>` (eg. `v1.6.2-1-g13dfd7b`).

assets/common.sh

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ load_pubkey() {
1212
if [ -s $private_key_path ]; then
1313
chmod 0600 $private_key_path
1414

15-
eval $(ssh-agent) >/dev/null 2>&1
16-
trap "kill $SSH_AGENT_PID" EXIT
17-
SSH_ASKPASS_REQUIRE=force SSH_ASKPASS=$(dirname $0)/askpass.sh GIT_SSH_PRIVATE_KEY_PASS="$passphrase" DISPLAY= ssh-add $private_key_path >/dev/null
15+
# create or re-initialize ssh-agent
16+
init_ssh_agent
17+
18+
SSH_ASKPASS_REQUIRE=force SSH_ASKPASS=$(dirname $0)/askpass.sh GIT_SSH_PRIVATE_KEY_PASS="$passphrase" DISPLAY= ssh-add $private_key_path > /dev/null
1819

1920
mkdir -p ~/.ssh
2021
cat > ~/.ssh/config <<EOF
@@ -35,6 +36,25 @@ EOF
3536
fi
3637
}
3738

39+
init_ssh_agent() {
40+
41+
# validate if ssh-agent exist
42+
set +e
43+
ssh-add -l &> /dev/null
44+
exit_code=$?
45+
set -e
46+
47+
if [[ ${exit_code} -eq 2 ]]; then
48+
# ssh-agent does not exist, create ssh-agent
49+
eval $(ssh-agent) > /dev/null 2>&1
50+
trap "kill $SSH_AGENT_PID" EXIT
51+
else
52+
# ssh-agent exist, remove all identities
53+
ssh-add -D &> /dev/null
54+
fi
55+
56+
}
57+
3858
configure_https_tunnel() {
3959
tunnel=$(jq -r '.source.https_tunnel // empty' <<< "$1")
4060

assets/in

Lines changed: 19 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ if [ "$submodules" != "none" ]; then
166166
sed -e 's/^submodule\.\(.\+\)\.path$/\1/'
167167
} | while read submodule_name; do
168168
submodule_path="$(git config --file .gitmodules --get "submodule.${submodule_name}.path")"
169+
submodule_url="$(git config --file .gitmodules --get "submodule.${submodule_name}.url")"
169170

170171
if [ "$depth" -gt 0 ]; then
171172
git config "submodule.${submodule_name}.update" "!$bin_dir/deepen_shallow_clone_until_ref_is_found_then_check_out $depth"
@@ -176,66 +177,34 @@ if [ "$submodules" != "none" ]; then
176177
continue
177178
fi
178179

179-
set +e
180-
git submodule update --init --no-fetch $depthflag $submodule_parameters "$submodule_path"
181-
code=$?
182-
set -e
180+
# check for ssh submodule_credentials
181+
submodule_cred=$(jq --arg submodule_url "${submodule_url}" '.source.submodule_credentials // [] | [.[] | select(.url==$submodule_url)] | first // empty' <<< ${payload})
183182

184-
if [[ ${code} -eq 1 ]]; then
183+
if [[ -z ${submodule_cred} ]]; then
185184

186-
credentials=$(jq '.source.submodule_credentials // [] | [.[] | select(has("private_key"))]' <<< ${payload})
187-
credentials_length=$(jq 'length' <<< ${credentials})
185+
# update normally
186+
git submodule update --init --no-fetch $depthflag $submodule_parameters "$submodule_path"
188187

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
188+
else
216189

217-
# kill short-lived ssh-agent
218-
ssh-agent -k > /dev/null || true
219-
trap - EXIT
190+
# create or re-initialize ssh-agent
191+
init_ssh_agent
220192

221-
if [[ ${code} -eq 0 ]]; then
222-
break;
223-
fi
193+
private_key=$(jq -r '.private_key' <<< ${submodule_cred})
194+
passphrase=$(jq -r '.private_key_passphrase // empty' <<< ${submodule_cred})
224195

225-
done
196+
private_key_path=$(mktemp -t git-resource-submodule-private-key.XXXXXX)
197+
echo "${private_key}" > ${private_key_path}
198+
chmod 0600 ${private_key_path}
226199

227-
# restore main ssh-agent (if needed)
228-
load_pubkey "${git_config_payloadd}"
200+
# add submodule private_key identity
201+
SSH_ASKPASS_REQUIRE=force SSH_ASKPASS=$(dirname $0)/askpass.sh GIT_SSH_PRIVATE_KEY_PASS="$passphrase" DISPLAY= ssh-add $private_key_path > /dev/null
229202

230-
fi
203+
git submodule update --init --no-fetch $depthflag $submodule_parameters "$submodule_path"
231204

232-
if [[ ${code} -ne 0 ]]; then
233-
echo $'\e[31m'"warning: failed to clone submodule: $submodule_path"$'\e[0m'
234-
exit ${code}
235-
fi
205+
# restore main ssh-agent (if needed)
206+
load_pubkey "${payload}"
236207

237-
elif [[ ${code} -ne 0 ]]; then
238-
exit ${code}
239208
fi
240209

241210
if [ "$depth" -gt 0 ]; then

0 commit comments

Comments
 (0)