Configure sync with upstream action #40
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: 'Upstream Sync' | |
permissions: | |
contents: write | |
actions: write | |
pull-requests: write | |
on: | |
pull_request: | |
schedule: | |
- cron: '0 7 * * 1' | |
# scheduled at 07:00 every Monday | |
workflow_dispatch: | |
inputs: | |
sync_test_mode: | |
description: 'Fork Sync Test Mode' | |
type: boolean | |
default: false | |
jobs: | |
sync_latest_from_upstream: | |
runs-on: ubuntu-latest | |
name: Sync latest commits from upstream repo | |
steps: | |
- name: Checkout target repo | |
uses: actions/checkout@v4 | |
with: | |
ref: upstream | |
- name: Configure Git | |
run: | | |
git config user.name 'GitHub Actions' | |
git config user.email '[email protected]' | |
- name: Sync upstream changes | |
id: sync | |
run: | | |
git remote add --fetch upstream https://github.com/polkadot-js/apps.git | |
git branch -u upstream/master upstream | |
git pull upstream --ff-only | |
- name: Check if there are new commits | |
id: check_commits | |
run: | | |
if git status --porcelain | grep -q '^\s*modified:.*'; then | |
echo "New commits found" | |
echo "new_commits=true" >> $GITHUB_ENV | |
else | |
echo "This branch is up to date" | |
echo "new_commits=false" >> $GITHUB_ENV | |
fi | |
- name: Push changes to origin | |
if: steps.check_commits.outputs.new_commit == 'true' | |
run: | | |
git push origin upstream | |
- name: Check if PR already exists | |
if: steps.check_commits.outputs.new_commit == 'true' | |
id: check_pr_exists | |
env: | |
GH_TOKEN: ${{ github.token }} | |
run: | | |
pr_count=$(gh pr list --repo ${{ github.repository }} --json 'headRefName' --jq 'map(select(.headRefName == "upstream")) | length') | |
echo "pr_count=${pr_count}" >> $GITHUB_ENV | |
if [ "$pr_count" -gt 0 ]; then | |
echo "Pull Request already exists." | |
else | |
echo "Pull Request not found. It will be created" | |
fi | |
- name: Create Pull Request | |
if: steps.check_commits.outputs.new_commit == 'true' && steps.check_pr_exists.outputs.pr_count == '0' | |
env: | |
GH_TOKEN: ${{ github.token }} | |
run: | | |
WORKFLOW_URL="https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" | |
gh pr create --repo ${{ github.repository }} --title "Upstream Sync" --body "[View Workflow Run](${WORKFLOW_URL})" --head upstream --base test-auto-pr --reviewer ${{ secrets.REVIEWER_1 }} |