Github Actions is an automated process control tool provided by Github. Package, test, log in to a remote server, publish to a third party, and so on.

GitHub Actions Documentation

The Actions of grammar

Context and expression syntax for GitHub operations

The main use of a few, directly see the following example basic can understand.

uses

The use of uses is critical. It is the provider of various actions, such as the EasingThemes /[email protected] library for SSH projects to remote servers, Install node.js environment actions/[email protected] etc.

More third-party actions can be searched at GitHub Marketplace · Actions to improve your workflow · GitHub.

Server generation key

The server needs to generate a pair of keys for Github to connect to.

Generate the key

[root@host ~]$ ssh-keygen
Copy the code

Install the public key

[root@host ~]$ cd .ssh
[root@host .ssh]$ cat id_rsa.pub >> authorized_keys
Copy the code

permissions

[root@host .ssh]$ chmod 600 authorized_keys
[root@host .ssh]$ chmod 700 ~/.ssh
Copy the code

Enabling Key Login

vi /etc/ssh/sshd_config
Copy the code

Make sure the Settings are as follows:

RSAAuthentication yes
PubkeyAuthentication yes

PermitRootLogin yes
Copy the code

Restarting the SSH Service

[root@host .ssh]$ service sshd restart
Copy the code

Copy the private key (including BEGIN and END) and add it to Github Settings => secrets

[root@host .ssh]$ cat id_rsa
Copy the code

Package and deploy to the server

name: Deploy To Aliyun CI

Trigger the process after the master branch push operation
on: 
  push:
    branches: 
      - master

jobs:
  build:
    
    runs-on: ubuntu-latest

    steps:
    An Actions that gives your workflow access to your repository
    - name: Checkout
      uses: actions/checkout@v2

    # Specify environment Node.js v12
    - name: Use Node.js 12
      uses: The actions/[email protected]
      with:
        node-version: '12.x'
    
    # Pack on GitHub server
    - name: Build
      run: | yarn yarn build      env:
        CI: true

    Pull the package result to the server
    - name: Deploy to Server
      uses: Easingthemes/[email protected]
      env:
          # Settings => secrets Add server private key
          SSH_PRIVATE_KEY: The ${{ secrets.ALIYUN_TOKEN }}
          # server address, can be directly written, can also be created in secrets (that is, private not public)
          REMOTE_HOST: The ${{ secrets.REMOTE_HOST }}
          The default SSH port for the server is 22
          REMOTE_PORT: The ${{ secrets.REMOTE_PORT }}
          # Login account
          REMOTE_USER: The ${{ secrets.REMOTE_USER }}
          The file 'build' that needs to be sent to the remote server is packaged for React
          SOURCE: 'build/'
          Server storage location
          TARGET: '/www/Client'
          The # rsync parameter defaults to the following
          ARGS: '-avzr --delete'
Copy the code

Remote server pull and then pack

The main reason for pulling and packing node_modules to the server is that it takes too long to pack node_modules on Github each time it is downloaded, and node_modules will be cached after the first packing, which will be faster the next time.

You can use git pull to forcibly overwrite the local directory, or use ssh-deploy to pull master from the current commit directory.

name: Deploy To Aliyun CI

on: 
  push:
    branches: 
      - master

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - name: SSH And Build
      uses: appleboy/ssh-action@master
      with:
        host: The ${{ secrets.REMOTE_HOST }}
        username: The ${{ secrets.REMOTE_USER }}
        key: The ${{ secrets.ALIYUN_TOKEN }}
        port: The ${{ secrets.REMOTE_PORT }}
        # git fetch and reset
        script: | cd /www/Client git fetch --all git reset --hard origin/master yarn yarn buildCopy the code