forked from SvanBoxel/gitlab-mirror-and-ci-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrypoint.sh
executable file
·145 lines (121 loc) · 6.04 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
#!/bin/sh
# Error handling
set -eo pipefail
# set -e : Instructs bash to immediately exit if any command has a non-zero exit status.
# (not active) set -u : Reference to any variable not previously defined - with the exceptions of $* and $@ - is an error
# set -o pipefail : Prevents errors in a pipeline from being masked.
##################################################################
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
)
##################################################################
# Time interval (secs) with which gutlab asked if it has finished CI yet.
DEFAULT_POLL_TIMEOUT=60
POLL_TIMEOUT=${POLL_TIMEOUT:-$DEFAULT_POLL_TIMEOUT}
DEFAULT_GITHUB_REF=${GITHUB_REF:11}
mirror_repo="$MIRROR_REPO"
sh -c "git config --global --add safe.directory '*'"
sh -c "git config --global user.name $GITLAB_USERNAME"
sh -c "git config --global user.email ${GITLAB_USERNAME}@${GITLAB_HOSTNAME}"
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"
if [[ ${IS_CMSSW:-false} == "true" ]]; then
# Checkout .gitlab-ci.yml
sh -c "git clone -o mirror -b masterCI $mirror_repo ."
branch="${CHECKOUT_BRANCH:-$DEFAULT_GITHUB_REF}"
else
git checkout "${CHECKOUT_BRANCH:-$DEFAULT_GITHUB_REF}"
sh -c "git remote add mirror $mirror_repo"
branch="$(git symbolic-ref --short HEAD)"
fi
echo "mirror repo = $mirror_repo and branch = $branch"
# Function urlencode sometimes crashes, so bypass it.
#branch_uri="$(urlencode ${branch})"
branch_uri=$branch
git branch -v
ls -la
if [[ ${REBASE_MASTER:-"false"} == "true" ]]; then
if [[ ${IS_CMSSW:-false} == "false" ]]; then
git rebase origin/master
fi
fi
# Removing and readding branch on mirror triggers CI there.
if [[ ${REMOVE_BRANCH:-"false"} == "true" ]]; then
# If branch exists
branchExists=$(git ls-remote $(git remote get-url --push mirror) ${CHECKOUT_BRANCH:-$DEFAULT_GITHUB_REF} | wc -l)
if [[ "${branchExists}" == "1" ]]; then
echo "removing the ${branch} branch at $(git remote get-url --push mirror)"
sh -c "git push mirror --delete ${branch}"
fi
fi
sh -c "echo pushing to $branch branch at $(git remote get-url --push mirror)"
if [[ ${IS_CMSSW:-false} == "true" ]]; then
# Copy .gitlab-ci.yml from branch masterCI to new branch triggers gitlab CI in the latte branch.
# The -o option here passes the owner-name of the repo where the new branch is located
# and the L1 track algorithm to be run to gitlab CI.
# https://docs.gitlab.com/ee/user/project/push_options.html#push-options-for-gitlab-cicd
sh -c "git push -o ci.variable="GITHUB_USER_UNDER_TEST=$GITHUB_REPOSITORY_OWNER" mirror masterCI:$branch"
# IDEA FOR FUTURE IMPROVEMENT
# Instead of triggering gitlab CI by pushing to branch,
# add a web hook https://gitlab.cern.ch/cms-l1tk/cmssw_CI/-/hooks
# and trigger it with "curl" (which can also pass variables)
# https://gitlab.cern.ch/cms-l1tk/cmssw_CI/-/settings/ci_cd#js-pipeline-triggers
else # not CMSSW
sh -c "git push 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')
pipeline_url="$mirror_repo/-/pipelines/${pipeline_id}"
echo "Poll timeout set to ${POLL_TIMEOUT}"
echo "Triggered CI for branch ${branch}"
echo "Working with pipeline id #${pipeline_id}"
echo ""
echo "--- GITLAB PIPELINE URL: $pipeline_url ---"
echo ""
# Is this the only way to return the pipeline URL to the calling .yml job?
echo "$pipeline_url" > $RETURN_FILE
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