Jillian Hoenig
Jillian Hoenig

Reputation: 247

Prevent wp-config.php (database connection) from being overwritten upon git automated deployments

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:

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!

Upvotes: 0

Views: 607

Answers (1)

bk2204
bk2204

Reputation: 76804

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.

Upvotes: 1

Related Questions