-
Notifications
You must be signed in to change notification settings - Fork 4
208 lines (176 loc) · 6.83 KB
/
Copy pathdistribute-reusable.yml
File metadata and controls
208 lines (176 loc) · 6.83 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
name: Distribute PushOk - Reusable
on:
workflow_call:
inputs:
branch:
required: true
type: string
app_ref:
required: false
type: string
default: ""
secrets:
GITLAB_TRIGGER_TOKEN:
required: true
permissions:
contents: read
jobs:
trigger:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
ref: ${{ inputs.branch }}
fetch-depth: 3
- name: Get last 3 commit messages
shell: bash
run: |
set -euo pipefail
commits="$(git log -3 --pretty=format:"%s")"
delim="EOF_$(openssl rand -hex 8)"
{
echo "commits<<$delim"
echo "$commits"
echo "$delim"
} >> "$GITHUB_ENV"
- name: Debug payload that will be sent to GitLab
shell: bash
env:
SOURCE_BRANCH: ${{ inputs.branch }}
APP_REF_OVERRIDE: ${{ inputs.app_ref }}
DEFAULT_APP_REF: develop
INPUT_COMMITS: ${{ env.commits }}
run: |
set -euo pipefail
APP_REF_OVERRIDE="$(printf '%s' "${APP_REF_OVERRIDE:-}" | xargs)"
echo "---- DEBUG (GitHub -> GitLab trigger payload) ----"
echo "SDK branch (INPUT_BRANCH): $SOURCE_BRANCH"
echo "Manual App ref override: ${APP_REF_OVERRIDE:-<empty>}"
echo "Default App ref: $DEFAULT_APP_REF"
echo ""
echo "RAW INPUT_COMMITS (cat -A):"
printf '%s' "${INPUT_COMMITS:-}" | cat -A
echo ""
echo "RAW INPUT_COMMITS (printf %q):"
printf '%q\n' "${INPUT_COMMITS:-}"
echo "--------------------------------------------------"
- name: Trigger build workflow in react-native-app repo (override strict; else same->develop)
shell: bash
env:
GITLAB_HOST: mindbox.gitlab.yandexcloud.net
APP_PROJECT_ID: "1512"
DEFAULT_APP_REF: develop
SOURCE_BRANCH: ${{ inputs.branch }}
APP_REF_OVERRIDE: ${{ inputs.app_ref }}
GITLAB_TRIGGER_TOKEN: ${{ secrets.GITLAB_TRIGGER_TOKEN }}
INPUT_COMMITS: ${{ env.commits }}
run: |
set -euo pipefail
# Trim override so " " becomes empty
APP_REF_OVERRIDE="$(printf '%s' "${APP_REF_OVERRIDE:-}" | xargs)"
# Normalize commits:
# - convert CRLF -> LF
# - if commits accidentally contain literal "\n", expand them to real newlines
normalize_commits() {
local raw="${1:-}"
raw="$(printf '%s' "$raw" | tr -d '\r')"
if [[ "$raw" == *"\\n"* ]]; then
raw="$(printf '%b' "$raw")"
fi
printf '%s' "$raw"
}
COMMITS_TO_SEND="$(normalize_commits "${INPUT_COMMITS:-}")"
echo "SDK branch (INPUT_BRANCH): $SOURCE_BRANCH"
echo "Manual App ref override: ${APP_REF_OVERRIDE:-<empty>}"
echo "Default App ref: $DEFAULT_APP_REF"
echo ""
echo "COMMITS_TO_SEND preview (cat -A):"
printf '%s' "$COMMITS_TO_SEND" | cat -A
echo ""
trigger_pipeline() {
local ref="$1"
local tmp_body
tmp_body="$(mktemp)"
local code
code="$(curl -sS -o "$tmp_body" -w '%{http_code}' --location \
--retry 3 --retry-all-errors --retry-delay 2 \
"https://${GITLAB_HOST}/api/v4/projects/${APP_PROJECT_ID}/trigger/pipeline" \
--form "token=${GITLAB_TRIGGER_TOKEN}" \
--form "ref=${ref}" \
--form "variables[INPUT_BRANCH]=${SOURCE_BRANCH}" \
--form "variables[INPUT_COMMITS]=${COMMITS_TO_SEND}")"
local body
body="$(cat "$tmp_body" 2>/dev/null || true)"
rm -f "$tmp_body"
echo "Trigger HTTP: $code (ref=$ref)"
echo "Response body:"
echo "$body"
if [[ "$code" == "200" || "$code" == "201" ]]; then
local web_url
web_url="$(
printf '%s\n' "$body" |
grep -o '"web_url":"[^"]*"' |
head -n 1 |
cut -d'"' -f4
)"
if [[ -n "${web_url:-}" ]]; then
echo "Pipeline URL: $web_url"
fi
return 0
fi
if [[ "$code" == "401" || "$code" == "403" ]]; then
echo "Auth error (HTTP $code). Check that GITLAB_TRIGGER_TOKEN is valid and has access to project ${APP_PROJECT_ID}."
return 1
fi
# Missing ref: GitLab returns 400 + "Reference not found"
if [[ "$code" == "400" || "$code" == "404" ]]; then
if [[ "$body" == *"Reference not found"* ]]; then
return 2
fi
echo "Got HTTP $code but it's NOT 'Reference not found'."
echo "This can happen if pipelines are blocked for triggers by workflow:rules or job rules when CI_PIPELINE_SOURCE == 'trigger'."
echo "Check the target repo .gitlab-ci.yml rules/workflow:rules."
return 1
fi
if [[ "$code" =~ ^5[0-9][0-9]$ ]]; then
echo "Server error (HTTP $code). GitLab/proxy might be temporarily unavailable."
return 1
fi
echo "Unexpected HTTP status: $code"
return 1
}
# If override is provided: try ONLY override; if missing ref -> fail
if [[ -n "$APP_REF_OVERRIDE" ]]; then
echo "Override provided -> trying ONLY App ref: $APP_REF_OVERRIDE"
trigger_pipeline "$APP_REF_OVERRIDE" || rc=$?
rc="${rc:-0}"
if [[ "$rc" == "0" ]]; then
echo "Triggered on override ref."
exit 0
fi
if [[ "$rc" == "2" ]]; then
echo "ERROR: App ref not found: $APP_REF_OVERRIDE (GitLab returned 'Reference not found')"
exit 1
fi
echo "Trigger failed for reasons other than missing ref."
exit 1
fi
# No override: same branch -> fallback develop
desired_ref="$SOURCE_BRANCH"
fallback_ref="$DEFAULT_APP_REF"
echo "No override -> trying App ref: $desired_ref"
trigger_pipeline "$desired_ref" || rc=$?
rc="${rc:-0}"
if [[ "$rc" == "0" ]]; then
echo "Triggered on same branch."
exit 0
fi
if [[ "$rc" == "2" ]]; then
echo "Same branch not found. Falling back to: $fallback_ref"
trigger_pipeline "$fallback_ref"
echo "Triggered on fallback ref."
exit 0
fi
echo "Trigger failed for reasons other than missing ref."
exit 1