Sync with Upstream #419
This file contains hidden or 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: Sync with Upstream | |
| on: | |
| schedule: | |
| - cron: '0 0 * * *' # Runs daily at midnight UTC | |
| workflow_dispatch: # Allows manual triggering | |
| jobs: | |
| sync: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout the repository | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 # Fetch all history so that we can compare with upstream | |
| - name: Check for existing sync PR | |
| id: pr_guard | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| EXISTING_PR=$(gh pr list \ | |
| --state open \ | |
| --search "Sync miekg/dns" \ | |
| --json title \ | |
| --jq '.[] | select(.title | startswith("Sync miekg/dns")) | .title') | |
| if [ -n "$EXISTING_PR" ]; then | |
| echo "Existing sync PR found:" | |
| echo "$EXISTING_PR" | |
| echo "Stopping workflow to avoid piling up PRs." | |
| echo "skip_sync=true" >> "$GITHUB_OUTPUT" | |
| exit 0 | |
| else | |
| echo "No existing sync PR found. Continuing." | |
| echo "skip_sync=false" >> "$GITHUB_OUTPUT" | |
| fi | |
| - name: Add upstream remote and fetch | |
| if: steps.pr_guard.outputs.skip_sync != 'true' | |
| run: | | |
| git remote add upstream https://github.com/miekg/dns.git | |
| git fetch upstream | |
| - name: Check for new changes | |
| if: steps.pr_guard.outputs.skip_sync != 'true' | |
| id: check_updates | |
| run: | | |
| UPDATES=$(git rev-list --count HEAD..upstream/master) | |
| if [ "$UPDATES" -gt 0 ]; then | |
| echo "There are $UPDATES new upstream changes available." | |
| echo "updates_available=true" >> $GITHUB_ENV | |
| else | |
| echo "There are no new upstream changes available." | |
| echo "updates_available=false" >> $GITHUB_ENV | |
| fi | |
| - name: Create Sync Branch | |
| if: env.updates_available == 'true' && steps.pr_guard.outputs.skip_sync != 'true' | |
| # Merge upstream changes, preferring upstream changes in case of conflicts. This way we can resolve conflicts with master during the PR review process. | |
| run: | | |
| echo "Merging upstream changes" | |
| git config --global user.email "phillip@cs.stanford.edu" | |
| git config --global user.name "Phillip Stephens" | |
| git merge --no-edit --strategy-option=theirs upstream/master || (git merge --abort && echo "merging was unsuccessful, aborting" && exit 1) | |
| - name: Set branch name | |
| if: env.updates_available == 'true' && steps.pr_guard.outputs.skip_sync != 'true' | |
| run: echo "BRANCH_NAME=sync-upstream-$(date +%Y%m%d)" >> $GITHUB_ENV | |
| - name: Open Pull Request | |
| if: env.updates_available == 'true' && steps.pr_guard.outputs.skip_sync != 'true' | |
| uses: peter-evans/create-pull-request@v7 | |
| with: | |
| branch: ${{ env.BRANCH_NAME }} | |
| delete-branch: true | |
| title: "Sync miekg/dns upstream changes" | |
| body: "Automated PR to sync upstream changes from miekg/dns. Please review and merge if appropriate. | |
| ALERT - If upstream tagged a new version, you'll need to tag manually after merging this PR." | |
| labels: "sync, automated" | |
| assignees: "phillip-stephens" |