Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
14 changes: 3 additions & 11 deletions .github/workflows/sync.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,12 @@ jobs:
- name: Back up and sync all updates
shell: bash
env:
GH_TOKEN: ${{ secrets.WORKFLOW_TOKEN || github.token }}
MIRROR_REPOSITORY: ${{ github.repository }}
REPOSITORY_NAME: ${{ github.event.repository.name }}
UPSTREAM_OWNER: ${{ inputs.upstream_owner }}
run: |
set -euo pipefail

upstream_repository="${UPSTREAM_OWNER}/${REPOSITORY_NAME}"
mirror_date=$(date --date="$(gh repo view "${MIRROR_REPOSITORY}" --json updatedAt --jq .updatedAt)" '+%s')
upstream_date=$(date --date="$(gh repo view "${upstream_repository}" --json updatedAt --jq .updatedAt)" '+%s')

echo "MIRROR_DATE: ${mirror_date}"
echo "UPSTREAM_DATE: ${upstream_date}"

# Only mirror if there are actually updates
if [[ ${mirror_date} -ge ${upstream_date} ]]; then exit 0; fi

git remote add upstream "https://github.com/${upstream_repository}.git"
git fetch --prune upstream

Expand All @@ -58,10 +47,13 @@ jobs:
continue
fi

echo "Syncing ${name} from upstream/${name} to origin/${name}"

backup="backup/${today}/${GITHUB_RUN_ID}-${GITHUB_RUN_ATTEMPT}/${name}"
git branch "${backup}" "${branch}"
git push origin "refs/heads/${backup}"

echo "pushing ${name} to origin/${name}"
git push origin "+${branch}:refs/heads/${name}"
done

Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ The caller's `GITHUB_TOKEN` is used unless an optional `WORKFLOW_TOKEN` secret
is inherited or explicitly passed.
That token needs permission to force-push the mirrored branches and tags under
the repository's branch and tag protection rules.
The workflow uses each repository's GitHub `updatedAt` value to skip a run when
the mirror is at least as recent as its upstream.
The workflow fetches and pushes the upstream branches and tags on every run.

### Beaker Acceptance action:

Expand Down