Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SRVLOGIC-282: Move main-sync CI to kie-ci repository #1552

Merged
115 changes: 115 additions & 0 deletions .ci/actions/main-sync/action.yml
Original file line number Diff line number Diff line change
@@ -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: [email protected]
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)'
rgdoliveira marked this conversation as resolved.
Show resolved Hide resolved
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/<br \/>/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<br />"
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.<br /><br />\
<b>:warning:Important:warning:</b><br />Please don't merge using squash, not to lose the git history.<br /><br />\
<b>Excluded files:</b><br />${excludedFiles}<br />\
[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
Loading