The core idea is that the master branch push triggers an Action to perform shell deployment on the SSH cloud server

1. The cloud server generates SSH public and private keys

Ssh-keygen -t rsa -b 4096 -c [email protected] Generates two files (private key and public key) in ~/. SSH/by default: Id_rsa, id_rsa.pub Copies the contents of the public key into the ~/. SSH /authorized_keys file

2. Create shell scripts on the cloud server for deployment

The example uses the PM2 deployment Node.js program, and you can write the shell according to your own situation

cd /your_project_path
git pull origin master
npm install
pm2 restart 0
Copy the code

3. Set Github Secrets variable

Project page Settings->Secrets->New secret Create the following three variables: REMOTE_HOST: cloud server IP address REMOTE_USER: SSH login user SERVER_SSH_KEY: id_RSA content in Step 1 (private key)

4. Create an Action

Click Action to create a new Workflow file.

# This is a basic workflow to help you get started with Actions name: Deploy # Controls when the action will run. Triggers the workflow on push or pull request # events but only for the master branch on: push: branches: [ master ] pull_request: branches: [ master ] # A workflow run is made up of one or more jobs that can run sequentially or in parallel jobs: # This workflow contains a single job called "build" build: # The type of runner that the job will run on runs-on: ubuntu-latest # Steps represent a sequence of tasks that will be executed as part of the job steps: # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it - name: Run a command remotely uses: Docker ://evaneos/ssh-action:0.1.0 with: hosts: ${{secrets.remote_host}} user: ${{secrets.remote_user}} private_key: ${{ secrets.SERVER_SSH_KEY }} commands: sh /your_path/your_shell.shCopy the code

After the master branch pushes, you can click Action to view workflows