Loading Now

How to push code from gitub to linux server

How to push code from gitub to linux server

Push code from gitub :

Pushing code from GitHub to a Linux server involves several steps, including setting up SSH access, cloning the repository, and pulling updates. Here’s a step-by-step guide:

Push code from gitub

1. Set Up SSH Access

Ensure you have SSH access to your Linux server.

  1. Generate SSH Key Pair (if you don’t have one): On your local machine, run:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

Add SSH Key to GitHub:

  • Copy the public key to your clipboard:
cat ~/.ssh/id_rsa.pub

.Go to GitHub, navigate to Settings > SSH and GPG keys > New SSH key, and paste your public key.

  • Copy SSH Key to Linux Server:
    • Use ssh-copy-id to copy your SSH key to the server:
ssh-copy-id user@your_server_ip

2. Clone the Repository on the Linux Server

  1. SSH into the Server:
ssh user@your_server_ip

2.Clone the Repository:

git clone git@github.com:your_username/your_repository.git

3. Pull Updates from GitHub to the Linux Server

  1. Navigate to the Repository Directory:
cd your_repository

Pull the Latest Changes:

git pull origin main

Replace main with the branch name if you’re using a different branch.

4. Automate Deployment (Optional)

You might want to automate the deployment process using hooks or continuous deployment tools.

Using Git Hooks

  1. Create a Post-Receive Hook:On your server, navigate to the hooks directory in your repository:
cd your_repository/.git/hooks

Create a post-receive Hook:

nano post-receive

Add the Following Script:

#!/bin/bash
GIT_WORK_TREE=/path/to/your/project git checkout -f

Replace /path/to/your/project with the path to your project directory.

  • Make the Hook Executable:
chmod +x post-receive

Using Continuous Deployment Tools

Tools like Jenkins, Travis CI, CircleCI, or GitHub Actions can automate deployment. For instance, with GitHub Actions:

  1. Create a Workflow File:In your repository, create a .github/workflows/deploy.yml file.
  2. Add Deployment Configuration:
name: Deploy to Server

on:
push:
branches:
- main

jobs:
deploy:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Deploy to Server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SERVER_IP }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
cd /path/to/your/project
git pull origin main

Add Secrets:

    • Go to your GitHub repository, navigate to Settings > Secrets and variables > Actions.
    • Add the following secrets:
      • SERVER_IP: Your server’s IP address.
      • SERVER_USER: Your server’s username.
      • SSH_PRIVATE_KEY: Your SSH private key.

By following these steps, you can push code from GitHub to a Linux server and even set up automated deployments.

If you have error while updating php updates in Linux server click hereÂ