0

I'm having trouble getting git to ignore my wp-config.php file on deployments. I set up automated deployments with git (it's a really great tool, if you don't know about it you can read about it here - you don't need to be using kinsta hosting like the article indicates, although your file structure may vary).

But every time I push changes, it overwrites my wp-config.php file, causing a database error on the site. This happens because the git hook used for deployments introduces the changes, then checks out any local changes. Since this is a staging site, it uses the repo from production, but has different values in wp-config.php, in order to connect to its own database.

What I've tried so far:

  • committing the changes to wp-config on the staging server, so it won't get checked out by the git hook
  • adding wp-config.php to .gitignore (I should've done originally) on staging server
  • adding wp-config.php to .gitignore (I should've done originally) on development and pushing to staging
  • adding a line to the post-receive hook to checkout the changes to wp-config.php after the deployment is performed

Here's my post-receive hook file (client name replaced with XXXXX):

#!/bin/bash
TARGET="/www/XXXXX_975/public"
GIT_DIR="/www/XXXXX_975/private/XXXXX.git"
BRANCH="master"

while read oldrev newrev ref
do
        # only checking out the master (or whatever branch you would like to deploy)
        if [[ $ref = refs/heads/$BRANCH ]];
        then
                echo "Ref $ref received. Deploying ${BRANCH} branch to staging..."
                git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f
                git checkout /www/XXXXX_975/public/wp-config.php
        else
                echo "Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server."
        fi
done

The site lives in the public directory. The repo is in a different directory private so as not to be overwritten by deployments.

This is a Wordpress website, hosted on Kinsta. Let me know if there's any information I forgot to include.

Thank you!

Jillian Hoenig
  • 137
  • 1
  • 6
  • 28
  • Is your `wp-config.php` file checked into the repository? – bk2204 Jul 09 '21 at 15:47
  • Hey @bk2204! Could you clarify your question? – Jillian Hoenig Jul 09 '21 at 15:49
  • Is the `wp-config.php` file you're seeing overwritten part of the checked in files in the repository (that is, it's listed as part of `git ls-files`), or is it something that is just in the same directory and not tracked by Git? – bk2204 Jul 09 '21 at 15:57
  • Yes it's tracked by git. I mentioned that I retroactively added it to gitignore, but it is being tracked by git. – Jillian Hoenig Jul 09 '21 at 15:58
  • If you already added it to `.gitignore`, then backup the config files on the servers. After that remove the files from git tracking using `git rm`. Then deploy that commit (which will delete the files from the server!) and restore the files manually from your backup. After you did that, git will not touch those files in future (because they are not tracked anymore and also `.gitignore`'d) – Jay Jul 09 '21 at 16:01
  • Incidentally, this sort of thing is one reason (but not the only one) that raw Git makes a poor deployment system. Sure, the file should never have been committed in the first place—but given a real deployment system, you could correct the problem without the multi-step "deploy, fix, deploy again" thing. – torek Jul 09 '21 at 21:50

1 Answers1

1

When you check out a revision without specifying specific paths, Git will always overwrite the files the working tree with those in the revision, and there's no way to avoid this. Your specific case is trying to ignore changes to a tracked file, which the Git FAQ explains cannot be done.

Since you've realized that this file shouldn't have been tracked in the first place and should have been ignore, you can just do git rm --cached wp-config.php and then commit to remove it from the repository. The file will then be deleted when you push it to the remote servers, but you will have the benefit in the future that you can configure a custom version for each of the staging and production servers in the future and Git will leave it alone.

bk2204
  • 64,793
  • 6
  • 84
  • 100
  • *Editing my comment because I misunderstood what @Jay said above* Will this result in any downtime? Is there a way to do this without downtime (at least for production)? – Jillian Hoenig Jul 09 '21 at 16:33
  • You can edit your deployment script to copy the file out of the way, then do the checkout, then copy it back. That will leave a small amount of downtime (maybe a second), but it shouldn't be substantial. – bk2204 Jul 09 '21 at 17:41