2024-06-15

Git Deployment on remote machine.

Git Deployment Workflow Manual. 

This tutorial is tested on two independent systems - Raspberry Pi with SSL and Git, and using Web Terminal in Cpanel. Available as LaTeX formatted document in pdf file here -https://jmp.sh/J4wNpSQJ

This manual outlines the steps to set up a workflow where you can work in a development folder and push changes to a production folder using Git. In this example, the development folder is /home/user/dev_folder and the production folder is /home/user/prod_folder.

Prerequisites

  • SSH access to the server
  • Git installed on the server

Step-by-Step Guide

1. Initialize Git in the Development Folder

Navigate to the development folder and initialize a Git repository.

cd /home/user/dev_folder
git init

2. Add and Commit the Existing Content

Add the existing content to the repository and commit it.

git add .
git commit -m "Initial commit"

3. Initialize Git in the Production Folder

Navigate to the production folder and initialize a bare Git repository.

cd /home/user/prod_folder
git init --bare

4. Configure Git User Information

Check if Git user information is configured. If not, set your Git user name and email globally.

Check if Git user is configured:

git config --global user.name
git config --global user.email

If the above commands return nothing, configure the Git user:

Globally:

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

5. Add the Production Folder as a Remote Repository in the Development Folder

Go back to the development folder and add the production folder as a remote repository.

cd /home/user/dev_folder
git remote add production /home/user/prod_folder

6. Create a Post-Receive Hook in the Production Repository

A post-receive hook in the production repository will automatically check out the latest code when you push to it.

Create the hook by creating a file named post-receive in the hooks directory of the production repository.

cd /home/user/prod_folder/hooks
nano post-receive

Add the following content to the post-receive file:

#!/bin/sh
echo "Running post-receive hook" >> /home/user/prod_folder/deploy.log
GIT_WORK_TREE=/home/user/prod_folder git checkout -f >> /home/user/prod_folder/deploy.log 2>&1
echo "Completed post-receive hook" >> /home/user/prod_folder/deploy.log

7. Make the Post-Receive Hook Executable

Ensure the post-receive hook is executable.

chmod +x /home/user/prod_folder/hooks/post-receive

8. Push the Content from the Development Folder to the Production Folder

Finally, push your work to the production folder.

Note: If you encounter an error "src refspec master does not match any", it means that you haven't created any commits on the master branch yet. Make sure you have committed at least one change.

cd /home/user/dev_folder
git push production master

If the error persists, you may not have any branch named master. List your branches with:

git branch

If there is no branch named master, you can push your current branch instead. For example, if you are on the main branch:

git push production main:master

Troubleshooting

Check Post-Receive Hook Permissions and Path

Ensure the post-receive hook has the correct permissions and paths.

ls -l /home/user/prod_folder/hooks/post-receive

Verify the Work Tree Path

Ensure the work tree path is correct and accessible.

ls /home/user/prod_folder

Manually Test the Post-Receive Hook

Simulate the execution of the post-receive hook to ensure it works as expected.

cd /home/user/prod_folder
./hooks/post-receive

Check the deploy.log file for output:

cat /home/user/prod_folder/deploy.log

Summary of Commands

  1. Initialize Git in the development folder:
    cd /home/user/dev_folder
    git init
  2. Add and commit the content:
    git add .
    git commit -m "Initial commit"
  3. Initialize a bare Git repository in the production folder:
    cd /home/user/prod_folder
    git init --bare
  4. Configure Git user information if needed:
    git config --global user.name "Your Name"
    git config --global user.email "your.email@example.com"
  5. Add the production repository as a remote in the development folder:
    cd /home/user/dev_folder
    git remote add production /home/user/prod_folder
  6. Create and configure the post-receive hook:
    cd /home/user/prod_folder/hooks
    nano post-receive

    Add the following content to post-receive:

    #!/bin/sh
    echo "Running post-receive hook" >> /home/user/prod_folder/deploy.log
    GIT_WORK_TREE=/home/user/prod_folder git checkout -f >> /home/user/prod_folder/deploy.log 2>&1
    echo "Completed post-receive hook" >> /home/user/prod_folder/deploy.log

    Make the hook executable:

    chmod +x /home/user/prod_folder/hooks/post-receive
  7. Push changes to the production folder:
    cd /home/user/dev_folder
    git push production master

If the error persists, push your current branch to master:

git push production main:master

By following these steps, you can effectively manage and deploy your code from a development environment to a production environment using Git.

Nav komentāru:

Ierakstīt komentāru