Git Deployment Workflow Manual.
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 init2. 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 --bare4. 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.emailIf 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_folder6. 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-receiveAdd 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.log7. Make the Post-Receive Hook Executable
Ensure the post-receive hook is executable.
chmod +x /home/user/prod_folder/hooks/post-receive8. 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 masterIf the error persists, you may not have any branch named master. List your branches with:
git branchIf 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:masterTroubleshooting
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-receiveVerify the Work Tree Path
Ensure the work tree path is correct and accessible.
ls /home/user/prod_folderManually 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-receiveCheck the deploy.log file for output:
cat /home/user/prod_folder/deploy.logSummary of Commands
- Initialize Git in the development folder:
cd /home/user/dev_folder git init - Add and commit the content:
git add . git commit -m "Initial commit" - Initialize a bare Git repository in the production folder:
cd /home/user/prod_folder git init --bare - Configure Git user information if needed:
git config --global user.name "Your Name" git config --global user.email "your.email@example.com" - Add the production repository as a remote in the development folder:
cd /home/user/dev_folder git remote add production /home/user/prod_folder - Create and configure the post-receive hook:
cd /home/user/prod_folder/hooks nano post-receiveAdd 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.logMake the hook executable:
chmod +x /home/user/prod_folder/hooks/post-receive - 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:masterBy 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