-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitlab-ci-credential
executable file
·56 lines (50 loc) · 1.74 KB
/
gitlab-ci-credential
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/bin/bash
# A custom git credential helper which works with GitLab CI. Can be installed by adding the
# following to a git configuration file:
#
# ```bash
# HOSTNAME=...
# git config --global credential.https://$HOSTNAME.helper "$HOME/gitlab-ci-credential"
# ```
#
# You can test the behaviour of this script by running the following and entering key/value
# pairs or just pressing enter twice:
#
# ```bash
# CI_PROJECT_URL=https://$HOSTNAME CI_JOB_TOKEN=passwd ./gitlab-ci-credential get
# ```
#
# If you which to use test the script with your credentials you can do do by providing your
# personal access token via an environmental variable:
#
# ````bash
# export CI_PROJECT_URL=https://$HOSTNAME CI_JOB_TOKEN=personal-access-token
# ```
# https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage#_a_custom_credential_cache
# Note: both CI_PROJECT_URL and CI_JOB_TOKEN are defined automatically when running inside
# of the GitLab CI (https://docs.gitlab.com/ee/ci/variables/#predefined-variables-environment-variables)
CI_HOST=$(echo "$CI_PROJECT_URL" | sed -e 's!.*://\([^/]*\).*!\1!')
CI_USER="gitlab-ci-token"
matched=1
IFS='='
while read -r line; do
[ "$line" == "" ] && break
if [ "$matched" -eq 1 ]; then
p=($line)
key=${p[0]}
val=${p[1]}
if [ "$key" == "protocol" -a "$val" != "https" ]; then
matched=0
elif [ "$key" == "host" -a "$val" != "$CI_HOST" ]; then
matched=0
elif [ "$key" == "username" -a "$val" != "$CI_USER" ]; then
matched=0
fi
fi
done < /dev/stdin
if [ "$1" == "get" -a ! -z "$CI_JOB_TOKEN" -a $matched -eq 1 ]; then
echo "protocol=https"
echo "host=${CI_HOST}"
echo "username=${CI_USER}"
echo "password=${CI_JOB_TOKEN}"
fi