2 minutes
How to automate a deploy with GitHub actions via SSH
Introduction
GitHub Actions is an API for cause and effect on GitHub: orchestrate any workflow, based on any event, while GitHub manages the execution, provides rich feedback, and secures every step along the way.
In this article, we will be exploring a hands-on approach to managing your CD processes using GitHub Actions via SSH.
The workflow:
- Connect to VPS via SSH
- Move to project directory
git pull
the new changes- execute any necessary command
Prerequisites
- A GitHub account. If you donβt have one, you can sign up here
- A server with SSH access
- Basic knowledge of writing valid YAML
- Basic knowledge of GitHub and Git
Configuring workflows
we should create a yml file on .github/workflows/
. For example .github/workflows/ci.yml
and add this code to the file:
name: CI
on: [push]
jobs:
deploy:
if: github.ref == 'refs/heads/master'
runs-on: [ubuntu-latest]
steps:
- uses: actions/checkout@v1
- name: Push to server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.SERVER_IP }}
username: ${{ secrets.SERVER_USERNAME }}
key: ${{ secrets.KEY }}
passphrase: ${{ secrets.PASSPHRASE }}
script: cd ${{ secrets.PROJECT_PATH }} && git pull
After add this file go to Settings -> Secrets
and add secrets SERVER_IP
, SERVER_USERNAME
, KEY
, PASSPHRASE
, PROJECT_PATH
note: you can use
password
insted ofkeys
just you need to replace thekey and passphrase
line withpassword
in the workflow filepassword: ${{ secrets.PASSWORD }}
and add thepassword
to secrets
I use the GitHub secrets to keep important information hidden
also you can add more commands to the script line as you need
the next time we push to the master branch, it will automatically be deployed to our server.