This commit is contained in:
parent
b3e2e79c31
commit
8fcb8a475d
|
|
@ -27,7 +27,6 @@ jobs: # 工作流
|
|||
env: # 设置环境变量
|
||||
GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }} # toKen私密变量
|
||||
CODING_TOKEN: ${{ secrets.CODING_TOKEN }} # 腾讯云开发者平台(coding)私密token
|
||||
BAIDU_TONGJI: ${{ secrets.BAIDU_TONGJI}} # 百度统计
|
||||
run: npm install && npm run deploy
|
||||
|
||||
# - name: Build and Deploy # 步骤3 (只提交到github可以使用这个步骤)
|
||||
|
|
|
|||
|
|
@ -7,3 +7,6 @@ node_modules
|
|||
|
||||
# vuepress
|
||||
docs/.vuepress/dist
|
||||
|
||||
# 百度链接推送
|
||||
urls.txt
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
# Evan Blog —— web前端技术博客
|
||||
# Evan Blog —— 前端技术博客
|
||||
|
||||
## 简介
|
||||
|
||||
|
|
@ -7,7 +7,7 @@
|
|||
* **简洁至上**
|
||||
* 继承了[VuePress](https://v1.vuepress.vuejs.org/zh/guide/) 简洁至上的特性,以 Markdown 为中心的项目结构,让你专注于写作
|
||||
* **高自动化**
|
||||
* 项目结合了各种自动化技术,[自动生成侧边栏](https://github.com/xugaoyi/blog/issues/113),[CI持续集成部署](#4部署),[自动百度链接推送](#百度自动推送和统计),结合GitHub Actions开发的自动化[每天定时推送](https://github.com/xugaoyi/blog/blob/master/.github/workflows/baiduPush.yml)
|
||||
* 项目结合了各种自动化技术,[自动生成侧边栏](https://github.com/xugaoyi/blog/issues/113),[CI持续集成部署](https://xugaoyi.com/pages/6b9d359ec5aa5019/),[自动百度链接推送](#百度自动推送和统计),结合GitHub Actions开发的自动化[每天定时推送](https://xugaoyi.com/pages/f44d2f9ad04ab8d3/)
|
||||
* **SEO友好**
|
||||
* 让你的博客给更多人看到
|
||||
|
||||
|
|
@ -261,4 +261,4 @@ echo 'xugaoyi.com' > CNAME # // 域名替换成你的
|
|||
]
|
||||
```
|
||||
|
||||
> 如果你也需要百度统计功能,请把hm参数该为你自己的,请到<https://tongji.baidu.com>获取你自己的hm参数
|
||||
> 如果你也需要百度统计功能,请把hm参数改为你自己的,请到<https://tongji.baidu.com>获取你自己的hm参数
|
||||
|
|
@ -6,7 +6,6 @@ set -e
|
|||
node utils/baiduPush.js # 生成所有链接文件
|
||||
|
||||
# 百度链接推送
|
||||
# curl -H 'Content-Type:text/plain' --data-binary @urls.txt "http://data.zz.baidu.com/urls?site=evanblogweb.com&token=T5PEAzhGaPNbjQ2X"
|
||||
curl -H 'Content-Type:text/plain' --data-binary @urls.txt "http://data.zz.baidu.com/urls?site=https://xugaoyi.com&token=T5PEAzhGaPNbjQ2X"
|
||||
|
||||
rm -rf urls.txt # 灭迹
|
||||
|
|
@ -28,7 +28,7 @@ git push -f $githubUrl master:gh-pages # 推送到github
|
|||
# deploy to coding
|
||||
echo 'www.xugaoyi.com\nxugaoyi.com' > CNAME # 自定义域名
|
||||
if [ -z "$CODING_TOKEN" ]; then # -z 字符串 长度为0则为true;$CODING_TOKEN来自于github仓库`Settings/Secrets`设置的私密环境变量
|
||||
#codingUrl=git@git.dev.tencent.com:xugaoyi/xugaoyi.git
|
||||
# codingUrl=git@git.dev.tencent.com:xugaoyi/xugaoyi.git
|
||||
codingUrl=git@e.coding.net:xgy/xgy.git
|
||||
else
|
||||
# codingUrl=https://xugaoyi:${CODING_TOKEN}@git.dev.tencent.com/xugaoyi/xugaoyi.git
|
||||
|
|
|
|||
|
|
@ -3,11 +3,6 @@ const nav = require('./config/nav.js');
|
|||
const sidebar = require('./config/sidebar-auto.js');
|
||||
const logger = require('tracer').colorConsole();
|
||||
|
||||
const BAIDU_TONGJI = process.env.BAIDU_TONGJI || '';
|
||||
if (!BAIDU_TONGJI) {
|
||||
logger.warn('未配置百度统计参数,将无法使用百度统计');
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
title: 'Evan Blog', // 标题
|
||||
description: 'web前端技术博客,基于vuepress构建,专注web前端学习与总结。JavaScript,js,ES6,TypeScript,vue,python,css3,html5,Node,git,github等技术文章。', // 描述,以 <meta> 标签渲染到当前页面的 HTML 中
|
||||
|
|
@ -45,7 +40,7 @@ module.exports = {
|
|||
[
|
||||
'vuepress-plugin-baidu-tongji', // 百度统计,https://tongji.baidu.com
|
||||
{
|
||||
hm: BAIDU_TONGJI
|
||||
hm: '503f098e7e5b3a5b5d8c5fc2938af002'
|
||||
}
|
||||
],
|
||||
[
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
module.exports = [
|
||||
{text: '首页', link: '/'},
|
||||
|
||||
{text: '前端', link: '/pages/70d1485bb4e5754b/'},
|
||||
{text: '前端', link: '/pages/d039d42572a97b66/'},
|
||||
{text: '页面', link: '/pages/0a83b083bdf257cb/'},
|
||||
{text: '技术杂谈', link: '/pages/9a7ee40fc232253e/'},
|
||||
{text: '其他',
|
||||
|
|
|
|||
|
|
@ -2,11 +2,11 @@
|
|||
// 侧边栏自动生成
|
||||
module.exports = {
|
||||
|
||||
"/01.前端/": [{"title":"JavaScript","collapsable":false,"children":[["01.JavaScript/01.JavaScript中的名词概念.md","JavaScript中的名词概念"],["01.JavaScript/02.数据类型转换.md","数据类型转换"],["01.JavaScript/03.ES5面向对象.md","ES5面向对象"],["01.JavaScript/04.ES6面向对象.md","ES6面向对象"],["01.JavaScript/05.new命令原理.md","new命令原理"],["01.JavaScript/06.多种数组去重性能对比.md","多种数组去重性能对比"]]}],
|
||||
"/01.前端/": [{"title":"JavaScript","collapsable":false,"children":[["01.JavaScript/01.数据类型转换.md","数据类型转换"],["01.JavaScript/02.new命令原理.md","new命令原理"],["01.JavaScript/03.ES5面向对象.md","ES5面向对象"],["01.JavaScript/04.ES6面向对象.md","ES6面向对象"],["01.JavaScript/05.JavaScript中的名词概念.md","JavaScript中的名词概念"],["01.JavaScript/06.多种数组去重性能对比.md","多种数组去重性能对比"]]}],
|
||||
|
||||
"/02.页面/": [{"title":"html-css","collapsable":false,"children":[["01.html-css/01.flex布局语法.md","flex布局语法"],["01.html-css/02.flex布局案例-基础.md","flex布局案例-基础"],["01.html-css/03.flex布局案例-骰子.md","flex布局案例-骰子"],["01.html-css/04.flex布局案例-圣杯布局.md","flex布局案例-圣杯布局"],["01.html-css/05.flex布局案例-网格布局.md","flex布局案例-网格布局"],["01.html-css/06.flex布局案例-输入框布局.md","flex布局案例-输入框布局"],["01.html-css/07.CSS3之transform过渡.md","CSS3之transform过渡"],["01.html-css/08.CSS3之animation动画.md","CSS3之animation动画"]]}],
|
||||
|
||||
"/03.技术杂谈/": [{"title":"技术","collapsable":false,"children":[["01.技术/01.Git使用手册.md","Git使用手册"],["01.技术/02.Markdown使用教程.md","Markdown使用教程"],["01.技术/03.npm常用命令.md","npm常用命令"],["01.技术/04.yaml语言教程.md","yaml语言教程"]]},{"title":"GitHub","collapsable":false,"children":[["02.GitHub/01.GitHub高级搜索技巧.md","GitHub高级搜索技巧"],["02.GitHub/02.GitHub Actions 实现自动部署静态博客.md","GitHub Actions 实现自动部署静态博客"]]},{"title":"Nodejs","collapsable":false,"children":[["03.Nodejs/01.nodejs递归读取所有文件.md","nodejs递归读取所有文件"]]},{"title":"博客搭建","collapsable":false,"children":[["04.博客搭建/01.解决百度无法收录搭建在GitHub上的个人博客的问题.md","解决百度无法收录搭建在GitHub上的个人博客的问题"],["04.博客搭建/02.使用Gitalk实现静态博客无后台评论系统.md","使用Gitalk实现静态博客无后台评论系统"]]}],
|
||||
"/03.技术杂谈/": [{"title":"技术","collapsable":false,"children":[["01.技术/01.Git使用手册.md","Git使用手册"],["01.技术/02.Markdown使用教程.md","Markdown使用教程"],["01.技术/03.npm常用命令.md","npm常用命令"],["01.技术/04.yaml语言教程.md","yaml语言教程"]]},{"title":"GitHub","collapsable":false,"children":[["02.GitHub/01.GitHub高级搜索技巧.md","GitHub高级搜索技巧"],["02.GitHub/02.GitHub Actions 实现自动部署静态博客.md","GitHub Actions 实现自动部署静态博客"],["02.GitHub/03.GitHub Actions 定时运行代码:每天定时百度链接推送.md","GitHub Actions 定时运行代码:每天定时百度链接推送"]]},{"title":"Nodejs","collapsable":false,"children":[["03.Nodejs/01.nodejs递归读取所有文件.md","nodejs递归读取所有文件"]]},{"title":"博客搭建","collapsable":false,"children":[["04.博客搭建/01.解决百度无法收录搭建在GitHub上的个人博客的问题.md","解决百度无法收录搭建在GitHub上的个人博客的问题"],["04.博客搭建/02.使用Gitalk实现静态博客无后台评论系统.md","使用Gitalk实现静态博客无后台评论系统"]]}],
|
||||
|
||||
"/04.其他/": [{"title":"学习","collapsable":false,"children":[["01.学习/01.学习网站.md","学习网站"],["01.学习/02.学习效率低,忘性很大怎么办?.md","学习效率低,忘性很大怎么办?"]]},{"title":"学习笔记","collapsable":false,"children":[["02.学习笔记/01.小程序笔记.md","小程序笔记"]]},{"title":"面试","collapsable":false,"children":[["03.面试/01.面试问题集锦.md","面试问题集锦"]]},["04.在线工具.md","在线工具"],["05.友情链接.md","友情链接"]],
|
||||
|
||||
|
|
|
|||
Binary file not shown.
|
After Width: | Height: | Size: 49 KiB |
|
|
@ -0,0 +1,144 @@
|
|||
---
|
||||
title: GitHub Actions 定时运行代码:每天定时百度链接推送
|
||||
date: 2019-12-30
|
||||
permalink: /pages/f44d2f9ad04ab8d3
|
||||
---
|
||||
# GitHub Actions 定时运行代码:每天定时百度链接推送
|
||||
|
||||
## 前言
|
||||
|
||||
最近有个想法💡,就是想让百度尽快收录我的博客页面,在百度推送工具当中除了有自动推送的代码嵌入网站之外,还有一个实时的主动推送更高效。
|
||||
|
||||
于是我想到了用GitHub Actions的定时运行代码功能来每天自动运行命令生成所有博客链接并进行一次性推送给百度。
|
||||
|
||||
GitHub Actions 是一个 CI/CD(持续集成/持续部署)工具,但也可用作代码运行环境。**功能非常强大,能够玩出许多花样。**
|
||||
|
||||
## 百度主动链接推送
|
||||
|
||||
链接主动推送在百度站长中有介绍,如图。
|
||||
|
||||

|
||||
|
||||
具体使用方法就是创建一个文件`urls.txt`,文件内每行一条链接的格式写入提交的多个链接,如图。
|
||||
|
||||

|
||||
|
||||
运行命令
|
||||
|
||||
```sh
|
||||
curl -H 'Content-Type:text/plain' --data-binary @urls.txt "http://data.zz.baidu.com/urls?site=xugaoyi.com&token=T5PEAzhG*****"
|
||||
```
|
||||
|
||||
上面命令的地址和参数由百度站长提供。运行完命令会返回推送结果,不出意外的话就会把`urls.txt`内的所有链接一次性推送给百度。
|
||||
|
||||
这个方法虽然比嵌入网站头部的自动推送更高效,但是也有它的麻烦之处,就是得自己填入链接到`urls.txt`文件,然后手动运行命令。
|
||||
|
||||
|
||||
|
||||
## 自动生成urls.txt
|
||||
|
||||
没关系,技术的本质就是让人"偷懒"的。于是,我写了一个nodejs工具,用于把所有的博客页面链接生成到`urls.txt`
|
||||
|
||||
```js
|
||||
/**
|
||||
* 生成百度链接推送文件
|
||||
*/
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const readFileList = require('./modules/readFileList');
|
||||
const urlsRoot = path.join(__dirname, '..', 'urls.txt'); // 百度链接推送文件
|
||||
const DOMAIN = 'https://xugaoyi.com'
|
||||
|
||||
main();
|
||||
function main() {
|
||||
fs.writeFileSync(urlsRoot, DOMAIN)
|
||||
const files = readFileList(); // 读取所有md文件数据
|
||||
files.forEach( file => {
|
||||
const link = `\r\n${DOMAIN}${file.permalink}/`;
|
||||
console.log(link);
|
||||
fs.appendFileSync(urlsRoot, link);
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
上面代码仅是针对我个人的博客生成链接到`urls.txt`文件。更多代码在 [这里](https://github.com/xugaoyi/blog/blob/master/utils/baiduPush.js)。
|
||||
|
||||
哈哈,第一个麻烦解决了:smirk:,接下来是解决第二个需要手动运行推送命令的问题。
|
||||
|
||||
|
||||
|
||||
## GitHub Actions 定时运行代码
|
||||
|
||||
今天的主角GitHub Actions 要登场了。(相关:[GitHub Actions 入门教程](http://www.ruanyifeng.com/blog/2019/09/getting-started-with-github-actions.html?20191227113947#comment-last)、[GitHub Actions 实现自动部署静态博客](https://xugaoyi.com/pages/6b9d359ec5aa5019/))
|
||||
|
||||
GitHub Actions 是一个 CI/CD(持续集成/持续部署)工具,但也可用作代码运行环境。**功能非常强大,能够玩出许多花样。**
|
||||
|
||||
### 配置 GitHub Actions
|
||||
|
||||
触发 GitHub Actions 需要在项目仓库新建一个`.github/workflows`子目录,里面是 [YAML 格式](https://xugaoyi.com/pages/4e8444e2d534d14f/)配置文件,文件名可以随便取。GitHub 只要发现配置文件,就会运行 Actions。
|
||||
|
||||
配置文件的第一部分是触发条件。
|
||||
|
||||
```sh
|
||||
name: 'baiduPush'
|
||||
|
||||
on:
|
||||
push:
|
||||
schedule:
|
||||
- cron: '0 23 * * *'
|
||||
```
|
||||
|
||||
上面代码中,`name`字段是配置文件的描述,`on`字段是触发条件。我们指定两种情况下触发,第一种是代码 Push 进仓库,第二种是[定时任务](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/events-that-trigger-workflows#scheduled-events-schedule),每天在国际标准时间23点(北京时间+8,即早上7点)运行。
|
||||
|
||||
> 定时设置看[这里](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/events-that-trigger-workflows#scheduled-events-schedule)
|
||||
|
||||
接着,就是运行流程。
|
||||
|
||||
```sh
|
||||
jobs:
|
||||
bot:
|
||||
runs-on: ubuntu-latest # 运行环境为最新版的Ubuntu
|
||||
steps:
|
||||
- name: 'Checkout codes' # 步骤一,获取仓库代码
|
||||
uses: actions/checkout@v1
|
||||
- name: 'Run baiduPush.sh' # 步骤二,执行sh命令文件
|
||||
run: npm install && bash ./baiduPush.sh # 运行命令。(注意,运行目录是仓库根目录)
|
||||
```
|
||||
|
||||
上面代码中,指定运行环境是最新的ubuntu,流程的第一步是从代码仓库获取代码,第二步运行两个命令,先安装项目依赖,再运行`baiduPush.sh`文件。完整代码看 [这里](https://github.com/xugaoyi/blog/blob/master/.github/workflows/baiduPush.yml)
|
||||
|
||||
### `baiduPush.sh`生成`urls.txt`和执行百度推送命令
|
||||
|
||||
`baiduPush.sh`文件:
|
||||
|
||||
```sh
|
||||
#!/usr/bin/env sh
|
||||
|
||||
set -e
|
||||
|
||||
node utils/baiduPush.js # 生成所有链接文件
|
||||
|
||||
# 百度链接推送
|
||||
curl -H 'Content-Type:text/plain' --data-binary @urls.txt "http://data.zz.baidu.com/urls?site=https://xugaoyi.com&token=T5PEAzhGa*****"
|
||||
|
||||
rm -rf urls.txt # 灭迹
|
||||
```
|
||||
|
||||
上面代码中,先使用node运行先前写好的工具用于生成`urls.txt`文件,然后执行推送命令。
|
||||
|
||||
> baiduPush.sh内的命令其实可以写在yml文件的`run`字段中,我这里分开写是为了方便在本地也可以运行baiduPush.sh文件。
|
||||
|
||||
|
||||
|
||||
写好配置,推送到仓库,就会在每天的早上7点钟,自动运行命令生成一个包含博客所有页面链接的`urls.txt`文件,并把所有链接一次性推送到百度。麻麻再也不用担心我的网站不被收录~~:kissing_heart: :kissing_heart: :kissing_heart:
|
||||
|
||||
在这个基础上可以扩展,满足你自己的定时需求。
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
@ -5,7 +5,8 @@
|
|||
"dev": "node utils/frontmatter.js && node utils/sidebar.js && vuepress dev docs",
|
||||
"build": "node utils/frontmatter.js && node utils/sidebar.js && vuepress build docs",
|
||||
"deploy": "bash deploy.sh",
|
||||
"updateFM": "node utils/frontmatter.js -update"
|
||||
"updateFM": "node utils/frontmatter.js -update",
|
||||
"baiduPush": "node utils/baiduPush.js && bash baiduPush.sh"
|
||||
},
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
|
|
|
|||
Loading…
Reference in New Issue