前提已创建两个仓库:
- 私有仓库 - 存储 博客源代码
- 公共仓库 - 存储 生成后的博客静态文件
首先 设置ssh 密钥对
1.生成密钥 可以使用git bash 命令工具,执行下面的命令,生成 私钥 和公钥
ssh-keygen -t rsa -C '邮箱'
2.设置github
-
- 在github 账户中设置公钥,路径:SSH and GPG keys (github.com) ,SSH keys 点击 new ssh key
-
打开公钥文件,复制内容到 key 输入框
- 打开私有库 settings-》secrets,设置私有密钥,点击 New repository secret;打开私钥文件,将私钥内部复制进key输入框中。
-
设置actions,打开私有库的 actions ,点击 set up a workflow yourself.
-
编写配置文件
-
# This is a basic workflow to help you get started with Actions name: clrs-blog # Controls when the action will run. on: # Triggers the workflow on push or pull request events but only for the main2021 branch push: branches: [ main2021 ] # Allows you to run this workflow manually from the Actions tab workflow_dispatch: # 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 - uses: actions/checkout@v2 - uses: actions/setup-node@v2 with: node-version: '>=12.x' # Runs a set of commands using the runners shell - name: Install hexo-cli run: | npm install hexo-cli -g npm install - name: Config Private Key env: PRIVATE_KEY: ${{ secrets.PRIVATE_KEY }} #此处的PRIVATE_KEY 是配置私有库时settings-》secrets的名称 run: | mkdir -p ~/.ssh/ echo "$PRIVATE_KEY" > ~/.ssh/github_rsa chmod 600 ~/.ssh/github_rsa ssh-keyscan github.com >> ~/.ssh/known_hosts - name: Config Git run: | git config --global user.name 'xxx' git config --global user.email 'xxx@xx.x' - name: Deploy run: | hexo clean & hexo g & hexo d
3.设置hexo 配置文件
在_config.yml中找到deploy节点,配置如下
deploy:
type: git
repo: git@github.com:xxxxx/xxxxx.github.io.git #github库 ssh 地址,此处一定要复制 ssh地址,如果用https地址,actions 运行的时候,执行到hexo deploy 会报错 could not read Username for 'https://github.com': No such device or address
branch: main2021
name: xxxx
email: xxxx@xx.x
最后,做好以上配置,即可写新文章然后推送到github,actions 将自动运行并将生成的静态文件部署到公开库中。
yaml 参考:YAML 语言教程 - 阮一峰的网络日志 (ruanyifeng.com)
github actions 参考:GitHub Actions 入门教程 - 阮一峰的网络日志 (ruanyifeng.com)