Github全自动部属
前言
借助Github Actions实现Github接到推送时,自动部属到Hexo站点,如:Github、Gtiee,原Conding已改为收费,所以不用。。。
以下内容基于本地git可以正常连接Github、Gitee
实现效果:
- 本地 推送 到 私库
- 私库 自动部署到公开的GitHub、Gitee站点
准备
私有仓库
用于存放源码站点仓库
用于存放公开public,也就blog存放仓库token
用于仓库之间的连接账户
用户名、仓库名
步骤
-
创建私有仓库,用来存放blog 源码
非public存放仓库
创建完成之后,将自己网站的所有源码,推送到私库。
记住仓库地址:私有仓库repo 下文用 【SourceRepo】
-
创建公开仓库,用于存放public
- 创建 github仓库,命名:
github账户名
+.github.io
选择公开
- 创建 gitee仓库,命名:与账户名同步
先创建私有,创建完成后在设置内开源
记住public仓库地址:Github / Gitee repo 下文分别用 【GithubRepo】、【GiteeRepo】 代替
记住public仓库名称:Github / Gitee Name 下文分别用 【GithubName】、【GiteebName】 代替
分别记住公开仓库地址
HTTPS/SSH
,下文涉及根据注释选择 - 创建 github仓库,命名:
-
修改分支,将所有仓库的默认分支均改为:
master
- Github :仓库→ settings → Branches
- gitee : 略
-
生成token
- Gihub ,具有repo 权限,时间永久。
- Gitee ,赋予所有权限即可,时间永久。
token 命名时建议详细说明此Token 用处,避免遗忘误删 下文分别用【GithubToken】【GiteeToken】代替
-
在已经推送的【SourceRepo】库中修改
_config.yml_
配置deploy
下文用 【Username】代替GitHub、Gitee的用户名
deploy: - type: git repo: gitHub: https://【Username】:【GithubToken】@github.com/【Username】/【GithubName】.git gitee: https://【Username】:【GiteeToken】@gitee.com/【Username】/【GiteebName】 branch: master message: 'Deloyed by Github Actions: {{ now("YYYY-MM--DD HH:mm:ss") }}'
-
在私有仓库【SourceRepo】下添加密钥
【SourceRepo】 Settings Secrets Actions New repository secret
变量名 值 GITEE_PASSWORD Gitee账户密码 GITEE_RSA_PRIVATE_KEY 【GiteeToken】 -
配置
Github Actions
【SourceRepo】 Actions New workflow set up a workflow yourself
填写下面代码
name: 自动部署 # 当有改动推送到master分支时,启动Action on: push: branches: - master #2020年10月后github新建仓库默认分支改为main,注意更改 release: types: - published jobs: deploy: runs-on: ubuntu-latest steps: - name: 检查分支 uses: actions/checkout@v2 with: ref: master - name: 安装 Node uses: actions/setup-node@v1 with: node-version: "12.x" - name: 安装 Hexo run: | export TZ='Asia/Shanghai' npm install hexo-cli -g - name: 缓存 Hexo uses: actions/cache@v1 id: cache with: path: node_modules key: ${{runner.OS}}-${{hashFiles('**/package-lock.json')}} - name: 安装依赖 if: steps.cache.outputs.cache-hit != 'true' run: | npm install gulp-cli -g #全局安装gulp npm install --save - name: 生成静态文件 run: | hexo clean hexo generate # hexo douban # 根据自己插件选择 # hexo bangumi -u #bilibili番剧更新 gulp - name: 部署 run: | git config --global user.name "like977" # github 用户名 git config --global user.email "1768045871@qq.com" # 邮箱 git clone 【GithubRepo】 .deploy_git # 此处务必用HTTPS链接。SSH链接可能有权限报错的隐患 hexo deploy - name: 同步到 Gitee uses: wearerequired/git-mirror-action@master env: # 注意在github私有仓库的Settings->Secrets 配置 GITEE_RSA_PRIVATE_KEY SSH_PRIVATE_KEY: ${{ secrets.GITEE_RSA_PRIVATE_KEY }} with: # 注意替换为你的 GitHub 源仓库地址 SSH source-repo: "【GithubRepo】" # 注意替换为你的 Gitee 目标仓库地址 SSH destination-repo: "【GithubRepo】" - name: 构建 Gitee Pages uses: yanglbme/gitee-pages-action@master with: # 注意替换为你的 Gitee 用户名 gitee-username: 【Username】 # 注意在在github私有仓库的Settings->Secrets 配置 GITEE_PASSWORD gitee-password: ${{ secrets.GITEE_PASSWORD }} # 注意替换为你的 Gitee 仓库 gitee-repo: 【Username】/【GiteeName】
修改部分
- name: 部署 run: | git config --global user.name "【Username】" # github 用户名 git config --global user.email "1768045871@qq.com" # 邮箱 git clone 【GithubRepo】 .deploy_git # 此处务必用【GithubRepo】的HTTPS链接。SSH链接可能有权限报错的隐患 hexo deploy - name: 同步到 Gitee uses: wearerequired/git-mirror-action@master env: # 注意在github私有仓库的Settings->Secrets 配置 GITEE_RSA_PRIVATE_KEY SSH_PRIVATE_KEY: ${{ secrets.GITEE_RSA_PRIVATE_KEY }} with: # 注意替换为你的 GitHub 源仓库地址 SSH source-repo: "【GithubRepo】" # 注意替换为你的 Gitee 目标仓库地址 SSH destination-repo: "【GithubRepo】" - name: 构建 Gitee Pages uses: yanglbme/gitee-pages-action@master with: # 注意替换为你的 Gitee 用户名 gitee-username: 【Username】 # 注意在在github私有仓库的Settings->Secrets 配置 GITEE_PASSWORD gitee-password: ${{ secrets.GITEE_PASSWORD }} # 注意替换为你的 Gitee 仓库 gitee-repo: 【Username】/【GiteeName】
-
注意事项
在生成静态文件时,根据自己的插件配置填入执行命令,没有相应的插件可注释掉,同时,如果仓库源码存在bug,hexo 命令将停留在错误点,可以在本地修改三连无误后再推到仓库。
- name: 生成静态文件 run: | hexo clean hexo generate # hexo douban # 根据自己插件选择 # hexo bangumi -u #bilibili番剧更新 # gulp #压缩
运行测试

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)