Update Feature Branches #66
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
branchPrefix: "workflow/auto-merge-feature-branch-" | |
jobs: | |
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: 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.8 | |
- 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: Name new branch | |
run: | | |
date_str=$(date "+%Y%m%d-%H%M%S") | |
echo "NEW_BRANCH=${{env.branchPrefix}}${{github.run_number}}-${date_str}" >> $GITHUB_ENV | |
- 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). | |
" | |
git checkout main | |
git checkout -b "${NEW_BRANCH}" | |
git push --set-upstream origin "${NEW_BRANCH}" | |
pr_number=$(python scripts/gha/create_pull_request.py --token ${{ steps.generate-token.outputs.token }} --base "${{ matrix.branch_name }}" --head "${NEW_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 }}" |