forked from SvanBoxel/gitlab-mirror-and-ci-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd.sh
executable file
·117 lines (99 loc) · 4.61 KB
/
cmd.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
#!/bin/bash
##################################################################
urlencode() (
i=1
max_i=${#1}
while test $i -le $max_i; do
c="$(expr substr $1 $i 1)"
case $c in
[a-zA-Z0-9.~_-])
printf "$c" ;;
*)
printf '%%%02X' "'$c" ;;
esac
i=$(( i + 1 ))
done
)
##################################################################
DEFAULT_POLL_TIMEOUT=5
POLL_TIMEOUT=${POLL_TIMEOUT:-$DEFAULT_POLL_TIMEOUT}
if [[ ${CI_DEBUG:-''} == "true" ]]; then
set -x
fi
git checkout "${GITHUB_REF:11}"
branch="$(git symbolic-ref --short HEAD)"
sh -c "git config --global --add safe.directory '*'"
sh -c "git config --global credential.username ${GITLAB_USERNAME}"
sh -c "git config --global core.askPass /cred-helper.sh"
sh -c "git config --global credential.helper cache"
sh -c "git remote add mirror $*"
sh -c "echo pushing to $branch branch at $(git remote get-url --push mirror)"
if [ "${FORCE_PUSH:-}" = "true" ]
then
sh -v -e -c "git push --force mirror $branch"
else
sh -v -e -c "git push mirror $branch"
fi
if [ "${FOLLOW_TAGS:-}" = "true" ]
then
sh -c "echo pushing with --tags"
sh -v -e -c "git push --tags mirror $branch"
fi
set -eu
GET_PIPELINE_ID_RETRIES=1
GET_PIPELINE_ID_RETRIES_MAX=60
pipeline_id=null
echo Get pipelineId from commit ${GITHUB_SHA}
until [[ ${pipeline_id} =~ [0-9] ]] || (( GET_PIPELINE_ID_RETRIES == GET_PIPELINE_ID_RETRIES_MAX )); do
echo -n "."
sleep 1
(( GET_PIPELINE_ID_RETRIES++ ))
pipeline_id=$(curl --fail --header "PRIVATE-TOKEN: ${GITLAB_PASSWORD}" --silent "https://${GITLAB_HOSTNAME}/api/v4/projects/${GITLAB_PROJECT_ID}/repository/commits/${GITHUB_SHA}" | jq '.last_pipeline.id')
done
echo
if [ ${GET_PIPELINE_ID_RETRIES} -eq ${GET_PIPELINE_ID_RETRIES_MAX} ]; then
echo "ERROR: Can not get pipelineId from api"
exit 1
fi
echo "INFO: Working with pipelineId #${pipeline_id}"
ci_status="pending"
GET_JOB_STATUS_RETRIES=1
GET_JOB_STATUS_RETRIES_MAX=600
until [[ "$ci_status" != "pending" && "$ci_status" != "running" ]] || (( GET_JOB_STATUS_RETRIES == GET_JOB_STATUS_RETRIES_MAX ))
do
sleep $POLL_TIMEOUT
ci_output=$(curl --fail --header "PRIVATE-TOKEN: $GITLAB_PASSWORD" --silent "https://${GITLAB_HOSTNAME}/api/v4/projects/${GITLAB_PROJECT_ID}/pipelines/${pipeline_id}")
ci_status=$(jq -n "$ci_output" | jq -r .status)
ci_web_url=$(jq -n "$ci_output" | jq -r .web_url)
echo "Current pipeline status: ${ci_status}"
if [ "$ci_status" = "running" ]
then
echo "Checking pipeline status..."
curl -d '{"state":"pending", "target_url": "'${ci_web_url}'", "context": "gitlab-ci"}' -H "Authorization: token ${GITHUB_TOKEN}" -H "Accept: application/vnd.github.antiope-preview+json" -X POST --silent "https://api.github.com/repos/${GITHUB_REPOSITORY}/statuses/${GITHUB_SHA}" > /dev/null
fi
done
echo
if [ ${GET_JOB_STATUS_RETRIES} -eq ${GET_JOB_STATUS_RETRIES_MAX} ]; then
echo "ERROR: Wait finish pipeline timeout"
curl -d '{"state":"failure", "target_url": "'${ci_web_url}'", "context": "gitlab-ci"}' -H "Authorization: token ${GITHUB_TOKEN}" -H "Accept: application/vnd.github.antiope-preview+json" -X POST --silent "https://api.github.com/repos/${GITHUB_REPOSITORY}/statuses/${GITHUB_SHA}"
exit 1
fi
echo "Pipeline finished with status ${ci_status}"
echo "Fetching all GitLab pipeline jobs involved"
ci_jobs=$(curl --header "PRIVATE-TOKEN: $GITLAB_PASSWORD" --silent "https://${GITLAB_HOSTNAME}/api/v4/projects/${GITLAB_PROJECT_ID}/pipelines/${pipeline_id}/jobs" | jq -r '.[] | { id, name, stage }')
echo "Posting output from all GitLab pipeline jobs"
for JOB_ID in $(echo $ci_jobs | jq -r .id); do
echo "##[group] Stage $( echo $ci_jobs | jq -r "select(.id=="${JOB_ID}") | .stage" ) / Job $( echo $ci_jobs | jq -r "select(.id=="${JOB_ID}") | .name" )"
curl --header "PRIVATE-TOKEN: $GITLAB_PASSWORD" --silent "https://${GITLAB_HOSTNAME}/api/v4/projects/${GITLAB_PROJECT_ID}/jobs/${JOB_ID}/trace"
echo "##[endgroup]"
done
echo "Debug problems by unfolding stages/jobs above"
if [ "$ci_status" = "success" ]
then
curl -d '{"state":"success", "target_url": "'${ci_web_url}'", "context": "gitlab-ci"}' -H "Authorization: token ${GITHUB_TOKEN}" -H "Accept: application/vnd.github.antiope-preview+json" -X POST --silent "https://api.github.com/repos/${GITHUB_REPOSITORY}/statuses/${GITHUB_SHA}"
exit 0
elif [ "$ci_status" = "failed" ]
then
curl -d '{"state":"failure", "target_url": "'${ci_web_url}'", "context": "gitlab-ci"}' -H "Authorization: token ${GITHUB_TOKEN}" -H "Accept: application/vnd.github.antiope-preview+json" -X POST --silent "https://api.github.com/repos/${GITHUB_REPOSITORY}/statuses/${GITHUB_SHA}"
exit 1
fi