本文最后更新于2 分钟前,文中所描述的信息可能已发生改变。
折腾了一个晚上加半个上午终于成功了!(悲
使用场景
当你不想将你项目的源代码公开或者源代码中有些隐私信息的时候,你可以创建一个仓库A并设置为private用来存放你的源代码,再创建一个仓库B设置为public,由仓库A通过Github Action自动推送build后的产物到仓库B。
准备工作
- 创建SSH
ssh-keygen -t ed25519 -C "your_email@example.com"
详细信息请看《生成新的 SSH 密钥并将其添加到 ssh-agent》 - 将生成的私钥添加到当前仓库的secrets->action下的仓库变量中(即自定义加密),公钥添加到目标仓库的Deploy Keys中
在Valaxy项目的.github/workflows目录下创建gh-pages.yml的文件
文件名可以是任意的,Github会自动扫描该目录下的yml文件并执行。 有关Github Actions的相关概念可前往《GitHub Actions 文档》了解详细信息。
yml
name: GitHub Pages
on:
# 在触发push的时候执行
push:
branches:
# The branch where the project source code resides
# 项目源代码所在的分支
- mymain
jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
node-version: [lts/*]
os: [ubuntu-latest]
fail-fast: false
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
registry-url: https://registry.npmjs.org/
- name: 📦 Install Dependencies
run: npm i
- name: 🌌 Build Valaxy Blog
run: npm run build
- name: to target repo
uses: s0/git-publish-subdir-action@develop
env:
# 换成自己的目标仓库
REPO: git@github.com:owner/repo.git
# 目标分支
BRANCH: gh-pages
# 要推送的目录
FOLDER: dist
# 保存在secrets里面生成的ssh私钥
SSH_PRIVATE_KEY: ${{ secrets.DEPLOY_PRIVATE_KEY }}
结语
当我们在本地push到仓库A(使用了Github Actions的仓库)时,Github Actions会自动帮我们将build后的目录推送到仓库B,这样我们就可以保护我们的一些隐私信息不被轻易的查看。
参考文献: