Skip to content

Instantly share code, notes, and snippets.

@albertgeorge56
Last active June 9, 2023 15:29
Show Gist options
  • Save albertgeorge56/2068e7be4f44167a8904adb5d1e8e246 to your computer and use it in GitHub Desktop.
Save albertgeorge56/2068e7be4f44167a8904adb5d1e8e246 to your computer and use it in GitHub Desktop.
Github Action Setup

To automatically pull a Git repository using appleboy/ssh-action on a server with SSH access, you can follow these steps:

  1. Ensure that the server has SSH access and the necessary permissions to execute Git commands.

  2. Create an SSH key pair on your local machine (if you haven't already) by running the following command in your terminal:

    ssh-keygen -t rsa -b 4096 -C "your-email@example.com"
  3. Copy the public key (id_rsa.pub) generated in the previous step.

  4. On the server, add the public key to the ~/.ssh/authorized_keys file. You can do this by appending the contents of the public key to the file using a text editor or by using the ssh-copy-id command.

  5. On your GitHub repository page, go to "Settings" and then "Secrets".

  6. Create two new secrets: SERVER_SSH_HOST and SERVER_SSH_PRIVATE_KEY.

    • Set the SERVER_SSH_HOST secret to the IP address or hostname of your server.

    • Set the SERVER_SSH_PRIVATE_KEY secret to the private key (id_rsa) generated in step 2.

  7. Create a new file named .github/workflows/git-pull.yml in the root of your repository.

  8. Open the git-pull.yml file and add the following content:

name: Automatic Git Pull

on:
  push:
    branches:
      - main

jobs:
  git_pull:
    runs-on: ubuntu-latest

    steps:
      - name: Git Pull on server
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.SERVER_SSH_HOST }}
          username: your-username
          key: ${{ secrets.SERVER_SSH_PRIVATE_KEY }}
          use_insecure_cipher: true
          script: |
            cd /path/to/your/repository
            git pull
  1. Replace your-username with the username for your SSH access on the server.

  2. Replace /path/to/your/repository with the actual path to your Git repository on the server.

  3. Commit and push the changes to your repository:

    git add .
    git commit -m "Add Git Pull workflow"
    git push

The workflow will now automatically trigger a Git pull on the server whenever a push occurs on the main branch. It uses the appleboy/ssh-action to establish an SSH connection to the server, navigate to the repository directory, and perform the Git pull operation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment