Skip to content

Instantly share code, notes, and snippets.

@jazzsequence
Created November 9, 2023 21:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jazzsequence/742b919850a9f2bb1f2b5267666dcd11 to your computer and use it in GitHub Desktop.
Save jazzsequence/742b919850a9f2bb1f2b5267666dcd11 to your computer and use it in GitHub Desktop.
Update a Pantheon site's WordPress version to a _specific_ version if multiple updates are available.

When multiple WordPress versions have passed without updates being applied to your site, you will get a list of the updates in your Pantheon dashboard but no way to apply specific updates to your site.

Screenshot 2023-11-09 at 12 58 26 PM

Generally, the only way to apply those updates is to click the Apply Updates button or use terminus upstream:updates:apply, but either of those options will give you all the updates. What if you only wanted to update to a specific WordPress version?

That's what this guide is here to illustrate!

Step 1: Add pantheon-systems/WordPress as a Git remote

The first step is setting our upstream repository as a remote for your site. A Git remote is simply a place where your code lives. Typically, on a Pantheon site, you have a single remote, your Pantheon Git repository. By adding the pantheon-systems/WordPress repo as an alternate remote, it means you'll be able to directly pull the same changes down to your site that would get applied to your site when you apply updates. To do this, you'll run the following command:

git remote add upstream git@github.com:pantheon-systems/WordPress.git

Once that's done, you should be able to run this to validate that you have an origin remote (the default) and an upstream remote (the one you just added).

git remote -v

You should see two listings for origin (for (push) and (fetch)) and two more for upstream. The origin remotes will both point to your Pantheon Git repository and the upstream remotes will point to git@github.com:pantheon-systems/WordPress.

Step 2: Get a list of available updates

Using Terminus, we can get a list of all the available updates. While this is mostly telling us what we can already see in the dashboard, the thing that's most interesting about what the Terminus command gives us is the commit hash of those updates.

terminus upstream:updates:list <site>.<env>

Screenshot 2023-11-09 at 2 00 26 PM

Pay attention to the Commit ID column, we'll need it later.

Step 3: Pull from the upstream and set to your desired update commit

This is where some Git magic comes in. You'll want to pull from your upstream remote, applying the incoming changes from those updates, and then you'll want to set your repository to your specific desired update. Let's break this down.

Pull from the upstream.

git pull -X theirs upstream master

This step will pull all updates from the upstream remote repository. The -X theirs flag makes sure that we use the changes that are incoming in the case of any conflicts. This will actually bring you up to date with the latest update, but don't worry, we'll fix that in a second.

Reset to the commit hash of the update you want to apply

Remember I said to hang onto that Commit ID? Here's where it comes in. That ID is what's known as a "hash" and a Git hash is a marker to a specific commit. By using this hash, we will be able to reset the history to the specific commit for the update you want to apply. We're going to do this by using git reset.

git reset --hard <Commit ID>

In this case the <Commit ID> refers to the ID or hash from the terminus upstream:updates:list command you ran earlier that references the specific update you want. Passing the --hard flag says to discard any changes that are from any commits that came after the one that you're applying, which is what we want.

Force push the result

You'll want to push these changes up to your site now, and to make sure that you're pushing the right commit you might want to add --force. Technically, since the Pantheon remote won't be ahead of your changes (it never received those updates, after all, since you were only working locally) the --force probably isn't needed here, but sometimes when using git reset, Git will yell at you if you try to push and update that's actually behind the history, so I would just use it anyway.

git push --force origin master

That's it!

At this point you should be able to go back to your dashboard and check for updates. The updates that appear should be the ones that would apply after the update you just pushed to your site.

Essentially, this process is just doing what Pantheon does behind the scenes when you apply updates since those WordPress updates are coming from pantheon-systems/WordPress. By hooking into that repository directly, you're able to have more control over what WordPress updates you are pushing to your site.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment