Skip to content

私有仓库打包到公有仓库

☕ 写在前面

关于这个问题,起初是在github看到很多官方文档的仓库 大多是不公开的,仅将打包文件作为公开的部署资源,放在github 中; 因此便出现了今天的话题;

私有部署

首先,在github中创建一个私有仓库,并将打包文件上传到该仓库中; 然后,在github中创建一个公开仓库,并将打包文件上传到该仓库中; 最后,在github中创建一个项目,并将私有仓库和公开仓库作为项目的依赖仓库; 这样,在项目部署时,便可以拉取私有仓库和公开仓库中的打包文件;

整体工作

首先,我们创建好一个私有的仓库,同时创建一个共有仓库来放置我们的打包结果, 接着,我们将项目代码上传到私有仓库; 然后,actions 将会进行 intall、build等操作,将打包的结果,推送到一个公开的仓库; 最后,我们将当公开仓库内容发生变动时执行action 这样,我们就可以在互联网上看到我们的页面了

私有仓库workflow
shell
name: Deploy to Public Repo

on:
  push:
    branches:
      - main

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout private repo
        uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Install dependencies
        run: npm install

      - name: copy file
        run: npm run copy

      - name: Build project
        run: npm run build

      - name: Deploy to public repo
        run: |
          # Clone the public repo  
          git clone https://github.com/yhx-yhx/yhx825-pdf-search-demo-dist.git yhx825-pdf-search-demo-dist
          cd yhx825-pdf-search-demo-dist  

          # Copy built files to public repo  
          cp -r dist .

          # Add, commit, and push changes to public repo  
          git config --global user.name "yhx"  
          git config --global user.email "2045399856@qq.com"  
          git add .  
          git commit -m "Deploy from private repo"  
          git push origin main

        env:
          GITHUB_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }} 
        # 这里值得注意我们需要配置好github的token 名称可以是任意的
        # 首先,我们需要点击用户找到·setings ·
        # 接着,我们来到最有一个option ·Developer settings·
        # 然后,我们点击左侧的·Personal access tokens·
        # 接着,我们点击右侧的·Generate new token·
        # 接着,我们输入token的名称,并勾选上repo的权限
        # 最后,我们点击·Generate token·
        # 这样,我们就可以在github中找到我们刚刚创建的token了
        # 值得注意的是,我们创建的token是有有效期的,我们需要在有效期内使用它
        # 因此,我们可以在每次使用时,重新生成一个token 
        # 当然我们可以直接选择长期有效,这个token仅会出现一次,我们需要保存好
        # 接下来,我们配置各个仓库的 GIthubToken
        # 首先,点击最右侧settings ·
        # 接着,我们来到最下面找到·Secrets ·
        # 接着,我们点击右侧的·New repository secret·
        # 接着,我们输入token的名称,并输入token的值
        # 最后,我们点击·Add secret·
        # 这样,我们一切准备就绪!!!
共有仓库workflow
shell
# Simple workflow for deploying static content to GitHub Pages
name: Deploy static content to Pages

on:
  # Runs on pushes targeting the default branch
  push:
    branches: ["master"]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
permissions:
  contents: read
  pages: write
  id-token: write

# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
concurrency:
  group: "pages"
  cancel-in-progress: false

jobs:
  # Single deploy job since we're just deploying
  deploy:
    environment:
      name: github-pages
      url: ${{ steps.deployment.outputs.page_url }}
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4
      - name: Setup Pages
        uses: actions/configure-pages@v5
      - name: Upload artifact
        uses: actions/upload-pages-artifact@v3
        with:
          # Upload entire repository
          path: '.'
      - name: Deploy to GitHub Pages
        id: deployment
        uses: actions/deploy-pages@v4

结语

至此,我们便可以实现私有仓库打包到共有仓库了。 🎊 🎊 🎊 🎊🎊🎊