Skip to content

Instantly share code, notes, and snippets.

@rodrigogalura
Forked from noelboss/git-deployment.md
Created January 5, 2021 09:45
Show Gist options
  • Save rodrigogalura/0f0456816a6bc24d029abcf6bd438d79 to your computer and use it in GitHub Desktop.
Save rodrigogalura/0f0456816a6bc24d029abcf6bd438d79 to your computer and use it in GitHub Desktop.

Revisions

  1. @noelboss noelboss revised this gist Mar 21, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion post-receive
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ 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 ]];
    if [ "$ref" = "refs/heads/$BRANCH" ];
    then
    echo "Ref $ref received. Deploying ${BRANCH} branch to production..."
    git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f $BRANCH
  2. @noelboss noelboss revised this gist Mar 21, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion git-deployment.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    # Simple automated GIT Deployment using GIT Hooks

    Here are the simple steps needed to create a deployment from your lokal GIT repository to a server based on [this](https://www.digitalocean.com/community/tutorials/how-to-use-git-hooks-to-automate-development-and-deployment-tasks) in-depth
    Here are the simple steps needed to create a deployment from your local GIT repository to a server based on [this](https://www.digitalocean.com/community/tutorials/how-to-use-git-hooks-to-automate-development-and-deployment-tasks) in-depth
    tutorial.

    ## How it works
  3. @noelboss noelboss revised this gist Mar 21, 2019. 1 changed file with 4 additions and 16 deletions.
    20 changes: 4 additions & 16 deletions git-deployment.md
    Original file line number Diff line number Diff line change
    @@ -40,23 +40,11 @@ Now we create a "bare" repository – one that does not contain the working copy

    $ git init --bare ~/project.git

    ## 4. Add the post-receive hook
    This scrtipt is executed when the push from the local machine has been completed and moves the files into place. It recides in project.git/hooks/ and is named 'post-receive'. You can use vim to edit and create it. The script does check if the correct branch is pushed (not deploying a develop branch for example) and

    #!/bin/bash
    while read oldrev newrev ref
    do
    # only checking out the master (or whatever branch you would like to deploy)
    if [[ $ref =~ .*/master$ ]];
    then
    echo "Master ref received. Deploying master branch to production..."
    git --work-tree=/home/webuser/deploy-folder/ --git-dir=/home/webuser/project.git/ checkout -f
    else
    echo "Ref $ref successfully received. Doing nothing: only the master branch may be deployed on this server."
    fi
    done
    ## 4. Add the post-receive hook script
    This scrtipt is executed when the push from the local machine has been completed and moves the files into place. It recides in project.git/hooks/ and is named 'post-receive'. You can use vim to edit and create it. The script does check if the correct branch is pushed (not deploying a develop branch for example). You can download a sample [post-receive](#file-post-receive) script below. Also, don't forget to add execute permissions to said script;

    chmod +x post-receive

    Download [post-receive](#file-post-receive) for an improved version
    ## 5. Add remote-repository localy
    Now we add the this bare repository to your local system as a remote. Where "production" is the name you want to give the remote. This also be called "staging" or "live" or "test" etc if you want to deploy to a different system or multiple systems.

  4. @noelboss noelboss revised this gist Mar 21, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion post-receive
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ do
    if [[ $ref = refs/heads/$BRANCH ]];
    then
    echo "Ref $ref received. Deploying ${BRANCH} branch to production..."
    git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f
    git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f $BRANCH
    else
    echo "Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server."
    fi
  5. @noelboss noelboss revised this gist Mar 21, 2019. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions git-deployment.md
    Original file line number Diff line number Diff line change
    @@ -6,8 +6,7 @@ tutorial.
    ## How it works

    You are developing in a working-copy on your local machine, lets say on the master branch. Most of the time, people would push code to a remote
    server like github.com or gitlab.com and pull or export it to a production server. Or you use a service like my [Deepl.io](https://deepl.io) to
    act upon a Web-Hook that's triggered that service.
    server like github.com or gitlab.com and pull or export it to a production server. Or you use a service like [deepl.io](https://deepl.io.noelboss.com) to act upon a Web-Hook that's triggered that service.

    But here, we add a "bare" git repository that we create on the production server and pusblish our branch (f.e. master) directly to that
    server. This repository acts upon the push event using a 'git-hook' to move the files into a deployment directory on your server. No need for a midle man.
  6. @noelboss noelboss revised this gist Mar 21, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions post-receive
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    #!/bin/bash
    TRAGET="/home/webuser/deploy-folder"
    TARGET="/home/webuser/deploy-folder"
    GIT_DIR="/home/webuser/www.git"
    BRANCH="master"

    @@ -9,7 +9,7 @@ do
    if [[ $ref = refs/heads/$BRANCH ]];
    then
    echo "Ref $ref received. Deploying ${BRANCH} branch to production..."
    git --work-tree=$TRAGET --git-dir=$GIT_DIR checkout -f
    git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f
    else
    echo "Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server."
    fi
  7. @noelboss noelboss revised this gist Jun 23, 2016. 2 changed files with 6 additions and 8 deletions.
    2 changes: 1 addition & 1 deletion git-deployment.md
    Original file line number Diff line number Diff line change
    @@ -51,7 +51,7 @@ This scrtipt is executed when the push from the local machine has been completed
    if [[ $ref =~ .*/master$ ]];
    then
    echo "Master ref received. Deploying master branch to production..."
    git --work-tree=~/deploy-folder/ --git-dir=~/project.git/ checkout -f
    git --work-tree=/home/webuser/deploy-folder/ --git-dir=/home/webuser/project.git/ checkout -f
    else
    echo "Ref $ref successfully received. Doing nothing: only the master branch may be deployed on this server."
    fi
    12 changes: 5 additions & 7 deletions post-receive
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,15 @@
    #!/bin/bash
    cd ~/
    PATH=$(pwd)
    REPO="$PATH/project.git"
    TRAGET="$PATH/deploy-folder"
    BRANCH="live"
    TRAGET="/home/webuser/deploy-folder"
    GIT_DIR="/home/webuser/www.git"
    BRANCH="master"

    while read oldrev newrev ref
    do
    # only checking out the master (or whatever branch you would like to deploy)
    if [[ $ref =~ .*/"${BRANCH}"$ ]];
    if [[ $ref = refs/heads/$BRANCH ]];
    then
    echo "Ref $ref received. Deploying ${BRANCH} branch to production..."
    git --work-tree=${TRAGET} --git-dir=${REPO} checkout -f
    git --work-tree=$TRAGET --git-dir=$GIT_DIR checkout -f
    else
    echo "Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server."
    fi
  8. @noelboss noelboss revised this gist Jun 23, 2016. 1 changed file with 9 additions and 9 deletions.
    18 changes: 9 additions & 9 deletions post-receive
    Original file line number Diff line number Diff line change
    @@ -1,18 +1,18 @@
    #!/bin/bash
    BRANCH ="deploy/live"
    REPO ="~/www.git"
    TRAGET ="~/testdeploy"
    LOG ="~/logs/deploy-master.log"
    NOW =$(date +"%Y-%m-%d-%H:%M:%S")
    cd ~/
    PATH=$(pwd)
    REPO="$PATH/project.git"
    TRAGET="$PATH/deploy-folder"
    BRANCH="live"

    while read oldrev newrev ref
    do
    # only checking out the master (or whatever branch you would like to deploy)
    if [[ $ref =~ .*/"$BRANCH"$ ]];
    if [[ $ref =~ .*/"${BRANCH}"$ ]];
    then
    echo "Ref $ref received. Deploying $BRANCH branch to production..."
    git --work-tree=$TRAGET --git-dir=$REPO checkout -f
    echo "Ref $ref received. Deploying ${BRANCH} branch to production..."
    git --work-tree=${TRAGET} --git-dir=${REPO} checkout -f
    else
    echo "Ref $ref successfully received. Doing nothing: only the $BRANCH branch may be deployed on this server."
    echo "Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server."
    fi
    done
  9. @noelboss noelboss revised this gist Jun 23, 2016. 1 changed file with 11 additions and 11 deletions.
    22 changes: 11 additions & 11 deletions post-receive
    Original file line number Diff line number Diff line change
    @@ -1,18 +1,18 @@
    #!/bin/bash
    BRANCH ="master"
    REPO ="~/project.git"
    TRAGET ="~/deploy-folder"
    BRANCH ="deploy/live"
    REPO ="~/www.git"
    TRAGET ="~/testdeploy"
    LOG ="~/logs/deploy-master.log"
    NOW =$(date +"%Y-%m-%d-%H:%M:%S")

    while read oldrev newrev ref
    do
    # only checking out the master (or whatever branch you would like to deploy)
    if [[ $ref =~ .*/${BRANCH}$ ]];
    then
    echo "Ref ${ref} received. Deploying ${BRANCH} branch to production..."
    git --work-tree=$TRAGET --git-dir=$REPO checkout -f
    else
    echo "Ref $ref successfully received. Doing nothing: only the ${BRANCH} branch may be deployed on this server."
    fi
    # only checking out the master (or whatever branch you would like to deploy)
    if [[ $ref =~ .*/"$BRANCH"$ ]];
    then
    echo "Ref $ref received. Deploying $BRANCH branch to production..."
    git --work-tree=$TRAGET --git-dir=$REPO checkout -f
    else
    echo "Ref $ref successfully received. Doing nothing: only the $BRANCH branch may be deployed on this server."
    fi
    done
  10. @noelboss noelboss revised this gist Jun 23, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion git-deployment.md
    Original file line number Diff line number Diff line change
    @@ -57,7 +57,7 @@ This scrtipt is executed when the push from the local machine has been completed
    fi
    done

    Download [post-receive](post-receive) for an improved version
    Download [post-receive](#file-post-receive) for an improved version
    ## 5. Add remote-repository localy
    Now we add the this bare repository to your local system as a remote. Where "production" is the name you want to give the remote. This also be called "staging" or "live" or "test" etc if you want to deploy to a different system or multiple systems.

  11. @noelboss noelboss revised this gist Jun 23, 2016. 2 changed files with 19 additions and 0 deletions.
    1 change: 1 addition & 0 deletions git-deployment.md
    Original file line number Diff line number Diff line change
    @@ -57,6 +57,7 @@ This scrtipt is executed when the push from the local machine has been completed
    fi
    done

    Download [post-receive](post-receive) for an improved version
    ## 5. Add remote-repository localy
    Now we add the this bare repository to your local system as a remote. Where "production" is the name you want to give the remote. This also be called "staging" or "live" or "test" etc if you want to deploy to a different system or multiple systems.

    18 changes: 18 additions & 0 deletions post-receive
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    #!/bin/bash
    BRANCH ="master"
    REPO ="~/project.git"
    TRAGET ="~/deploy-folder"
    LOG ="~/logs/deploy-master.log"
    NOW =$(date +"%Y-%m-%d-%H:%M:%S")

    while read oldrev newrev ref
    do
    # only checking out the master (or whatever branch you would like to deploy)
    if [[ $ref =~ .*/${BRANCH}$ ]];
    then
    echo "Ref ${ref} received. Deploying ${BRANCH} branch to production..."
    git --work-tree=$TRAGET --git-dir=$REPO checkout -f
    else
    echo "Ref $ref successfully received. Doing nothing: only the ${BRANCH} branch may be deployed on this server."
    fi
    done
  12. @noelboss noelboss revised this gist Jun 23, 2016. 1 changed file with 21 additions and 21 deletions.
    42 changes: 21 additions & 21 deletions git-deployment.md
    Original file line number Diff line number Diff line change
    @@ -33,41 +33,41 @@ Nuf said. I asume we are working on master – but you could work on any branch.
    ## 2. Create a folder to deploy to
    ssh into your prodctionserver:

    $ ssh [email protected]
    $ mkdir ~/deploy-folder

    $ ssh [email protected]
    $ mkdir ~/deploy-folder
    ## 3. Add a bare repository on the productions server
    Now we create a "bare" repository – one that does not contain the working copy files. It basicaly is the content of the .git repository folder in a normal working copy. Name it whatever you like, you can also ommit the .git part from project.git or leave it to create the repository in an exisiting empty folder:

    $ git init --bare ~/project.git
    $ git init --bare ~/project.git

    ## 4. Add the post-receive hook
    This scrtipt is executed when the push from the local machine has been completed and moves the files into place. It recides in project.git/hooks/ and is named 'post-receive'. You can use vim to edit and create it. The script does check if the correct branch is pushed (not deploying a develop branch for example) and

    #!/bin/bash
    while read oldrev newrev ref
    do
    # only checking out the master (or whatever branch you would like to deploy)
    if [[ $ref =~ .*/master$ ]];
    then
    echo "Master ref received. Deploying master branch to production..."
    git --work-tree=~/deploy-folder/ --git-dir=~/project.git/ checkout -f
    else
    echo "Ref $ref successfully received. Doing nothing: only the master branch may be deployed on this server."
    fi
    done
    #!/bin/bash
    while read oldrev newrev ref
    do
    # only checking out the master (or whatever branch you would like to deploy)
    if [[ $ref =~ .*/master$ ]];
    then
    echo "Master ref received. Deploying master branch to production..."
    git --work-tree=~/deploy-folder/ --git-dir=~/project.git/ checkout -f
    else
    echo "Ref $ref successfully received. Doing nothing: only the master branch may be deployed on this server."
    fi
    done

    ## 5. Add remote-repository localy
    Now we add the this bare repository to your local system as a remote. Where "production" is the name you want to give the remote. This also be called "staging" or "live" or "test" etc if you want to deploy to a different system or multiple systems.

    $ cd ~/path/to/working-copy/
    $ git remote add production [email protected]:project.git

    $ cd ~/path/to/working-copy/
    $ git remote add production [email protected]:project.git
    Make sure "project.git" coresponds to the name you gave in step 3. If you are using Tower or a similar App, you will see the newly added remote in your sidebar under "Remotes" (make sure it's not collapsed).

    ## 6. Push to the production server
    Now you can push the master branch to the production server:

    $ git push production master
    $ git push production master

    If you are using tower, you can drag&drop the master branch onto the new production remote. That's it. Have questions, improvements?
  13. @noelboss noelboss revised this gist Jun 23, 2016. No changes.
  14. @noelboss noelboss revised this gist Jun 23, 2016. No changes.
  15. @noelboss noelboss revised this gist Jun 23, 2016. 1 changed file with 39 additions and 5 deletions.
    44 changes: 39 additions & 5 deletions git-deployment.md
    Original file line number Diff line number Diff line change
    @@ -16,9 +16,9 @@ This creates a scenario where there is no middle man, high security with encrypt
    and high flexibility tue to the use of .sh scripts for the deployment.

    # Prerequisit
    1. Have a local working-working copy ready
    2. Have SSH Access to your server
    3. Optonaly configure your server for access with ssh-key (google it)
    1. Know how to use GIT, Terminal etc.
    2. Have a local working-working copy ready
    2. Have SSH access to your server using private/public key

    # Todos
    1. Create a folder to deploy to on production server (i.e. your httpds folder)
    @@ -34,6 +34,40 @@ Nuf said. I asume we are working on master – but you could work on any branch.
    ssh into your prodctionserver:

    $ ssh [email protected]
    $ mkdir deploy
    $ mkdir ~/deploy-folder


    ## 3. Add a bare repository on the productions server
    Now we create a "bare" repository – one that does not contain the working copy files. It basicaly is the content of the .git repository folder in a normal working copy. Name it whatever you like, you can also ommit the .git part from project.git or leave it to create the repository in an exisiting empty folder:

    $ git init --bare ~/project.git

    ## 4. Add the post-receive hook
    This scrtipt is executed when the push from the local machine has been completed and moves the files into place. It recides in project.git/hooks/ and is named 'post-receive'. You can use vim to edit and create it. The script does check if the correct branch is pushed (not deploying a develop branch for example) and

    #!/bin/bash
    while read oldrev newrev ref
    do
    # only checking out the master (or whatever branch you would like to deploy)
    if [[ $ref =~ .*/master$ ]];
    then
    echo "Master ref received. Deploying master branch to production..."
    git --work-tree=~/deploy-folder/ --git-dir=~/project.git/ checkout -f
    else
    echo "Ref $ref successfully received. Doing nothing: only the master branch may be deployed on this server."
    fi
    done

    ## 5. Add remote-repository localy
    Now we add the this bare repository to your local system as a remote. Where "production" is the name you want to give the remote. This also be called "staging" or "live" or "test" etc if you want to deploy to a different system or multiple systems.

    $ cd ~/path/to/working-copy/
    $ git remote add production [email protected]:project.git

    Make sure "project.git" coresponds to the name you gave in step 3. If you are using Tower or a similar App, you will see the newly added remote in your sidebar under "Remotes" (make sure it's not collapsed).

    ## 6. Push to the production server
    Now you can push the master branch to the production server:

    $ git push production master

    If you are using tower, you can drag&drop the master branch onto the new production remote. That's it. Have questions, improvements?
  16. @noelboss noelboss revised this gist Jun 23, 2016. 1 changed file with 20 additions and 1 deletion.
    21 changes: 20 additions & 1 deletion git-deployment.md
    Original file line number Diff line number Diff line change
    @@ -15,6 +15,25 @@ server. This repository acts upon the push event using a 'git-hook' to move the
    This creates a scenario where there is no middle man, high security with encrypted communication (using ssh keys, only authorized people get access to the server)
    and high flexibility tue to the use of .sh scripts for the deployment.

    # Todos
    # Prerequisit
    1. Have a local working-working copy ready
    2. Have SSH Access to your server
    3. Optonaly configure your server for access with ssh-key (google it)

    # Todos
    1. Create a folder to deploy to on production server (i.e. your httpds folder)
    2. Add a bare repository on the productions server
    4. Add the post-receive hook script to the bare repository (and make it executable)
    5. Add the remote-repository resided on the production server to your local repository
    6. Push to the production server, relax.

    ## 1. Have a local working-working copy ready
    Nuf said. I asume we are working on master – but you could work on any branch.

    ## 2. Create a folder to deploy to
    ssh into your prodctionserver:

    $ ssh [email protected]
    $ mkdir deploy


  17. @noelboss noelboss created this gist Jun 23, 2016.
    20 changes: 20 additions & 0 deletions git-deployment.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    # Simple automated GIT Deployment using GIT Hooks

    Here are the simple steps needed to create a deployment from your lokal GIT repository to a server based on [this](https://www.digitalocean.com/community/tutorials/how-to-use-git-hooks-to-automate-development-and-deployment-tasks) in-depth
    tutorial.

    ## How it works

    You are developing in a working-copy on your local machine, lets say on the master branch. Most of the time, people would push code to a remote
    server like github.com or gitlab.com and pull or export it to a production server. Or you use a service like my [Deepl.io](https://deepl.io) to
    act upon a Web-Hook that's triggered that service.

    But here, we add a "bare" git repository that we create on the production server and pusblish our branch (f.e. master) directly to that
    server. This repository acts upon the push event using a 'git-hook' to move the files into a deployment directory on your server. No need for a midle man.

    This creates a scenario where there is no middle man, high security with encrypted communication (using ssh keys, only authorized people get access to the server)
    and high flexibility tue to the use of .sh scripts for the deployment.

    # Todos
    1. Have a local working-working copy ready
    2. Add a bare repository on the productions server