文件配置
- 在
github
构建仓库blog
用于存储博客源码
- 本地
git clone
仓库
- 将能够正常部署的
hexo
博客文档复制到本地git
仓库中,执行提交push
- 创建
.github/workflows/Hexo-Auto-Deploy.yml
文件并填入执行脚本(脚本在下面)
需要提交至blog源码目录结构如下:
![微信图片_20220923101325]()
图中红框是必要的源码文件,其余是我写的git上传脚本,theme
文件夹为非必要文件
Hexo-Auto-Deploy.yml
脚本代码如下,配置需要根据个人需要修改几个变量:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| name: Hexo Auto Deploy
on: push: branches: - master workflow_dispatch:
env: GIT_USER: GIT_EMAIL: GIT_DEPLOY_REPO: GIT_DEPLOY_BRANCH: master
jobs: build: name: Build on node ${{ matrix.node_version }} and ${{ matrix.os }} runs-on: ubuntu-latest if: github.event.repository.owner.id == github.event.sender.id strategy: matrix: os: [ubuntu-20.04] node_version: [16.x]
steps: - name: Checkout uses: actions/checkout@v2
- name: Checkout deploy repo uses: actions/checkout@v2 with: repository: ${{ env.GIT_DEPLOY_REPO }} ref: ${{ env.GIT_DEPLOY_BRANCH }} path: .deploy_git
- name: Use Node.js ${{ matrix.node_version }} uses: actions/setup-node@v1 with: node-version: ${{ matrix.node_version }}
- name: Configuration environment env: HEXO_DEPLOY_PRI: ${{secrets.GITEE_PRIVATE_KEY}} run: | sudo timedatectl set-timezone "Asia/Shanghai" mkdir -p ~/.ssh/ echo "$HEXO_DEPLOY_PRI" > ~/.ssh/id_rsa chmod 600 ~/.ssh/id_rsa eval `ssh-agent -s` ssh-add ~/.ssh/id_rsa ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts ssh-keyscan -t rsa gitee.com >> ~/.ssh/known_hosts sudo chown -R runner:runner ~/.ssh git config --global user.name $GIT_USER git config --global user.email $GIT_EMAIL
- name: Install dependencies run: | npm install
- name: Deploy hexo run: | npm run deploy
|
若需要部署到阿里oss
实现静态页面托管可追加以下代码
1 2 3 4 5 6 7 8 9
| - name: Setup Aliyun OSS uses: manyuanrong/setup-ossutil@master with: endpoint: oss-cn-beijing.aliyuncs.com access-key-id: ${{ secrets.OSS_KEY }} access-key-secret: ${{ secrets.OSS_SECRET }} - name: Deploy to aliyun run: ossutil cp -rf public oss://xxxx/
|
OSS_KEY
与OSS_SECRET
为阿里的AccessID
和AccessSecret
参照下方GITEE_PRIVATE_KEY
的配置
github配置
看到上面的action
脚本配置也能知道我们需要一套ssh
密钥了,
用本地git命令行ssh-keygen
重新生成一套rsa
公钥私钥
按正常配置ssh
与github
的方式,将*.pub
里的公钥添加到github
账户ssh key
中
将id_rsa
里的私钥添加在仓库的 Settings -> Secrets -> Actions
中,取个GITEE_PRIVATE_KEY
的名字(因为之前做github
与gitee
交互所以用的这个名字,可以根据自己需要更改但也要更改脚本里的名字)存放私钥
到这里就可以写完博客直接执行push命令自动化部署了
1 2 3
| git add . git commit -m '文件更新' git push
|