-
Notifications
You must be signed in to change notification settings - Fork 3
/
entrypoint.sh
158 lines (134 loc) · 4.5 KB
/
entrypoint.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/bin/bash
#
# Docker entrypoint.sh
# Author: ttionya <[email protected]>
. /app/actions_functions.sh
. /app/actions_push.sh
. /app/actions_delete.sh
TARGET_REPOSITORY="${INPUT_TARGET_REPOSITORY}"
#################### Function ####################
########################################
# Check target repository URL type.
# Arguments:
# None
########################################
function check_target_repository() {
local TARGET_REPOSITORY_HTTP_URL_MATCHED="$(echo "${TARGET_REPOSITORY}" | grep -E '^http(s?)://')"
if [[ -n "${TARGET_REPOSITORY_HTTP_URL_MATCHED}" ]]; then
# HTTP URL
color blue "use HTTP URL"
configure_token
else
# SSH URL
color blue "use SSH URL"
configure_ssh
fi
}
########################################
# Configure access token.
# Arguments:
# None
########################################
function configure_token() {
git config --global credential.helper store
local TARGET_REPOSITORY_PROTOCOL="$(echo "${TARGET_REPOSITORY}" | grep -oE '^http(s?)://')"
local TARGET_REPOSITORY_HOST="$(echo "${TARGET_REPOSITORY}" | sed -r "s|${TARGET_REPOSITORY_PROTOCOL}||" | awk -F'/' '{ print $1 }')"
color yellow "TARGET_REPOSITORY_PROTOCOL: ${TARGET_REPOSITORY_PROTOCOL}"
color yellow "TARGET_REPOSITORY_HOST: ${TARGET_REPOSITORY_HOST}"
echo "${TARGET_REPOSITORY_PROTOCOL}${INPUT_HTTP_ACCESS_NAME}:${INPUT_HTTP_ACCESS_TOKEN}@${TARGET_REPOSITORY_HOST}" > ~/.git-credentials
}
########################################
# Configure SSH.
# Arguments:
# None
# Outputs:
# SSH test message
########################################
function configure_ssh() {
# configure known_hosts
local TARGET_REPOSITORY_HOST="$(echo "${TARGET_REPOSITORY}" | sed -r 's|(.*@)?(.*):.*|\2|')"
color yellow "TARGET_REPOSITORY_HOST: ${TARGET_REPOSITORY_HOST}"
# ~ is /github/home/, not /root/
mkdir -p /root/.ssh
local KNOWN_HOSTS_FILE="/root/.ssh/known_hosts"
retry ssh-keyscan "${TARGET_REPOSITORY_HOST}" > "${KNOWN_HOSTS_FILE}"
chmod 644 "${KNOWN_HOSTS_FILE}"
if [[ ! -s "${KNOWN_HOSTS_FILE}" ]]; then
color red "ssh-keyscan failed"
exit 1
fi
# configure SSH private key
if [[ -n "${INPUT_SSH_PRIVATE_KEY}" ]]; then
color blue "find SSH private key"
eval $(ssh-agent -s)
echo "${INPUT_SSH_PRIVATE_KEY}" | tr -d '\r' | ssh-add - > /dev/null
fi
# test SSH connection
local SSH_TEST_URL="$(echo "${TARGET_REPOSITORY}" | awk -F':' '{ print $1 }')"
color yellow "SSH_TEST_URL: ${SSH_TEST_URL}"
# Do not judge the exit code here, known github.com will return an error even if the connection is successful.
ssh -T "${SSH_TEST_URL}"
}
########################################
# Configure Git remote information.
# Arguments:
# None
# Outputs:
# Git remote list
########################################
function configure_git_remote() {
git remote add target "${TARGET_REPOSITORY}"
git remote -v
}
########################################
# Configure Git safe directory.
# Arguments:
# None
# Outputs:
# None
########################################
function configure_git_safe_directory() {
git config --global --add safe.directory "${GITHUB_WORKSPACE}"
}
########################################
# Display some variables.
# Arguments:
# None
# Outputs:
# variables value
########################################
function display_variables() {
color yellow "========================================"
color yellow "GITHUB_WORKFLOW: ${GITHUB_WORKFLOW}"
color yellow "GITHUB_REPOSITORY: ${GITHUB_REPOSITORY}"
color yellow "GITHUB_EVENT_NAME: ${GITHUB_EVENT_NAME}"
color yellow "GITHUB_SHA: ${GITHUB_SHA}"
color yellow "GITHUB_REF: ${GITHUB_REF}"
color yellow "GITHUB_WORKSPACE: ${GITHUB_WORKSPACE}"
color yellow "TARGET_REPOSITORY: ${TARGET_REPOSITORY}"
color yellow "CURRENT_DIR: $(pwd)"
color yellow "WHOAMI: $(whoami)"
color yellow "========================================"
}
#################### Actions ####################
check_target_repository
display_variables
configure_git_safe_directory
configure_git_remote
case "${GITHUB_EVENT_NAME}" in
push)
color blue "=============== PUSH ==============="
push_current_ref
;;
delete)
color blue "=============== DELETE ==============="
delete_refs
;;
schedule)
color blue "=============== SCHEDULE ==============="
delete_refs
push_refs
;;
*)
;;
esac