Sync Release with Upstream #10091
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: Sync Release with Upstream | |
on: | |
schedule: | |
# Runs every 5 minutes | |
- cron: '*/5 * * * *' | |
# Allows you to manually trigger the workflow from the Actions tab | |
workflow_dispatch: | |
jobs: | |
sync: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout the forked repository | |
uses: actions/checkout@v3 | |
with: | |
token: ${{ secrets.GITHUB_TOKEN }} | |
persist-credentials: false # We disable this because we will be using a different token | |
- name: Check for UPSTREAM_URL and UPSTREAM_SYNC_BRANCH_NAME in environment | |
id: check_variables | |
run: | | |
if [ -z "${{ vars.UPSTREAM_URL }}" ]; then | |
echo "UPSTREAM_URL environment variable does not exist." | |
echo "HAS_UPSTREAM=false" >> $GITHUB_ENV | |
else | |
echo "UPSTREAM_URL environment variable found." | |
echo "HAS_UPSTREAM=true" >> $GITHUB_ENV | |
echo "UPSTREAM_URL=${{ vars.UPSTREAM_URL }}" >> $GITHUB_ENV | |
fi | |
if [ -z "${{ vars.UPSTREAM_SYNC_BRANCH_NAME }}" ]; then | |
echo "UPSTREAM_SYNC_BRANCH_NAME environment variable does not exist." | |
echo "HAS_SYNC_BRANCH=false" >> $GITHUB_ENV | |
else | |
echo "UPSTREAM_SYNC_BRANCH_NAME environment variable found." | |
echo "HAS_SYNC_BRANCH=true" >> $GITHUB_ENV | |
echo "UPSTREAM_SYNC_BRANCH_NAME=${{ vars.UPSTREAM_SYNC_BRANCH_NAME }}" >> $GITHUB_ENV | |
fi | |
- name: Configure git for GitHub Actions | |
if: env.HAS_UPSTREAM == 'true' && env.HAS_SYNC_BRANCH == 'true' | |
run: | | |
git config user.name "github-actions[bot]" | |
git config user.email "github-actions[bot]@users.noreply.github.com" | |
- name: Add the upstream repository | |
if: env.HAS_UPSTREAM == 'true' && env.HAS_SYNC_BRANCH == 'true' | |
run: git remote add upstream ${{ env.UPSTREAM_URL }} | |
- name: Check for differences between local and upstream branch, excluding .github/workflows | |
if: env.HAS_UPSTREAM == 'true' && env.HAS_SYNC_BRANCH == 'true' | |
run: | | |
git fetch upstream | |
git fetch origin | |
# Check if the branch exists on the remote | |
if git show-ref --verify --quiet refs/remotes/origin/${{ env.UPSTREAM_SYNC_BRANCH_NAME }}; then | |
echo "Branch ${{ env.UPSTREAM_SYNC_BRANCH_NAME }} exists on remote." | |
# Compare origin branch with upstream, excluding .github/workflows | |
git diff --quiet origin/${{ env.UPSTREAM_SYNC_BRANCH_NAME }} upstream/main -- . ':(exclude).github/workflows' || echo "CHANGES_DETECTED=true" >> $GITHUB_ENV | |
else | |
echo "Branch ${{ env.UPSTREAM_SYNC_BRANCH_NAME }} does not exist on remote." | |
# Compare upstream/main with itself to force no changes | |
git diff --quiet upstream/main upstream/main -- . ':(exclude).github/workflows' || echo "CHANGES_DETECTED=false" >> $GITHUB_ENV | |
fi | |
- name: Fetch all branches from the upstream repository | |
if: env.HAS_UPSTREAM == 'true' && env.HAS_SYNC_BRANCH == 'true' && env.CHANGES_DETECTED == 'true' | |
run: git fetch upstream | |
- name: Set up authentication | |
if: env.HAS_UPSTREAM == 'true' && env.HAS_SYNC_BRANCH == 'true' && env.CHANGES_DETECTED == 'true' | |
run: | | |
git remote set-url origin https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }} | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
GITHUB_ACTOR: ${{ github.actor }} | |
- name: Merge upstream/main into forked repo branch | |
if: env.HAS_UPSTREAM == 'true' && env.HAS_SYNC_BRANCH == 'true' && env.CHANGES_DETECTED == 'true' | |
run: | | |
git checkout -B ${{ env.UPSTREAM_SYNC_BRANCH_NAME }} upstream/main | |
# GitHub Actions worker doesn't have the correct permissions to checked in | |
# modified workflow files. To workaround this, we'll remove all files in | |
# .github/workflows that doesn't exist on the remote | |
# Remove files not on the remote | |
rm -rf .github/workflows | |
# Check if the deploy branch exists on the remote | |
if git ls-remote --exit-code --heads origin ${{ env.UPSTREAM_SYNC_BRANCH_NAME }}; then | |
# Mirror remote workflows if they exist | |
if git ls-tree --name-only -r origin/${{ env.UPSTREAM_SYNC_BRANCH_NAME }}:.github/workflows &> /dev/null; then | |
git checkout origin/${{ env.UPSTREAM_SYNC_BRANCH_NAME }} .github/workflows | |
fi | |
fi | |
git commit -am "Prepare branch for deploy" | |
git push -f origin ${{ env.UPSTREAM_SYNC_BRANCH_NAME }} | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
GITHUB_ACTOR: ${{ github.actor }} |