1

On my webserver I currently have a Virtual Host, with an almost exact copy of my main public website, set up for testing purposes so I can edit my website and test new features without editing my live website. It has a simple .htaccess and .htpasswd authentication using

AuthType Basic
AuthName "Restricted access"
AuthUserFile /home/USERNAME/.htpasswd
require valid-user

I am wondering if it is possible to have a branch from my main website GitHub repo that, when I merge the changes from the test branch to the master branch, it will not merge the .htaccess and .htpasswd files.

I'm also wondering if it is possible to have the test branch link to a different folder on my webserver. For example the master branch will be linked to /var/www/html and I would like the test branch to be linked to /var/www/test.

Please make answers idiot proof as I still only have a very basic understanding of Git and GitHub

Harry Cameron
  • 97
  • 2
  • 13
  • Still, a content filter driver should be the answer here. And https://stackoverflow.com/q/22909620/6309 provides with a very detailed example. – VonC Jan 08 '18 at 05:57
  • one option is to keep the production/test branches the same, don't futz around with git trying to keep the delta constant, and just put the "deploy test" vs "deploy prod" logic in your "depoy" program/logic/script, whatever that may be (eg a bash script, a gradle build, a jenkins job, whatever). – michael Jan 08 '18 at 08:13
  • @michael what do you mean by that? – Harry Cameron Jan 08 '18 at 08:51
  • added a more detailed comment as an answer :-) – michael Jan 08 '18 at 10:46

2 Answers2

1

This answer kind of side-steps the issue by avoiding the problem (and added as a more detailed follow-up to my comment). Let's say you have two branches (for simplicity), master where all dev & testing happens (note: normally I'd have separate branches for both dev & test, though), and a second branch for production, prod. I'll assume these are both already set up, and identical, and currently contains your production website. (I don't know which OS you use for development, but I'll assume linux?) Then, make some changes in master, deploy to your test staging area using a script something like deploy.sh:

#!/bin/bash
# usage: deploy [test | prod]
deploy_test() {
    git checkout master
    # copy site to remote
    scp -r ./path/to/site me@host:/var/www/test

    # or, you could generate htpasswd here
    read -p 'Input path to your htpasswd file: ' pw 
    scp -r $pw ./path/to/site me@host:~/
}

deploy_prod() {
    git checkout prod
    # copy site to remote
    scp -r ./path/to/site me@host:/var/www/html
}

if [ "$1" = "prod" ]; then 
    deploy_prod
elif [ "$1" = "test" ]; then 
    deploy_test
fi

Ok, that's a crazy simple example, but that same idea can be implemented in whatever you're using for deployment. Everything is checked-in to git, except anything related to authentication (passwords, users), which are either env vars or typed into the deploy script. Further, a tool like jenkins ( https://jenkins-ci.org/ ) could call these scripts, and be used for test / deployment from a QA server to your staging host ...ok, you can see how things can slowly move from "small script" to "deployment pipeline" -- the key is keep each step simple, documented, and build it out slowly. Keep doing the simplest thing possible, and build on that. (note: I just typed all this into S.O., so it's an example only, not production code :-) )

michael
  • 9,161
  • 2
  • 52
  • 49
  • That is very helpful, 2 questions though, one what changes the $1 at the end of the code and 2, would both the .htaccess files be the same considering that on the test server it would have to be different than the main server as it includes the authentication in the file on the test server. To over come this I guess I could just add the .htaccess file to my .gitignore file and if I need to edit it I can just do it on the server – Harry Cameron Jan 08 '18 at 11:26
  • (nb: just fixed a copy/paste typo) - yes, you can further customize test vs prod -- and actually may end up returning to a more complicated `git` option. (personally: I've tried the same via git in the past, and have always reverted back to something simpler). E.g., check-in a template file & modify via `sed` (gradle/ant/maven/jenkins all directly support this); or selecting a file to deploy based on extension (eg `htaccess.prod` vs `htaccess.test`). Always just pick whatever is easiest to implement at that time -- it's too easy to waste time going down some of the deeper git rabbit holes. – michael Jan 08 '18 at 11:47
  • Ah the typo now makes sense, you mentioned using Jenkins, how would I go about using Jenkins to call the script? – Harry Cameron Jan 10 '18 at 16:54
0

Whenever you need to keep different content based on branches, one way is to:

  • version only a template file
  • seek the value files outside the repo (if they are confidential like htpasswd should be) or in versioned files named after the branch (if you want to track them in the same repo, like .htaccess.master, .htaccess.test)
    That way, there is no merge issue.

For that, you would register (in a .gitattributes declaration) in your submodule repo a content filter driver.

smudge (image from "Customizing Git - Git Attributes", from "Pro Git book")

The smudge script, associate to the template file (.htpasswd.tpl, .htaccess.tpl), would generate (automatically on git checkout) the properties file by looking in outside source the actual values. The generated actual .htxxx properties files remain ignored (by the .gitignore).

See a complete example at "git smudge/clean filter between branches".

Your smudge script can determine the name of the checked out branch with:

branch=$(git rev-parse --symbolic --abbrev-ref HEAD)
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Would you be able to dumb your answer down a little bit, its a bit confusing for me. I've had a look at the links but thats even more confusing – Harry Cameron Jan 08 '18 at 08:51
  • @HarryCameron what part is confusing? First, do you like the idea of having those files generated, avoiding any merge issue? – VonC Jan 08 '18 at 08:53
  • I don't really understand what you mean about generating the files. Sorry novice here – Harry Cameron Jan 08 '18 at 08:55
  • @HarryCameron Simply that `.htpasswd` and `.htaccess` should not be versioned (since their content should automagically change depending on the branch you are using). Instead, they should be automatically generated with the right content depending on the branch. – VonC Jan 08 '18 at 08:56
  • ohhh ok that makes sense – Harry Cameron Jan 08 '18 at 08:58
  • Well the smudge/clean scripts – Harry Cameron Jan 08 '18 at 09:28
  • @HarryCameron In yur case, you only need a smudge script, which is a file versioned in your repo, that you can give any name to. You will declare the smudge script in your .gitattributes files, as well as in your local git config, as illustrated in https://stackoverflow.com/a/2316728/6309. That script will be in charge of generated your actual valued files `.htpasswd` and `.htaccess`, copying template files (`.htpasswd.tpl` and `.htaccess.tpl`) and replacing the placeholders with actual values depending on the branch being checked out. – VonC Jan 08 '18 at 09:31
  • Ok that makes a bit more sense, what would I put in the template file and also would I be able to have the Test branch located on a separate directory from the master branch so Vhosts can work – Harry Cameron Jan 08 '18 at 09:33
  • @HarryCameron The first thing you would put in is the `branch=$(git rev-parse --symbolic --abbrev-ref HEAD)` to get the branch name currently checked out. Then, depending on the branch, you can combine the template file and the value files in order to generate the actual files. – VonC Jan 08 '18 at 09:34
  • @HarryCameron If your value files can actually set those values as environment variables, then your smudge script can simply use envsubst: https://stackoverflow.com/a/12006837/6309 – VonC Jan 08 '18 at 09:35
  • Ok this is starting to get confusing again, I think I might sway away from using branches for testing and production and just use separate repos – Harry Cameron Jan 08 '18 at 09:38
  • @HarryCameron What part is confusing: this is just about generated the right files depending no the branch. But instead of using separate repos, you could still using separate branches, and clone your repo twice with git worktree. https://stackoverflow.com/a/30185564/6309 – VonC Jan 08 '18 at 09:40