From ac00b156bfcb8404d502efa876005789ce23615f Mon Sep 17 00:00:00 2001 From: Fabrizio Antonangeli Date: Tue, 30 Apr 2024 14:22:33 +0200 Subject: [PATCH] SRVLOGIC-282: Move main-sync CI to kie-ci repository (#1552) --- .ci/actions/main-sync/action.yml | 115 +++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 .ci/actions/main-sync/action.yml diff --git a/.ci/actions/main-sync/action.yml b/.ci/actions/main-sync/action.yml new file mode 100644 index 00000000..a050c7f3 --- /dev/null +++ b/.ci/actions/main-sync/action.yml @@ -0,0 +1,115 @@ +name: 'Sync main branch' +description: 'Create a PR to sync main branch with main-apache excluding ' + +inputs: + username: + description: 'Username for git' + required: false + default: kie-ci + useremail: + description: 'User email for git' + required: false + default: kie-ci0@redhat.com + main_sync_workflow_exclude_paths: + description: 'Paths to be excluded during merge' + required: false + default: .ci .github .asf.yaml + main_sync_workflow_pr_reviewers: + description: 'Reviewers for the PR' + required: false + default: '' + dry_run: + description: 'True to perform a dry run (dry run git push and skip create PR step)' + required: false + default: false + +runs: + if: github.repository_owner == 'kiegroup' + using: 'composite' + steps: + - name: Generate PR ID + id: generate_pr_id + shell: bash + run: echo "pr_id=$(date +'%Y-%m-%d')" >> $GITHUB_OUTPUT + + - name: Checkout repository + uses: actions/checkout@v4 + with: + ref: main + # By default, checkout@v4 fetches only a single commit unless you specify "fetch-depth: 0". Without this "git merge" throwns an "unrelated histories". + fetch-depth: '0' + + - name: Setup git environment + shell: bash + run: | + git config --global user.name "${{ inputs.username }}" + git config --global user.email "${{ inputs.useremail }}" + + - name: Fetch all + shell: bash + run: git fetch --all + + - name: Checkout main branch + shell: bash + run: git checkout main + + - name: Create the PR branch + shell: bash + run: git checkout -b sync-main-pr-${{ steps.generate_pr_id.outputs.pr_id }} + + - name: Merge main-apache branch excluding white-listed paths + id: merge + shell: bash + run: | + set -x + git merge --no-commit origin/main-apache || true + if [ -n "${{ inputs.main_sync_workflow_exclude_paths }}" ]; then + git reset origin/main ${{ inputs.main_sync_workflow_exclude_paths }} + fi + if git diff --cached --quiet; then + echo "No changes staged for commit." + else + git commit -m "Merge main-apache and exclude white-listed changes from the merge" + fi + echo "excluded_files=$(git ls-files -mo | sed -z 's/\n/
/g')" >> $GITHUB_OUTPUT + + - name: Check for changes + id: check_changes + shell: bash + run: | + set -x + if git diff --quiet origin/main; then + echo "No changes detected." + echo "is_changed=false" >> $GITHUB_OUTPUT + else + echo "Changes detected." + echo "is_changed=true" >> $GITHUB_OUTPUT + fi + + - name: Push changes + if: steps.check_changes.outputs.is_changed == 'true' + shell: bash + run: | + set -x + git push origin sync-main-pr-${{ steps.generate_pr_id.outputs.pr_id }}${{ inputs.dry_run == 'true' && ' --dry-run' || '' }} + + - name: Create the PR + if: steps.check_changes.outputs.is_changed == 'true' && inputs.dry_run == 'false' + shell: bash + run: | + set -x + runUrl="${{github.server_url}}/${{ github.repository }}/actions/runs/${{ github.run_id }}" + if [[ -n "${{ steps.merge.outputs.excluded_files }}" ]]; then + excludedFiles="${{ steps.merge.outputs.excluded_files }}" + else + excludedFiles="No files have been excluded
" + fi + prTitle="Automatic PR: Sync main with main-apache (${{steps.generate_pr_id.outputs.pr_id}})" + prBody="This pull request has been created by a GitHub workflow to synchronize the main branch with main-apache branch.

\ + :warning:Important:warning:
Please don't merge using squash, not to lose the git history.

\ + Excluded files:
${excludedFiles}
\ + [View Action](${runUrl})" + if [[ -n "${{ inputs.main_sync_workflow_pr_reviewers }}" ]]; then + reviewersOption="--reviewer ${{inputs.main_sync_workflow_pr_reviewers}}" + fi + gh pr create --title "${prTitle}" --body "${prBody}" --base main $reviewersOption