Github Actions实现仓库自动同步Gitee并更新文档

2,113 阅读2分钟

业务开发过程中,经常用到日期格式化url参数转对象浏览器类型判断节流函数等常用函数,为避免不同项目多次复制粘贴的麻烦,我封闭了工具库github.com/CatsAndMice…。如果你也有常用的代码,欢迎为本项目提交pr。

前言

工作之余,我一直再扩展github.com/CatsAndMice…工具库。仓库部署在Github,文档使用了Gitee Page,但每次 push代码或更新文档都需要手动将Github代码同步Gitee,并且需要人为点击更新按钮重新部署Gitee Page,有点蠢。Github Actions可以帮我自动化完成前述操作,本文记录我配置Github Actions的过程,希望能帮助到有类似需求的读者。

配置Github Actions

首先需要知道Github Actions是什么,这里准备一篇阮一峰老师Github Actions介绍文章GitHub Actions 入门教程 - 阮一峰的网络日志 (ruanyifeng.com)

创建Github工作区

在项目文件夹下面,创建.github\workflows 文件夹,然后创建一个.yml 类型的文件,这里我取名为gitee-repos-mirror

gitee-repos-mirror.yml文件添加配置

gitee-repos-mirror.yml 写入如下配置:

name: Sync To Gitee  #名字  可自定义
on: page_build  #触发条件   page_build表示Github Page部署完成后触发
jobs:
  sync:
    runs-on: ubuntu-latest
    steps:
      - uses: wearerequired/git-mirror-action@master #开源actions包
        env:
          SSH_PRIVATE_KEY: ${{ secrets.GITEE_PRIVATE_KEY }}
        with:
          source-repo: "git@github.com:CatsAndMice/medash.git" # github仓库地址
          destination-repo: "git@gitee.com:JsHai/tool.git" # gitee仓库地址

其中wearerequired/git-mirror-action@master 是Github开源的仓库github.com/wearerequir…

source-repo,destination-repo 字段地址均采用了SSH 地址,所以需要在Github中设置SSH Key

配置私钥和公钥

需要安装git,然后打开Git Bash Here

打开之后,输入ssh-keygen,一路按enter键。

注意:.ssh是隐藏文件,需开启文件显示

全部结束后,再到C:\Users\用户名\.ssh 文件夹会生成三个文件,其中id_rsa 为私钥,id_rsa.pub 为公钥。

Github配置SSH Key

打开id_rsa.pub 文件,复制全部内容,然后进入Github,按如下图进行操作:

Github配置SSH Key 完成。

Gitee配置SSH Key

Gitee亦需要配置SSH Key, 操作与Github配置SSH Key类似。

Github仓库配置secrests

打开id_rsa 文件,复制全部内容,进入Github对应的仓库,切换至settings

添加一个以GITEE_PRIVATE_KEY 命名的secret, 粘贴私钥。

到此,完成了Github仓库同步至Gitee的目的,还需要配置更新Gitee Page的actions。

actions自动更新Gitee Page

为了方便快速访问,部署文档时优先考虑Gitee Page,但缺点是文档更新需要人为去点击,没有Github Page那么智能自动更新。因此需要借助actions帮我们完成。

gitee-repos-mirror.yml文件添加文档更新配置

name: Sync To Gitee  
on: page_build
jobs:
  sync:
    runs-on: ubuntu-latest
    steps:
      - uses: wearerequired/git-mirror-action@master
        env:
          SSH_PRIVATE_KEY: ${{ secrets.GITEE_PRIVATE_KEY }}
        with:
          source-repo: "git@github.com:CatsAndMice/medash.git"
          destination-repo: "git@gitee.com:JsHai/tool.git"
      - name: Build Gitee Pages
        uses: yanglbme/gitee-pages-action@main
        with:
          # 注意替换为你的 Gitee 用户名
          gitee-username: 130****3806 #我的用户名是电话号码
          # 注意在 Settings->Secrets 配置 GITEE_PASSWORD
          gitee-password: ${{ secrets.GITEE_PASSWORD }}
          # 注意替换为你的 Gitee 仓库,仓库名严格区分大小写,请准确填写,否则会出错
          gitee-repo: JsHai/tool
          # 要部署的分支,默认是 master,若是其他分支,则需要指定(指定的分支必须存在)
          branch: master

yanglbme/gitee-pages-action@main 为Gihub开源轮子: github.com/wearerequir…

注意点:gitee-repo 表示仓库名,最好使用url中的信息。由于我仓库更换过名字,如果使用JsHai/medash 会报错。

配置GITEE_PASSWORD

GITEE_PASSWORD 为Gitee登陆密码,与上文中Github仓库配置****secrests 操作一样。

提交至Github仓库

将创建的文件,push至Github仓库后。每次向master 分支推送新的代码时,pages build and deployment 首先执行更新Github Page ,Github Page更新完成后触发Sync To Gitee 同步Gitee并更新Gitee Page。

文档更新完成后,可以访问jshai.gitee.io/tool,但访问不了。这是因为我将文档放在docs文件夹下,只需要在链接后面添加/docs ,即可访问成功jshai.gitee.io/tool/docs

简单一个gif动图演示下,详情可查看github.com/CatsAndMice…

总结

文章总结配置Github Actions,通过Actions代替我们完成日常中简单重复的操作。Actions还有更多好玩的操作,有兴趣的小伙伴可以去探索下。