Skip to content

Commit 75837b8

Browse files
authored
CI Improvements (#262)
1 parent 1814b36 commit 75837b8

File tree

1 file changed

+43
-12
lines changed

1 file changed

+43
-12
lines changed

.github/workflows/pull-components.yml

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ on:
88
workflow_dispatch:
99

1010
env:
11-
PR_BRANCH: ci/${{ github.ref_name }}
11+
WORKING_BRANCH: ci/${{ github.ref_name }}
12+
PR_BRANCH: pr/ci/${{ github.ref_name }}
1213

1314
concurrency:
14-
# Github Actions does not support ${{ env.PR_BRANCH }} as group name...
15+
# Github Actions does not support ${{ env.WORKING_BRANCH }} as group name...
1516
group: ci/${{ github.ref_name }}
1617

1718
jobs:
@@ -22,18 +23,43 @@ jobs:
2223
with:
2324
path: "bottle-components-repository"
2425
ref: ${{ github.ref_name }}
26+
fetch-depth: 0
2527

26-
- name: Create or reset ${{ env.PR_BRANCH }}
28+
- name: Create or reset ${{ env.WORKING_BRANCH }}
2729
working-directory: "bottle-components-repository"
30+
shell: bash {0}
2831
run: |
2932
git config user.email "github-actions[bot]@users.noreply.github.com"
3033
git config user.name "github-actions[bot]"
34+
pr_already_exists=$(gh pr list --app 'github-actions' --head ${{ env.PR_BRANCH }} --base ${{ github.ref_name }} --state 'open' --json number --jq '.[] | .[]')
35+
if ! [ -z "$pr_already_exists" ]; then
36+
declare -a BranchArray=( "${{ env.PR_BRANCH }}" "${{ env.WORKING_BRANCH }}" )
37+
status=0
38+
for BRANCH in ${BranchArray[@]}; do
39+
git checkout $BRANCH
40+
git rebase ${{ github.ref_name }}
41+
status=$?
42+
if [ $status -eq 0 ]; then
43+
git push -f origin $BRANCH
44+
else
45+
break
46+
fi
47+
done
48+
if [ $status -eq 0 ]; then
49+
exit 0
50+
fi
51+
fi
52+
git reset --hard ${{ github.ref_name }}
3153
git checkout -B ${{ env.PR_BRANCH }}
54+
git push -f --set-upstream origin ${{ env.PR_BRANCH }}
55+
git checkout -B ${{ env.WORKING_BRANCH }}
3256
mkdir patch_dir
3357
touch patch_dir/.gitkeep
3458
git add patch_dir/.gitkeep
3559
git commit -m "Add .gitkeep file"
36-
git push -f --set-upstream origin ${{ env.PR_BRANCH }}
60+
git push -f --set-upstream origin ${{ env.WORKING_BRANCH }}
61+
env:
62+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3763

3864
pull:
3965
needs: prepare
@@ -272,8 +298,12 @@ jobs:
272298
git config user.name "github-actions[bot]"
273299
git add "${{ env.CATEGORY }}/${{ env.SUBCATEGORY }}/${{ env.NAME }}.yml" "input_files/${{ env.YAML_FILENAME }}"
274300
git commit -m "Add ${{ env.NAME }}"
275-
git diff HEAD~1 HEAD > patch_dir/"${{ matrix.components.order }}-${{ env.NAME }}.patch"
276-
git reset --hard HEAD~1
301+
file_name="${{ matrix.components.order }}-$(date -d "${{ env.CREATED_AT }}" +%s)-${{ env.NAME }}.patch"
302+
git diff HEAD~1 HEAD > "$file_name"
303+
git fetch origin ${{ env.WORKING_BRANCH }}:${{ env.WORKING_BRANCH }}
304+
git switch ${{ env.WORKING_BRANCH }}
305+
git branch --set-upstream-to=origin/${{ env.WORKING_BRANCH }} ${{ env.WORKING_BRANCH }}
306+
mv "$file_name" patch_dir
277307
git add patch_dir/*.patch
278308
git pull
279309
git commit -m "Add ${{ env.NAME }}"
@@ -293,22 +323,23 @@ jobs:
293323
- uses: actions/checkout@v3
294324
with:
295325
path: "bottle-components-repository"
296-
ref: ${{ env.PR_BRANCH }}
326+
ref: ${{ env.WORKING_BRANCH }}
297327
fetch-depth: 0
298328

299329
- name: Generate index.yml
300330
working-directory: "bottle-components-repository"
331+
shell: bash {0}
301332
run: |
302333
git checkout ${{ github.ref_name }}
303334
git config user.email "github-actions[bot]@users.noreply.github.com"
304335
git config user.name "github-actions[bot]"
305-
git merge ${{ env.PR_BRANCH }} --no-commit --no-ff
336+
git merge ${{ env.WORKING_BRANCH }} --no-commit --no-ff
306337
git reset
307338
readarray -d '' entries < <(printf '%s\0' patch_dir/*.patch | sort -zV)
308339
for entry in "${entries[@]}"; do
309340
patch -p1 --fuzz 5 < "$entry"
310341
git add -- . ':!patch_dir' ':!**.orig'
311-
entry=${entry#patch_dir/*-}
342+
entry=${entry#patch_dir/*-[0-9]*-}
312343
git commit -a -m "Add component ${entry%\.patch}"
313344
done
314345
./generate_index.sh
@@ -320,10 +351,10 @@ jobs:
320351
working-directory: "bottle-components-repository"
321352
shell: bash {0}
322353
run: |
323-
git checkout -B pr/${{ env.PR_BRANCH }}
324-
git push -f --set-upstream origin pr/${{ env.PR_BRANCH }}
354+
git checkout -B ${{ env.PR_BRANCH }}
355+
git push -f --set-upstream origin ${{ env.PR_BRANCH }}
325356
326-
gh_output=$(gh pr create --title "Update available :tada:" --body "One or more components have been updated!" --head pr/${{ env.PR_BRANCH }} --base ${{ github.ref_name }})
357+
gh_output=$(gh pr create --title "Update available :tada:" --body "One or more components have been updated!" --head ${{ env.PR_BRANCH }} --base ${{ github.ref_name }})
327358
echo "PR_CREATED=$([ $? -eq 0 ] && echo true || echo false)" >> $GITHUB_OUTPUT
328359
pr_number=$($gh_output | tail -n1)
329360
echo "PR_NUMBER=${pr_number#https://github.com/${{ github.repository }}/pull/}" >> $GITHUB_OUTPUT

0 commit comments

Comments
 (0)