Skip to content

Add script to merge main into all active feature branches on a regular schedule. #1394

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

Merged
merged 29 commits into from
Jul 23, 2023
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
12ea723
Add workflow for automatically updating feature branches weekly.
jonsimantov Jul 20, 2023
b8a73c8
Corrected filename, and added inputs.
jonsimantov Jul 20, 2023
af49149
Remove trailing spaces.
jonsimantov Jul 20, 2023
2e2137a
Fix workflow.
jonsimantov Jul 20, 2023
d2a8f35
Fix workflow name.
jonsimantov Jul 20, 2023
98faefb
Fix syntax.
jonsimantov Jul 20, 2023
41dedd0
Fix syntax.
jonsimantov Jul 20, 2023
5bf3fe8
List remote branches instead.
jonsimantov Jul 20, 2023
90a3e75
Clean up script.
jonsimantov Jul 20, 2023
601e4cc
Untab.
jonsimantov Jul 20, 2023
d63ee26
Add branch list for debugging.
jonsimantov Jul 20, 2023
707b92b
Untab.
jonsimantov Jul 20, 2023
782873f
Specify remote branches.
jonsimantov Jul 20, 2023
a686cc3
Skip second stage if no first.
jonsimantov Jul 20, 2023
997046a
Typo
jonsimantov Jul 20, 2023
c7e7f05
Error.
jonsimantov Jul 20, 2023
1abb4d6
List all branches.
jonsimantov Jul 20, 2023
8e54a6a
Fix logic.
jonsimantov Jul 20, 2023
55f2ae3
Fix spacing.
jonsimantov Jul 20, 2023
835ec7c
Fix output
jonsimantov Jul 20, 2023
bc31e18
Fix parameters.
jonsimantov Jul 20, 2023
266925b
Fix merge to use origin.
jonsimantov Jul 20, 2023
be988c4
Remove debug echos.
jonsimantov Jul 20, 2023
ebfed91
Add git config.
jonsimantov Jul 20, 2023
18b73a6
Fix PR creation.
jonsimantov Jul 20, 2023
6fbca64
Fix PR creation.
jonsimantov Jul 20, 2023
85f818f
Merge branch 'main' into update-all-feature-branches
jonsimantov Jul 20, 2023
62339be
Fix automatic push.
jonsimantov Jul 20, 2023
2be2875
Merge branch 'update-all-feature-branches' of https://github.com/fire…
jonsimantov Jul 20, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 119 additions & 4 deletions .github/workflows/update-feature-branches.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,126 @@ name: Update Feature Branches
on:
workflow_dispatch:
inputs:
branch_patterns:
description: 'Space-separated list of feature branch patterns'
default: 'feature_branch/*'
required: true
main_branch:
description: 'Main branch to merge'
default: 'main'
required: true
schedule:
- cron: "0 16 * * 1" # Mondays, 4pm UTC = 9am PST / 10am PDT

env:
defaultBranchPattern: "feature_branch/*"
defaultMainBranch: "main"
triggerTestsLabel: "tests-requested: quick"

jobs:
no_op:
name: no-op
list_feature_branches:
name: list-feature-branches
runs-on: ubuntu-20.04
outputs:
branch_list: ${{ steps.get-branches.outputs.branch_list }}
steps:
- name: Check out repo (if needed)
if: ${{ github.event.inputs.branch_list == '' }}
uses: actions/checkout@v3

- name: Get list of feature branches
id: get-branches
run: |
branch_pattern='origin/${{ env.defaultBranchPattern }}'
if [[ -n '${{ github.event.inputs.branch_patterns }}' ]]; then
branch_pattern=origin/$(echo '${{ github.event.inputs.branch_patterns }}' | sed 's| | origin/|g')
fi
git remote update
echo "Branch pattern: ${branch_pattern}"
branch_list=$(git branch --list --all "${branch_pattern}")
if [[ -n ${branch_list} ]]; then
# If there's at least one entry, process the list.
echo "Remote branch list: ${branch_list}"
# Remove remotes/origin/ from each branch.
branch_list=$(echo ${branch_list} | sed 's| remotes/origin/| |g' | sed 's|^remotes/origin/||')
# Change spaces to commas.
branch_list=$(echo ${branch_list} | sed 's/ /,/g')
# Add quotes around each branch name.
branch_list='"'$(echo ${branch_list} | sed 's/,/","/g')'"'
fi
echo "::warning ::Branch list: [${branch_list}]"
echo "branch_list=[${branch_list}]" >> $GITHUB_OUTPUT

create_merge_prs:
name: create-merge-pr-${{ matrix.branch_name }}
needs: [ list_feature_branches ]
runs-on: ubuntu-20.04
if: ${{ needs.list_feature_branches.outputs.branch_list != '[]' }}
strategy:
fail-fast: false
matrix:
branch_name: ${{ fromJson(needs.list_feature_branches.outputs.branch_list) }}
steps:
- name: noop
run: true
- name: Get token for firebase-workflow-trigger
uses: tibdex/github-app-token@v1
id: generate-token
with:
app_id: ${{ secrets.WORKFLOW_TRIGGER_APP_ID }}
private_key: ${{ secrets.WORKFLOW_TRIGGER_APP_PRIVATE_KEY }}

- name: Setup python
uses: actions/setup-python@v4
with:
python-version: 3.7

- uses: actions/checkout@v3
with:
ref: ${{ matrix.branch_name }}
fetch-depth: 0
submodules: false

- name: Install prerequisites
run: |
python scripts/gha/install_prereqs_desktop.py
python -m pip install requests

- name: Create merge PR
id: create-pr
run: |
git config user.email "[email protected]"
git config user.name "firebase-workflow-trigger-bot"
git config core.commentChar "%" # so we can use # in git commit messages

main_branch='${{ env.defaultMainBranch }}'
if [[ -n '${{ github.event.inputs.main_branch }}' ]]; then
main_branch='${{ github.event.inputs.main_branch }}'
fi
# Attempt a merge, then check if any files changed.
git merge --no-commit --no-ff "origin/${main_branch}" || true
if git diff --quiet ${{ matrix.branch_name }}; then
# No merge necessary.
echo "::warning ::No merge needed for ${{ matrix.branch_name }}, won't create pull request."
echo "created_pr_number=0" >> $GITHUB_OUTPUT
exit 0
fi

# Undo the actual merge. Let the PR creation handle it.
git merge --abort

date_str=$(date "+%b %d, %Y")

pr_title="Automatic merge of ${main_branch} into ${{ matrix.branch_name }} - ${date_str}"
pr_body="Automatic merge of ${main_branch} into ${{ matrix.branch_name }}.

> Created on ${date_str} by [${{github.workflow}} workflow]($GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID).
"
pr_number=$(python scripts/gha/create_pull_request.py --token ${{ steps.generate-token.outputs.token }} --base "${{ matrix.branch_name }}" --head "${main_branch}" --title "${pr_title}" --body "${pr_body}")
echo "created_pr_number=${pr_number}" >> $GITHUB_OUTPUT

- name: Set test trigger label.
uses: actions-ecosystem/action-add-labels@v1
if: ${{ steps.create-pr.outputs.created_pr_number }}
with:
github_token: ${{ steps.generate-token.outputs.token }}
number: ${{ steps.create-pr.outputs.created_pr_number }}
labels: "${{ env.triggerTestsLabel }}"