关于github:Github-Actions自动部署Hugo到Gitee同时刷新Gitee-Pages

4次阅读

共计 2629 个字符,预计需要花费 7 分钟才能阅读完成。

我的博客应用 GitHub 上的 pages 性能公布的基于 Hugo 生成的动态网站,根本无奈失常拜访,所以想要同步一份到 gitee 上公布,当初应用 GitHub Actions 提供的计算机资源就能够间接在 GitHub 上进行动态网站的生成,公布,近程刷新 gitee pages,触发条件能够是 push 或者定时等等,堪称非常好用,之后看到能够间接同步到 gitee 仓库,就实现一下试试,以下就是实现步骤,以及踩坑,当然强烈建议看开源代码的官网阐明文档。

生成公钥和私钥并填入仓库

输出ssh-keygen -t rsa -C "user@email.com",而后回车几次,会生成 id_rsa.pub 文件和 id_rsa 文件,别离寄存公钥和私钥:

Gitee 仓库填入公钥

将公钥 id_rsa.pub 中的数据填入到 gitee 待备份仓库界面下 settings→Deploy keys→add personal public key

这里留神:要选右上增加 personal public key 才有写入权限

GitHub 仓库填入私钥

Settings→Secret→New repository secre 用于之后的程序环境配置拜访,命名为 GITEE_RSA_PRIVATE_KEY

生成 GitHub 账号的 personal access token

将仓库权限选上就行了,而后将生成的 token,配到私钥配置的中央 仓库→Settings→Secret→New repository secre,命名为 ACCESS_TOKEN

在仓库 secret 处增加 GITEE_PASSWORD,放入 gitee 账号密码用于刷新 gitee pages

同之前步骤雷同,之后用于环境变量的配置,就是以下 3 条 secret,OSS 的是主动部署到阿里 OSS 的脚本应用的,我是放在一个脚本里运行,须要理解能够看我另一篇文章。

在 GitHub 仓库创立并编写 Actions 脚本!!!

文件名随便从这点开就行,上面有模板,点开后批改也行,创立的文件默认放在 .github/workflows/

也能够用命令创立mkdir -p .github/workflows && touch .github/workflows/name.yml

将代码拷入,批改具体变量,比方仓库名等,如果不须要 deploy 间接去掉就行了,不影响:

name: deploy blog to gitee
 
on:
  push:
    branches:
      - main    # master 分支 push 的时候触发
      
jobs:
  deploy: #执行部署 Hugo 生成动态代码,默认放在 gh-pages 分支
    runs-on: ubuntu-18.04
    steps:
      - uses: actions/checkout@v2
        with:
          submodules: recursive  # Fetch Hugo themes (true OR recursive)
          fetch-depth: 0    # Fetch all history for .GitInfo and .Lastmod

      - name: Setup Hugo
        uses: peaceiris/actions-hugo@v2
        with:
          hugo-version: '0.81.0'
          extended: true #不须要 extended 版本就能够正文

      - name: Build
        run: hugo --minify

      - name: Deploypage
        uses: peaceiris/actions-gh-pages@v3
        with:
          personal_token: ${{secrets.ACCESS_TOKEN}}
          external_repository: JohntunLiu/JohntunLiu.github.io
          publish_branch: gh-pages  # default: gh-pages
          publish_dir: ./public
          
      - name: Deploygitee
        uses: peaceiris/actions-gh-pages@v3
        with:
          personal_token: ${{secrets.ACCESS_TOKEN}}
          publish_dir: ./public
                
  
  sync: #同步到 gitee 仓库
    needs: deploy
    runs-on: ubuntu-latest
    steps:
    - name: Sync to Gitee
      uses: wearerequired/git-mirror-action@master
      env:
        SSH_PRIVATE_KEY: ${{secrets.GITEE_RSA_PRIVATE_KEY}}
      with:
        # 起源仓库
        source-repo: "git@github.com:JohntunLiu/myblog.git"
        # 指标仓库
        destination-repo: "git@gitee.com:JohntunLiu/JohntunLiu.git"
        
  reload-pages:
    needs: sync
    runs-on: ubuntu-latest
    steps:
      - name: Build Gitee Pages
        uses: yanglbme/gitee-pages-action@main
        with:
          # 留神替换为你的 Gitee 用户名
          gitee-username: JohntunLiu
          # 留神在 Settings->Secrets 配置 GITEE_PASSWORD
          gitee-password: ${{secrets.GITEE_PASSWORD}}
          # 留神替换为你的 Gitee 仓库,仓库名严格辨别大小写,请精确填写,否则会出错
          gitee-repo: JohntunLiu/JohntunLiu
          # 要部署的分支,默认是 master,若是其余分支,则须要指定(指定的分支必须存在)branch: gh-pages

点击 commit changes 提交运行,之后就看失去运行流程了

实际效果和流程

如果是部署其余动态网站,批改 deploy 的代码块就行了,我顺便把部署到 GitHub pages 放在了外面:name: Deploypage

如果是 hugo 的话开源人员还提供了缓存机制,能够进步部署速度,能够去开源局部看,具体怎么看就是复制- uses: peaceiris/actions-hugo@v2 前面的局部搜寻到 GitHub 中看,比方:https://github.com/peaceiris/actions-hugo,readme.md 文档写得相当详实,也会更新阐明

更多精彩能够关注我的博客或微信公众号 LiuJohntun,记录并分享我的所见、所学、所想 …

正文完
 0