To automatically pull a Git repository using appleboy/ssh-action
on a server with SSH access, you can follow these steps:
-
Ensure that the server has SSH access and the necessary permissions to execute Git commands.
-
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 "[email protected]"
-
Copy the public key (
id_rsa.pub
) generated in the previous step. -
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 thessh-copy-id
command. -
On your GitHub repository page, go to "Settings" and then "Secrets".
-
Create two new secrets:
SERVER_SSH_HOST
andSERVER_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.
-
-
Create a new file named
.github/workflows/git-pull.yml
in the root of your repository. -
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
-
Replace
your-username
with the username for your SSH access on the server. -
Replace
/path/to/your/repository
with the actual path to your Git repository on the server. -
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.