搭建GitHub 自动镜像至 Gitee
GitHub Actions
首先簡單介紹下 GitHub Actions,其官方介紹頁面為:https://github.com/features/actions,介紹語如下:
Automate your workflow from idea to production. GitHub Actions makes it easy to automate all your software workflows, now with world-class CI/CD. Build, test, and deploy your code right from GitHub. Make code reviews, branch management, and issue triaging work the way you want.
簡而言之就是提供了一個高效易用的 CI/CD 工作流,幫助我們自動構建、測試、部署我們的代碼。
另外它支持三大平臺—— Linux、MacOS、Windows,支持任何編程語言,而且官方提供了許許多多的 Actions 庫供我們直接使用,幫助我們更快地搭建工作流。
GitHub Actions 的官方文檔可以見:https://help.github.com/en/actions/automating-your-workflow-with-github-actions,如果大家想好好研究下的話,一定要好好看看。
一、準備工作
1.生成同步用的ssh密鑰
ssh-keygen
2.添加公鑰文件 id_rsa.pub 的內容至 Github、Gitee的個人配置里,點 Github、Gitee。目的是為了可以使用git協議免輸入密碼。
3.新建Gitee私人令牌Token,點這里。添加后復制這次的Token內容。
4.Fork 工程https://github.com/Yikun/hub-mirror-action。
5.添加私鑰 id_rsa 內容至剛剛 Fork 的 Github 項目 hub-mirror-action。 點擊Settings。
然后添加Secrets
二、導入倉庫(可以跳過,主要是為了能使用Gitee提供的強制同步功能)
1.導入你需要鏡像的倉庫
三、編寫workflow (推薦使用ssh模式)
1.刪除fork項目目錄下的.github/workflows 的所有工作流配置。
2.添加一個鏡像配置gitee-repos-sync.yml
########################################
#
#使用參考
#https://zhuanlan.zhihu.com/p/102977226
#http://yikun.github.io/2020/01/17/%E5%B7%A7%E7%94%A8Github-Action%E5%90%8C%E6%AD%A5%E4%BB%A3%E7%A0%81%E5%88%B0Gitee/
#https://github.com/Yikun/hub-mirror-action/blob/master/README_CN.md
#https://help.github.com/cn/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets
#
# 建議先在gitee上先導入github的項目,再編輯此文件增加同步功能。這樣就能利用好gitee的強制同步功能了。
########################################
name: Gitee repos mirror periodic sync job
on:
# push推送時觸發
#push:
# 定時任務觸發
schedule:
# 每天北京時間9點跑
# Runs at 01:00 UTC (9:00 AM Beijing) every day
- cron: '0 1 * * *'
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Cache phpdragon src repos
# 引用發行版的主要版本
uses: actions/cache@v1
with:
path: /home/runner/work/phpdragon/phpdragon-cache
key: ${{ runner.os }}-phpdragon-repos-cache
- name: Mirror the Github organization repos to Gitee.
# 引用master分支的腳本支持
uses: phpdragon/hub-mirror-action@master
with:
# 需要被同步的源端賬戶名,如github/phpdragon,表示Github的phpdragon賬戶。
src: github/phpdragon
# 需要同步到的目的端賬戶名,如gitee/phpdragon,表示Gitee的phpdragon賬戶。
dst: gitee/phpdragon
# 用于目的端上傳代碼的SSH key,用于上傳代碼
dst_key: ${{ secrets.GITEE_PRIVATE_SSH_KEY }}
# 創建倉庫的API tokens, 用于自動創建不存在的倉庫
dst_token: ${{ secrets.GITEE_PRIVATE_TOKEN }}
account_type: user
# 指定目錄cache
cache_path: /github/workspace/phpdragon-cache
# 白名單,僅同步白名單中的repos,如“repo1,repo2,repo3”
white_list: "hub-mirror-action,spark-example"
# 僅同步靜態列表,不會再動態獲取需同步列表(黑白名單機制依舊生效)
static_list: "spark-example"
# 強制更新
force_update: true
# clone方式,使用http、ssh方式進行clone
clone_style: "ssh"
3. 提交代碼。等待定時任務觸發即可。
4.如果要測試,請開啟 push 觸發。效果如下:
源碼地址:https://github.com/phpdragon/hub-mirror-action
感謝:
http://yikun.github.io/、https://github.com/Yikun
參考鏈接:
巧用Github Action同步代碼到Gitee
GitHub Action使用雜記
創建和存儲加密密碼
Github Action你值得了解的~
你知道什么是 GitHub Action 么?
https://blog.csdn.net/cqcre/article/details/103342314
總結
以上是生活随笔為你收集整理的搭建GitHub 自动镜像至 Gitee的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 高德地图的使用
- 下一篇: 关于SAP S/4HANA里ABAP源代