Skip to content

GitHub Actions: Access Server through GitHub Actions

Executes commands on a remote server via SSH from GitHub Actions. Useful for tasks like pulling the latest code, running scripts, or managing deployments.

name: Access Server via GitHub Actions
on:
push:
branches:
- main
jobs:
build-and-deploy:
runs-on: ubuntu-22.04
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: SSH to server and running some commands.
uses: appleboy/ssh-action@v0.1.10
with:
host: ${{ secrets.SSH_HOST }}
username: ${{ secrets.SSH_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
port: ${{ secrets.SSH_PORT }}
script: |
cd /var/www/my-project && git pull origin main
# Add another commands here

Requirements

  • Server must allow SSH connections with a valid key.
  • Set SSH_HOST, SSH_USER, SSH_PRIVATE_KEY, and SSH_PORT in GitHub Secrets.
  • Use appleboy/ssh-action to execute commands on the server.