Search code examples
gitdeploymentversion-controlweb-deploymentgit-post-receive

Git pass variable through push to remote server


I have a server set up as a "production" remote, which runs a post-recieve shell script to deploy the site with either a 'hard' deploy for the master branch. Or a 'soft' deploy for the dev branch to different directories.

soft (fast, low resources) - checks out the work-tree to the /dev directory (fast)

hard (slower, more resources) - check out the work-tree to a new '/temp' directory, 
       copies the non-tracked files from the '/live' directory to '/temp' (can be large)
       if successful, renames the '/live' directory to '/live-old'
       renames the '/temp' to '/live' and deletes the older '/live-old' directory

Sometimes I only want to do a 'soft' deploy on the master branch push if it's something tiny like a hotfix, as a hard one can take a while to copy non-tracked files like uploads etc, and seems to waste server resources say if doing multiple hotfixes for different applications.

Can I pass a variable from my local server (like in the remote section of the git config file) to the remote server to tell it to do a soft or hard deploy for the master branch?

I was thinking if the remote server knows what the local remote name is I could us that, or if I can add a variable to the url in the remote definition like:

[remote "production-hard"]
    url = ssh://project.git?deploy=hard
    fetch = +refs/heads/*:refs/remotes/production/*
[remote "production-soft"]
    url = ssh://project.git?deploy=soft
    fetch = +refs/heads/*:refs/remotes/production/*

I'm sure this way probably doesn't work, but hopefully you can understand what I'm trying to do.

Also, please say if this is really bad practise and I should use another method.

Thanks.


Solution

  • No, you cannot pass additional variables.

    But you could evaluate the branch name being pushed to.

    So, you could set up a post-receive hook that inspects the branch being pushed to:

    • push to production-hard → do the hard deploy
    • push to production-soft → do the soft deploy