GitHub Actions automatic deployment + timing Baidu link push

foreword

Recently, I built a static website with VuePress. Because it is purely static, it is very troublesome to repackage and upload the article every time I modify it. Although the author of the vuepress-theme-vdoing theme provides a tutorial article on the automatic deployment of GitHub Actions, it is too simple and was published in 19 years. .

1. Create a GitHub warehouse (private warehouses are also available)

insert image description here

2. Set server address and SSH private key

Server address acquisition: Needless to say.
SSH private key: Obtain it yourself at the console of the server service provider. For example, I am a lightweight server of Alibaba Cloud
insert image description here

Set the server address and SSH private key variables in GitHub:

insert image description here
insert image description here
insert image description here

insert image description here

3. Create necessary files for automated deployment

3.1 Create a directory file

  1. Create a directory in the project root directory:.github
  2. .githubCreate workflowsdirectory in directory
  3. Create a yml file in workflowsthe directory, the name can be arbitrary. my name isci.yml

insert image description here

3.2 ci.yml configuration (super detailed comments on each line)

# 工作流名称,可以根据你的需求进行命名
name: CI
# 这个部分定义了触发工作流程的事件,即在什么情况下工作流程会被触发。在这里,工作流会在master分支有push事件时被触发。
on:
  push:
    branches:
      - master
# 这是工作流程的主要部分,包含了一个或多个工作(jobs),每个工作都是一系列步骤的组合。
jobs:
  build:
    # 指定工作在哪种虚拟机环境中运行,这里是在ubuntu-latest(最新版本的Ubuntu)中运行。(注意:这里指的是GitHub托管的虚拟机,与你实际服务器没有直接关系)
    runs-on: ubuntu-latest
    # 这部分允许你定义一些矩阵策略,以在不同的条件下运行不同的步骤。在这里,使用了一个node-version矩阵,具体的版本是18.x。
    strategy:
      matrix:
      	# 一定要注意!这里一定要是自己项目所使用的node版本,否则在自动打包时会出错。
        node-version: [ 18.x ]
    # 这是工作的实际步骤。
    steps:
      # 步骤1 这是步骤的名称,表示将检出(克隆)代码库。
      - name: Checkout # 步骤1
        # 使用的动作。这里使用了actions/checkout@v1,这是一个官方的GitHub Actions动作,用于检出代码库中的代码。官方actions库:https://github.com/actions
        uses: actions/checkout@v1
      # 步骤2 这是步骤的名称,表示使用特定的Node.js版本。
      - name: Use Node.js ${
    
    {
    
     matrix.node-version }}
        # 使用的动作。这里使用了actions/setup-node@v1,用于安装指定版本的Node.js。
        uses: actions/setup-node@v1 # 作用:安装nodejs
        # 这里是动作的输入参数,指定了要安装的Node.js版本,来自于上面定义的矩阵。
        with:
          node-version: ${
    
    {
    
     matrix.node-version }} # 版本
      # 步骤3 安装依赖
      - name: npm install
        run: npm install
      # 步骤4 打包
      - name: build
        run: npm run build
      # 步骤5 部署
      - name: deploy
        # 使用动作:wlixcc/[email protected]
        uses: wlixcc/SFTP-Deploy-[email protected]
        with:
          # 这里是动作的输入参数,指定了要部署的服务器信息。
          username: 'root' #ssh user name
          # 服务器地址,我们在项目的secrets中配置的SERVER_HOST
          server: ${
    
    {
    
     secrets.SERVER_HOST }}
          # 服务器私钥,我们在项目的secrets中配置的PRIVATE_KEY
          ssh_private_key: ${
    
    {
    
     secrets.PRIVATE_KEY }}
          # 项目打包后的文件路径(根据你们实际的打包路径进行修改)
          local_path: './docs/.vuepress/dist/*'
          # 服务器上的项目路径(根据你们实际的项目路径进行修改)
          remote_path: '/xxx/xxx/xxx'
      # 步骤6 清理打包文件(根据你们实际的打包路径进行修改)
      - name: clean
        run: rm -rf ./docs/.vuepress/dist

3.3 Submit new files to the GitHub repository

I used WebStorm to submit, and I can also directly order the submission.
insert image description here

After submitting, refresh and you will see:

insert image description here

4. Test automation deployment

The previous configuration process is over, now test the update code and submit it to the main branch of the warehouse to see if it will be triggered.

I modified some code randomly. Then submit:

insert image description here

ok, it has been triggered.

insert image description here
insert image description here

Click on a specific task in the left sidebar jobsto view specific logs:

insert image description here

Expand to view specific logs for each step:

insert image description here

If the task is completed without error, it will turn into a green checkmark icon:

insert image description here

Check if the server already has:

insert image description here
This is ok! !

5. Timing Baidu link push

.github/workflowsCreate a yml for Baidu push in the directory, I named it herebaiduPush.yml

insert image description here

Created in the project root directorybaiduPush.sh

insert image description here

yml configuration:

## 利用GitHub Actions每天定时给百度推送链接,提高收录率 ##
name: baiduPush
# 两种触发方式:一、push代码,二、每天国际标准时间23点(北京时间+8即早上7点)运行
on:
  push:
  schedule:
    # 每天国际标准时间23点(北京时间+8即早上7点)运行
    - cron: '0 23 * * *' # https://help.github.com/en/actions/automating-your-workflow-with-github-actions/events-that-trigger-workflows#scheduled-events-schedule
# on:
#   schedule:
#     - cron: '*/5 * * * *' # 每5分钟一次,测试用

jobs:
  bot:
    runs-on: ubuntu-latest # 运行环境为最新版的Ubuntu
    steps:
      - name: 'Checkout codes' # 步骤一,获取仓库代码
        uses: actions/checkout@v1
      - name: 'Run baiduPush' # 步骤二,执行sh命令文件
        run: npm install && npm run baiduPush # 运行目录是仓库根目录

baiduPush.sh script content:

#!/usr/bin/env sh

# 确保脚本抛出遇到的错误
set -e

# 百度链接推送
curl -H 'Content-Type:text/plain' --data-binary @urls.txt "http://data.zz.baidu.com/urls?site=ytxy.xyz&token=Oo5Ue3daxKJxkPid"

rm -rf urls.txt # 删除文件

Finally, submit the code to the GitHub warehouse

insert image description here

The test push is successful:

insert image description here

6. Note

Before or after automatic deployment, you need to configure Nginx yourself to allow normal access to the website!
Before or after automatic deployment, you need to configure Nginx yourself to allow normal access to the website!
The automated deployment here just uploads the packaged files to a certain folder on your server. You need to configure access yourself.

For Nginx or other configurations, please Baidu by yourself. This is too simple, so I won’t talk about it here anymore! ! !

Guess you like

Origin blog.csdn.net/weixin_52799373/article/details/132333626