-
Notifications
You must be signed in to change notification settings - Fork 59
/
entrypoint.sh
executable file
·93 lines (78 loc) · 3.73 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
#!/bin/sh
set -u
##################################################################
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=10
POLL_TIMEOUT=${POLL_TIMEOUT:-$DEFAULT_POLL_TIMEOUT}
sh -c "git config --global --add safe.directory /github/workspace"
git checkout "$GITHUB_REF_NAME"
branch="$(git symbolic-ref --short HEAD)"
branch_uri="$(urlencode ${branch})"
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 -c "git push --force mirror $branch"
else
sh -c "git push mirror $branch"
fi
if [ "${FOLLOW_TAGS:-}" = "true" ]
then
sh -c "echo pushing with --tags"
sh -c "git push --tags mirror $branch"
fi
sleep $POLL_TIMEOUT
pipeline_id=$(curl --header "PRIVATE-TOKEN: $GITLAB_PASSWORD" --silent "https://${GITLAB_HOSTNAME}/api/v4/projects/${GITLAB_PROJECT_ID}/repository/commits/${branch_uri}" | jq '.last_pipeline.id')
echo "Triggered CI for branch ${branch}"
echo "Working with pipeline id #${pipeline_id}"
echo "Poll timeout set to ${POLL_TIMEOUT}"
ci_status="pending"
until [[ "$ci_status" != "pending" && "$ci_status" != "running" ]]
do
sleep $POLL_TIMEOUT
ci_output=$(curl --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 "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