forked from bytebase/login-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.sh
executable file
·62 lines (56 loc) · 1.59 KB
/
main.sh
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
57
58
59
60
61
62
#!/bin/bash
# ===========================================================================
# File: main.sh
# Description: usage: ./main.sh --url=[url] --version=[version] --service-key=[service key] --service-secret=[service secret]
# ===========================================================================
# Get parameters
for i in "$@"
do
case $i in
--url=*)
URL="${i#*=}"
shift
;;
--version=*)
VERSION="${i#*=}"
shift
;;
--service-key=*)
SERVICE_KEY="${i#*=}"
shift
;;
--service-secret=*)
SERVICE_SECRET="${i#*=}"
shift
;;
*) # unknown option
;;
esac
done
API_URL="$URL/$VERSION"
LOGIN_URL="$API_URL/auth/login"
ACTION_VERSION=`cat $GITHUB_ACTION_PATH/VERSION`
REPOSITORY=`echo $GITHUB_REPOSITORY | tr '[:upper:]' '[:lower:]'`
ACTOR=`echo $GITHUB_ACTOR | tr '[:upper:]' '[:lower:]'`
request_body=$(jq -n \
--arg email "$SERVICE_KEY" \
--arg password "$SERVICE_SECRET" \
'$ARGS.named')
response=$(curl -s -w "\n%{http_code}" -X POST $LOGIN_URL \
-H "X-Platform: GitHub" \
-H "X-Repository: $REPOSITORY" \
-H "X-Actor: $ACTOR" \
-H "X-Version: $ACTION_VERSION" \
-H "X-Source: action" \
-H "Content-Type: application/json" \
-d "$request_body")
http_code=$(tail -n1 <<< "$response")
body=$(sed '$ d' <<< "$response")
echo "::debug::response code: $http_code, response body: $body"
if [ $http_code != 200 ]; then
echo ":error::Failed to login with response code $http_code and body $body"
exit 1
fi
token=`echo $body | jq -r ".token"`
echo "token=$token" >> $GITHUB_OUTPUT
echo "api_url=$API_URL" >> $GITHUB_OUTPUT