From 6086b546c79ef17cd85c54e582a7f1e4a6fac762 Mon Sep 17 00:00:00 2001 From: koplo199 <85577251+koplo199@users.noreply.github.com> Date: Mon, 5 Jun 2023 12:46:42 +0200 Subject: [PATCH] CI Improvements --- .github/workflows/pull-components.yml | 55 +++++++++++++++++++++------ 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/.github/workflows/pull-components.yml b/.github/workflows/pull-components.yml index c9e24b7c..0e2b1dba 100644 --- a/.github/workflows/pull-components.yml +++ b/.github/workflows/pull-components.yml @@ -8,10 +8,11 @@ on: workflow_dispatch: env: - PR_BRANCH: ci/${{ github.ref_name }} + WORKING_BRANCH: ci/${{ github.ref_name }} + PR_BRANCH: pr/ci/${{ github.ref_name }} concurrency: - # Github Actions does not support ${{ env.PR_BRANCH }} as group name... + # Github Actions does not support ${{ env.WORKING_BRANCH }} as group name... group: ci/${{ github.ref_name }} jobs: @@ -22,18 +23,43 @@ jobs: with: path: "bottle-components-repository" ref: ${{ github.ref_name }} + fetch-depth: 0 - - name: Create or reset ${{ env.PR_BRANCH }} + - name: Create or reset ${{ env.WORKING_BRANCH }} working-directory: "bottle-components-repository" + shell: bash {0} run: | git config user.email "github-actions[bot]@users.noreply.github.com" git config user.name "github-actions[bot]" + pr_already_exists=$(gh pr list --app 'github-actions' --head ${{ env.PR_BRANCH }} --base ${{ github.ref_name }} --state 'open' --json number --jq '.[] | .[]') + if ! [ -z "$pr_already_exists" ]; then + declare -a BranchArray=( "${{ env.PR_BRANCH }}" "${{ env.WORKING_BRANCH }}" ) + status=0 + for BRANCH in ${BranchArray[@]}; do + git checkout $BRANCH + git rebase ${{ github.ref_name }} + status=$? + if [ $status -eq 0 ]; then + git push -f origin $BRANCH + else + break + fi + done + if [ $status -eq 0 ]; then + exit 0 + fi + fi + git reset --hard ${{ github.ref_name }} git checkout -B ${{ env.PR_BRANCH }} + git push -f --set-upstream origin ${{ env.PR_BRANCH }} + git checkout -B ${{ env.WORKING_BRANCH }} mkdir patch_dir touch patch_dir/.gitkeep git add patch_dir/.gitkeep git commit -m "Add .gitkeep file" - git push -f --set-upstream origin ${{ env.PR_BRANCH }} + git push -f --set-upstream origin ${{ env.WORKING_BRANCH }} + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} pull: needs: prepare @@ -272,8 +298,12 @@ jobs: git config user.name "github-actions[bot]" git add "${{ env.CATEGORY }}/${{ env.SUBCATEGORY }}/${{ env.NAME }}.yml" "input_files/${{ env.YAML_FILENAME }}" git commit -m "Add ${{ env.NAME }}" - git diff HEAD~1 HEAD > patch_dir/"${{ matrix.components.order }}-${{ env.NAME }}.patch" - git reset --hard HEAD~1 + file_name="${{ matrix.components.order }}-$(date -d "${{ env.CREATED_AT }}" +%s)-${{ env.NAME }}.patch" + git diff HEAD~1 HEAD > "$file_name" + git fetch origin ${{ env.WORKING_BRANCH }}:${{ env.WORKING_BRANCH }} + git switch ${{ env.WORKING_BRANCH }} + git branch --set-upstream-to=origin/${{ env.WORKING_BRANCH }} ${{ env.WORKING_BRANCH }} + mv "$file_name" patch_dir git add patch_dir/*.patch git pull git commit -m "Add ${{ env.NAME }}" @@ -293,22 +323,23 @@ jobs: - uses: actions/checkout@v3 with: path: "bottle-components-repository" - ref: ${{ env.PR_BRANCH }} + ref: ${{ env.WORKING_BRANCH }} fetch-depth: 0 - name: Generate index.yml working-directory: "bottle-components-repository" + shell: bash {0} run: | git checkout ${{ github.ref_name }} git config user.email "github-actions[bot]@users.noreply.github.com" git config user.name "github-actions[bot]" - git merge ${{ env.PR_BRANCH }} --no-commit --no-ff + git merge ${{ env.WORKING_BRANCH }} --no-commit --no-ff git reset readarray -d '' entries < <(printf '%s\0' patch_dir/*.patch | sort -zV) for entry in "${entries[@]}"; do patch -p1 --fuzz 5 < "$entry" git add -- . ':!patch_dir' ':!**.orig' - entry=${entry#patch_dir/*-} + entry=${entry#patch_dir/*-[0-9]*-} git commit -a -m "Add component ${entry%\.patch}" done ./generate_index.sh @@ -320,10 +351,10 @@ jobs: working-directory: "bottle-components-repository" shell: bash {0} run: | - git checkout -B pr/${{ env.PR_BRANCH }} - git push -f --set-upstream origin pr/${{ env.PR_BRANCH }} + git checkout -B ${{ env.PR_BRANCH }} + git push -f --set-upstream origin ${{ env.PR_BRANCH }} - 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 }}) + 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 }}) echo "PR_CREATED=$([ $? -eq 0 ] && echo true || echo false)" >> $GITHUB_OUTPUT pr_number=$($gh_output | tail -n1) echo "PR_NUMBER=${pr_number#https://github.com/${{ github.repository }}/pull/}" >> $GITHUB_OUTPUT