Skip to content

Email for professionals and teams

fix(updater): merge upstream instead of overwriting the working tree

Bruce McLachlan8b6177de03c1
diff --git a/.github/workflows/deploy-update.yml b/.github/workflows/deploy-update.yml
index b076d9f..9a458c8 100644
--- a/.github/workflows/deploy-update.yml
+++ b/.github/workflows/deploy-update.yml
@@ -20,31 +20,34 @@ jobs:
with:
fetch-depth: 0
- - name: Download latest release
- env:
- GH_TOKEN: ${{ github.token }}
+ - name: Merge upstream changes
run: |
set -euo pipefail
- release_json="$(curl -fsSL \
- -H "Accept: application/vnd.github+json" \
- -H "Authorization: Bearer ${GH_TOKEN}" \
- -H "X-GitHub-Api-Version: 2022-11-28" \
- "https://api.github.com/repos/${UPDATE_SOURCE_REPOSITORY}/releases/latest")"
- tarball_url="$(node -e 'const fs = require("fs"); const release = JSON.parse(fs.readFileSync(0, "utf8")); process.stdout.write(release.tarball_url);' <<< "${release_json}")"
- mkdir -p /tmp/mailflare-release
- curl -fsSL \
- -H "Accept: application/vnd.github+json" \
- -H "Authorization: Bearer ${GH_TOKEN}" \
- "${tarball_url}" \
- -o /tmp/mailflare-release.tar.gz
- tar -xzf /tmp/mailflare-release.tar.gz -C /tmp/mailflare-release --strip-components=1
- rsync -a --delete \
- --exclude ".git" \
- --exclude ".github" \
- --exclude "node_modules" \
- --exclude ".dev.vars" \
- --exclude "wrangler.jsonc" \
- /tmp/mailflare-release/ ./
+ git config user.name "mailflare-update"
+ git config user.email "[email protected]"
+
+ 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
+ echo "Merging upstream/${upstream_branch} into ${GITHUB_REF_NAME}."
+
+ # A merge keeps local modifications instead of overwriting them, and
+ # stops on conflict rather than silently discarding local work.
+ if ! git merge --no-edit "upstream/${upstream_branch}"; then
+ git merge --abort || true
+ echo "::error::Merge conflict with upstream/${upstream_branch}. Resolve it locally and push."
+ exit 1
+ fi
- name: Setup Node.js
uses: actions/setup-node@v4
@@ -57,15 +60,11 @@ jobs:
- name: Apply database migrations
run: npx wrangler d1 migrations apply DB --remote
- - name: Commit update
+ - name: Push update
run: |
set -euo pipefail
- if [ -z "$(git status --porcelain)" ]; then
+ if [ -z "$(git log "origin/${GITHUB_REF_NAME}..HEAD" --oneline)" ]; then
echo "Repository is already up to date."
exit 0
fi
- git config user.name "mailflare-update"
- git config user.email "[email protected]"
- git add --all
- git commit -m "chore: update Mailflare to latest release"
git push origin "HEAD:${GITHUB_REF_NAME}"