Files

70 lines
2.3 KiB
YAML

name: Dashboard Update
on:
workflow_dispatch:
permissions:
contents: read
jobs:
update:
runs-on: ubuntu-latest
env:
UPDATE_SOURCE_REPOSITORY: ${{ vars.UPDATE_SOURCE_REPOSITORY || 'hieunc229/mailflare' }}
steps:
- name: Check update token
env:
UPDATE_TOKEN: ${{ secrets.MAILFLARE_UPDATE_TOKEN }}
run: |
if [ -z "${UPDATE_TOKEN}" ]; then
echo "::error::Add MAILFLARE_UPDATE_TOKEN as a repository Actions secret."
exit 1
fi
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.MAILFLARE_UPDATE_TOKEN }}
- name: Replace repository with upstream source
run: |
set -euo pipefail
git config user.name "mailflare-update"
git config user.email "mailflare-update@users.noreply.github.com"
upstream_url="https://github.com/${UPDATE_SOURCE_REPOSITORY}.git"
if git remote get-url upstream >/dev/null 2>&1; then
git remote set-url upstream "${upstream_url}"
else
git remote add upstream "${upstream_url}"
fi
git fetch --no-tags upstream
upstream_branch="$(git remote show upstream | sed -n 's/.*HEAD branch: //p')"
if [ -z "${upstream_branch}" ]; then
echo "::error::Could not resolve the default branch of ${UPDATE_SOURCE_REPOSITORY}."
exit 1
fi
upstream_ref="upstream/${upstream_branch}"
echo "Replacing ${GITHUB_REF_NAME} with ${upstream_ref}."
# Keep the installation repository's commit history while replacing
# its complete tracked tree with the current upstream tree.
git read-tree --reset -u "${upstream_ref}"
if git diff --cached --quiet; then
echo "Repository is already up to date."
exit 0
fi
git commit -m "Update Mailflare from ${UPDATE_SOURCE_REPOSITORY}@${upstream_branch}"
- name: Push update
run: |
set -euo pipefail
if [ -z "$(git log "origin/${GITHUB_REF_NAME}..HEAD" --oneline)" ]; then
echo "Repository is already up to date."
exit 0
fi
git push origin "HEAD:${GITHUB_REF_NAME}"