✍github action部署参考

obsidian +git维护hexo博客参考

搜索关键词:obsidian 部署 hexo github

github action 部署 hexo 过程中踩坑记录及解决办法

github action 报错

git@github.com: Permission denied (publickey).

fatal: Could not read from remote repository.
原因:无公钥,需重新生成
解决 :
在git Bash中执行:

1
ssh-keygen -f github-deploy-key # 生成秘钥

会在当前目录中生成两个文件:

  • 私钥文件 github-deploy-key 
  • 公钥文件 github-deploy-key.pub

配置私钥
复制 github-deploy-key 文件内容,在Hexo项目仓库,此处即 blog 仓库(priviate) Settings -> Secrets -> Secrets -> Actions 页面上点击 New repository secret 添加。

在 Name 输入框填写 HEXO_DEPLOY_PRI
在 Value 输入框填写 github-deploy-key 文件内容。
配置公钥:
1.和私钥一样,只不过需要在部署github page的仓库配置公钥,以便链接。
2.记得勾选 Allow write access 选项。
参考文章:利用 Github Actions 自动部署 Hexo博客 | 天涯路远 (diego369.github.io)

编写github action

在本地 blog 仓库中 创建文件 .github/workflows/autodeply.yml (无路径的话就直接创建该路径文件夹,该文件在上传github仓库后在github action中自动运行文件中设定的工作流)GitHub Actions 的工作流语法 - GitHub 文档

github创建work flow:

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
name: 自动部署

#当有改动推送到master分支时,启动Action

on:

  push:

    branches:

      - master #2020年10月后github新建仓库默认分支改为main,注意在github上更改main为master

  release:

    types:

      - published

env:

  GIT_USER: weixiansheng

  GIT_EMAIL: ${{secrets.GITHUBEMAIL}}

  THEME_REPO: jerryc127/hexo-theme-butterfly

  THEME_BRANCH: master



jobs:

  deploy:

    runs-on: ubuntu-latest

    steps:

      - name: 检查分支

        uses: actions/checkout@v2

        with:

          ref: master #2020年10月后github新建仓库默认分支改为main,注意在github上更改main为master

      - name: Checkout theme repo #检查主题

        uses: actions/checkout@v2

        with:

          repository: ${{ env.THEME_REPO }}

          ref: ${{ env.THEME_BRANCH }}

          path: node_modules/hexo-theme-butterfly

      - name: 安装 Node

        uses: actions/setup-node@v3

        with:

          node-version: "20.x" #action使用的node版本,建议大版本和本地保持一致。可以在本地用node -v查询版本号。

      - name: 配置 环境变量

        env:

          HEXO_DEPLOY_PRI: ${{secrets.HEXO_DEPLOY_PRI}}

        run: |

          mkdir -p ~/.ssh/

          echo "$HEXO_DEPLOY_PRI" > ~/.ssh/id_rsa

          chmod 600 ~/.ssh/id_rsa

          ssh-keyscan github.com >> ~/.ssh/known_hosts

          git config --global user.name $GIT_USER

          git config --global user.email $GIT_EMAIL

      - name: 安装 Hexo

        run: |

          export TZ='Asia/Shanghai'

          npm install hexo-cli -g

      - name: 缓存 Hexo

        id: cache-npm

        uses: actions/cache@v3

        env:

          cache-name: cache-node-modules

        with:

          path: node_modules

          key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}

          restore-keys: |

            ${{ runner.os }}-build-${{ env.cache-name }}-

            ${{ runner.os }}-build-

            ${{ runner.os }}-

      - name: 安装依赖

        if: ${{ steps.cache-npm.outputs.cache-hit != 'true' }}

        run: |

          npm install --save

      - name: 生成静态文件

        run: |

          hexo clean

          hexo generate

      - name: 部署到Github page

        run: |

          hexo deploy        

        #uses: JamesIves/github-pages-deploy-action@v4

        #env:

         #PERSONAL_TOKEN: ${{ secrets.ACCESS_TOKEN }}

         #PUBLISH_REPOSITORY: it-briefcode/it-briefcode.github.io # The repository the action should deploy to.

         #BRANCH: master  # The branch the action should deploy to.

         #PUBLISH_DIR: ./public # The folder the action should deploy.

        #uses: JamesIves/github-pages-deploy-action@v4

        #with:

          #token: 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

          #repository-name: weixiansheng1/weixiansheng1.github.io

          #branch: main

          #folder: public

          #commit-message: "${{ github.event.head_commit.message }} Updated By Github Actions"

        #run: |

          #ssh-add /Users/duansg/.ssh/id_rsa

          #git config --global user.name "[GithubUsername]"

          #git config --global user.email "[GithubEmail]"

          #git clone https://github.com/weixiansheng1/weixiansheng1.github.io.git .deploy_git

          # 此处务必用HTTPS链接。SSH链接可能有权限报错的隐患

          # =====注意.deploy_git前面有个空格=====

          # 这行指令的目的是clone博客静态文件仓库,防止Hexo推送时覆盖整个静态文件仓库,而是只推送有更改的文件

          #hexo deploy

完成后,点击deploy ,可看到警告:该警告提示 Node.js 12 的准备过时(即将弃用),需要使用Node.js 16 actions/setup-node:使用特定版本的节点设置 GitHub 操作工作流.js

我们来修改 github action 中的workflow:点击Workflow file

v2-416ea48fe1744b98225240c0f3e27b8c_1440w.webp (1440×859) (zhimg.com)
都改成:

  • uses: actions/checkout@v3
  • uses: actions/setup-node@v3
    v2-b195dfd17c71494076bed11c1a7368ca_r.jpg (1919×1281) (zhimg.com)
    修改完成后点击提交
    v2-0f22a0a85b9769cf89236db2ce840e72_1440w.webp (1440×791) (zhimg.com)
    运行成功,无任何警告和报错,之后备份到本地workflow/autodeply.yaml中,以免下次提交后覆盖
    v2-21a1f87ef4cb035056ed407dc30392cd_r.jpg (2400×1340) (zhimg.com)
    修改完成后,使用git add . 载入缓存,git commit -m”up date” 链接仓库,git push / git push -u origin master/git push origin master /git push –force origin master 来测试本地的workflow.file文件是否被修改好v2-c22aa176769ec5bc5999ca1a5d785741_1440w.webp (845×1103) (zhimg.com)

如果git bash部署过程中报错:Another git process seems to be running in this repository, e.g.

翻译:另一个git进程似乎正在这个存储库中运行,例如 由“git commit”打开的编辑器。请确保所有流程终止,然后重试。如果它仍然失败,一个git进程可能已在此存储库中崩溃:手动删除文件以继续。

原因:.git下的index.lock文件,在进行某些比较费时的git操作时自动生成,操作结束后自动删除,相当于一个锁定文件,目的在于防止对一个目录同时进行多个操作。有时强制关闭进行中的git操作,这个文件没有被自动删除,之后就无法进行其他git操作,必须手动删除。

解决方法:git Bash输入

1
git clean -f .git/index.lock

github action 主题不显示:

解决:github 上配置hexo theme 要在本地主题文件夹复制到theme中
这样既能够本地hexo s启动服务器去测试,又可上传github启动action workflow。

报错 :The deploy step encountered an error: The process ‘/usr/bin/git’ failed with exit code 128

原因:github action 无操作仓库权限
解决:需要在workflow file补充环境env变量信息,参考本文workflow写法

git add .命令错误,显示有其他文件正在使用

原因:

  1. git block文件但未删除
  2. 百度网盘同步过程中占用该文件