Skip to content

Commit 32ed656

Browse files
authored
WMSDK-608: Update distribution flow (#185)
Solution without GItLab API token With manual distribution, you can specify the branch name for the app. If there is no such branch, the workflow will fail and the logs will show an error stating that the branch could not be found. In all other cases, I always try to trigger a build on GitLab with the exact same branch as the one on which the distribution workflow runs. If there is no such branch, it triggers a build in GitLab on develop.
1 parent 1a7b0ad commit 32ed656

2 files changed

Lines changed: 177 additions & 10 deletions

File tree

.github/workflows/distribute-manual.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,16 @@ name: Distribute PushOk (manual)
22

33
on:
44
workflow_dispatch:
5+
inputs:
6+
app_ref:
7+
description: "GitLab App branch (optional)"
8+
required: false
9+
default: ""
510

611
jobs:
712
call-reusable:
813
uses: ./.github/workflows/distribute-reusable.yml
914
with:
10-
branch: ${{ github.ref_name }}
15+
branch: ${{ github.ref_name }} # SDK branch = current branch
16+
app_ref: ${{ inputs.app_ref }} # optional override for GitLab ref
1117
secrets: inherit

.github/workflows/distribute-reusable.yml

Lines changed: 170 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,186 @@ on:
66
branch:
77
required: true
88
type: string
9+
app_ref:
10+
required: false
11+
type: string
12+
default: ""
13+
secrets:
14+
GITLAB_TRIGGER_TOKEN:
15+
required: true
916

1017
jobs:
1118
trigger:
12-
runs-on: macos-latest
19+
runs-on: ubuntu-latest
1320
steps:
1421
- name: Checkout repository
1522
uses: actions/checkout@v4
1623
with:
1724
ref: ${{ inputs.branch }}
25+
fetch-depth: 3
1826

1927
- name: Get last 3 commit messages
28+
shell: bash
29+
run: |
30+
set -euo pipefail
31+
commits="$(git log -3 --pretty=format:"%s")"
32+
echo "commits<<EOF" >> "$GITHUB_ENV"
33+
echo "$commits" >> "$GITHUB_ENV"
34+
echo "EOF" >> "$GITHUB_ENV"
35+
36+
- name: Debug payload that will be sent to GitLab
37+
shell: bash
38+
env:
39+
SOURCE_BRANCH: ${{ inputs.branch }}
40+
APP_REF_OVERRIDE: ${{ inputs.app_ref }}
41+
DEFAULT_APP_REF: develop
42+
INPUT_COMMITS: ${{ env.commits }}
2043
run: |
21-
commits=$(git log -3 --pretty=format:"%s")
22-
echo "commits=$commits" >> $GITHUB_ENV
44+
set -euo pipefail
45+
46+
APP_REF_OVERRIDE="$(printf '%s' "${APP_REF_OVERRIDE:-}" | xargs)"
47+
48+
echo "---- DEBUG (GitHub -> GitLab trigger payload) ----"
49+
echo "SDK branch (INPUT_BRANCH): $SOURCE_BRANCH"
50+
echo "Manual App ref override: ${APP_REF_OVERRIDE:-<empty>}"
51+
echo "Default App ref: $DEFAULT_APP_REF"
52+
echo ""
53+
echo "RAW INPUT_COMMITS (cat -A):"
54+
printf '%s' "${INPUT_COMMITS:-}" | cat -A
55+
echo ""
56+
echo "RAW INPUT_COMMITS (printf %q):"
57+
printf '%q\n' "${INPUT_COMMITS:-}"
58+
echo "--------------------------------------------------"
59+
60+
- name: Trigger build workflow in flutter-app repo (override strict; else same->develop)
61+
shell: bash
62+
env:
63+
GITLAB_HOST: mindbox.gitlab.yandexcloud.net
64+
APP_PROJECT_ID: "1089"
65+
DEFAULT_APP_REF: develop
66+
67+
SOURCE_BRANCH: ${{ inputs.branch }}
68+
APP_REF_OVERRIDE: ${{ inputs.app_ref }}
2369

24-
- name: Trigger build workflow in flutter-app repo
70+
GITLAB_TRIGGER_TOKEN: ${{ secrets.GITLAB_TRIGGER_TOKEN }}
71+
INPUT_COMMITS: ${{ env.commits }}
2572
run: |
26-
curl --location 'https://mindbox.gitlab.yandexcloud.net/api/v4/projects/1089/trigger/pipeline' \
27-
--form "token=${{ secrets.GITLAB_TRIGGER_TOKEN }}" \
28-
--form "ref=develop" \
29-
--form "variables[INPUT_BRANCH]=${{ inputs.branch }}" \
30-
--form "variables[INPUT_COMMITS]=${{ env.commits }}"
73+
set -euo pipefail
74+
75+
APP_REF_OVERRIDE="$(printf '%s' "${APP_REF_OVERRIDE:-}" | xargs)"
76+
77+
normalize_commits() {
78+
local raw="${1:-}"
79+
raw="$(printf '%s' "$raw" | tr -d '\r')"
80+
if [[ "$raw" == *"\\n"* ]]; then
81+
raw="$(printf '%b' "$raw")"
82+
fi
83+
printf '%s' "$raw"
84+
}
85+
86+
COMMITS_TO_SEND="$(normalize_commits "${INPUT_COMMITS:-}")"
87+
88+
trigger_pipeline() {
89+
local ref="$1"
90+
local tmp_body
91+
tmp_body="$(mktemp)"
92+
93+
local code
94+
code="$(curl -sS -o "$tmp_body" -w '%{http_code}' --location \
95+
--retry 3 --retry-all-errors --retry-delay 2 \
96+
"https://${GITLAB_HOST}/api/v4/projects/${APP_PROJECT_ID}/trigger/pipeline" \
97+
--form "token=${GITLAB_TRIGGER_TOKEN}" \
98+
--form "ref=${ref}" \
99+
--form "variables[INPUT_BRANCH]=${SOURCE_BRANCH}" \
100+
--form "variables[INPUT_COMMITS]=${COMMITS_TO_SEND}")"
101+
102+
local body
103+
body="$(cat "$tmp_body" 2>/dev/null || true)"
104+
rm -f "$tmp_body"
105+
106+
echo "Trigger HTTP: $code (ref=$ref)"
107+
echo "Response body:"
108+
echo "$body"
109+
110+
if [[ "$code" == "200" || "$code" == "201" ]]; then
111+
local web_url
112+
web_url="$(
113+
printf '%s\n' "$body" |
114+
grep -o '"web_url":"[^"]*"' |
115+
head -n 1 |
116+
cut -d'"' -f4
117+
)"
118+
if [[ -n "${web_url:-}" ]]; then
119+
echo "Pipeline URL: $web_url"
120+
fi
121+
return 0
122+
fi
123+
124+
if [[ "$code" == "401" || "$code" == "403" ]]; then
125+
echo "Auth error (HTTP $code). Check that GITLAB_TRIGGER_TOKEN is valid and has access to project ${APP_PROJECT_ID}."
126+
return 1
127+
fi
128+
129+
if [[ "$code" == "400" || "$code" == "404" ]]; then
130+
if [[ "$body" == *"Reference not found"* ]]; then
131+
return 2
132+
fi
133+
echo "Got HTTP $code but it's NOT 'Reference not found'."
134+
echo "This can happen if pipelines are blocked for triggers by workflow:rules or job rules when CI_PIPELINE_SOURCE == 'trigger'."
135+
echo "Check the target repo .gitlab-ci.yml rules/workflow:rules."
136+
return 1
137+
fi
138+
139+
if [[ "$code" =~ ^5[0-9][0-9]$ ]]; then
140+
echo "Server error (HTTP $code). GitLab/proxy might be temporarily unavailable."
141+
return 1
142+
fi
143+
144+
echo "Unexpected HTTP status: $code"
145+
return 1
146+
}
147+
148+
echo "SDK branch (INPUT_BRANCH): $SOURCE_BRANCH"
149+
echo "Manual App ref override: ${APP_REF_OVERRIDE:-<empty>}"
150+
echo "Default App ref: $DEFAULT_APP_REF"
151+
152+
if [[ -n "$APP_REF_OVERRIDE" ]]; then
153+
echo "Override provided -> trying ONLY App ref: $APP_REF_OVERRIDE"
154+
trigger_pipeline "$APP_REF_OVERRIDE" || rc=$?
155+
rc="${rc:-0}"
156+
157+
if [[ "$rc" == "0" ]]; then
158+
echo "Triggered on override ref."
159+
exit 0
160+
fi
161+
162+
if [[ "$rc" == "2" ]]; then
163+
echo "ERROR: App ref not found: $APP_REF_OVERRIDE (GitLab returned 'Reference not found')"
164+
exit 1
165+
fi
166+
167+
echo "ERROR: Trigger failed for reasons other than missing ref."
168+
exit 1
169+
fi
170+
171+
desired_ref="$SOURCE_BRANCH"
172+
fallback_ref="$DEFAULT_APP_REF"
173+
174+
echo "No override -> trying App ref: $desired_ref"
175+
trigger_pipeline "$desired_ref" || rc=$?
176+
rc="${rc:-0}"
177+
178+
if [[ "$rc" == "0" ]]; then
179+
echo "Triggered on same branch."
180+
exit 0
181+
fi
182+
183+
if [[ "$rc" == "2" ]]; then
184+
echo "Same branch not found. Falling back to: $fallback_ref"
185+
trigger_pipeline "$fallback_ref"
186+
echo "Triggered on fallback ref."
187+
exit 0
188+
fi
189+
190+
echo "ERROR: Trigger failed for reasons other than missing ref."
191+
exit 1

0 commit comments

Comments
 (0)