diff --git a/.github/actions/merge-main/action.yml b/.github/actions/merge-main/action.yml new file mode 100644 index 0000000..b73bff2 --- /dev/null +++ b/.github/actions/merge-main/action.yml @@ -0,0 +1,22 @@ +name: Merge Main +description: Merges main into feature branches (*-main) +inputs: + exempt-branches: + description: Feature branches that are exempt from receiving merges from main (comma separated, no blank space) + +runs: + using: composite + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Set up Git + shell: bash + run: | + git config user.name aws-sdk-kotlin-ci + git config user.email "aws-kotlin-sdk-automation@amazon.com" + + - name: Run Script + shell: bash + run: | + .github/scripts/merge-main.sh ${{ inputs.exempt-branches }} \ No newline at end of file diff --git a/.github/scripts/merge-main.sh b/.github/scripts/merge-main.sh new file mode 100755 index 0000000..61dd87d --- /dev/null +++ b/.github/scripts/merge-main.sh @@ -0,0 +1,83 @@ +#!/bin/bash + +input=$1 + +function fetch_latest_changes() { + echo "Fetching the latest remote branches" + git fetch --all +} + +fetch_latest_changes + +function find_feature_branches() { + echo "Searching for feature branches" + feature_branches=($(git branch -r --list "*-main" | sed 's|origin/||')) + + if [ ${#feature_branches[@]} -eq 0 ]; then + echo "...none found" + return + fi + + for feature_branch in "${feature_branches[@]}"; do + echo "...found feature branch: $feature_branch" + done +} + +find_feature_branches + +function find_exempt_branches() { + echo "Searching for exempt branches" + IFS=',' read -r -a exempt_branches <<< "$input" + + if [ ${#exempt_branches[@]} -eq 0 ]; then + echo "...none found" + return + fi + + for exempt_branch in "${exempt_branches[@]}"; do + echo "...found exempt branch: $exempt_branch" + done +} + +find_exempt_branches + +function filter_feature_branches() { + echo "Filtering branches" + branches=() + + for feature_branch in "${feature_branches[@]}"; do + if ! [[ "${exempt_branches[*]}" =~ "$feature_branch" ]]; then + echo "...including feature branch: $feature_branch" + branches+=("$feature_branch") + else + echo "...excluding feature branch: $feature_branch" + fi + done +} + +filter_feature_branches + +function merge_main() { + echo "Merging main into branches" + + if [ ${#branches[@]} -eq 0 ]; then + echo "...no branches to merge into" + return + fi + + for branch in "${branches[@]}"; do + echo "...switching to branch: $branch" + git switch "$branch" + echo "...merging main" + git merge -m "misc: merge from main" main + if [ $? -eq 0 ]; then + echo "...pushing to origin" + git push origin "$branch" + else + echo "...merge failed" + git merge --abort + fi + done +} + +merge_main \ No newline at end of file diff --git a/.github/workflows/merge-main.yml b/.github/workflows/merge-main.yml new file mode 100644 index 0000000..560876b --- /dev/null +++ b/.github/workflows/merge-main.yml @@ -0,0 +1,14 @@ +name: Merge main +on: + schedule: + - cron: "0 7 * * 1-5" # At 07:00 UTC (00:00 PST, 03:00 EST), Monday through Friday + workflow_dispatch: + +jobs: + test: + runs-on: ubuntu-latest + steps: + - name: Merge main + uses: awslabs/aws-kotlin-repo-tools/.github/actions/merge-main@main + with: + exempt-branches: # Add any if required \ No newline at end of file