xChar
·8 months ago

GitHub Actions 是 GitHub 的推出的持续集成服务。教程可以参看 GitHub Actions 入门教程官方文档

我们或许会希望在仓库A上触发 workflow 将仓库A上的全部或部分代码,提交(部署)到另一个仓库 B 上,此时需要分情况考虑

仓库A

不论仓库 A 公有或私有,对整个workflow没有影响。在创建workflow时,github会自动创建秘钥,以支持仓库访问。秘钥通过 ${{ secrets.GITHUB_TOKEN }} 从上下文中获取(示例)。秘钥权限可以根据需求调整。

仓库B

public

个人博客项目可能会需要将博客页面部署到github page上。此时我们可以在私有的仓库A上维护博客源代码,然后在A仓库上定义工作流,对源代码进行编译和部署。这时候仓库B是一个公有仓库

# to do

该种类型也适用下面的private小节中介绍方法

private

有时也可能会向私有的仓库B提交代码

这就需要引入 deploy key 再通过ssh协议提交

此时需要首先生成ssh key,例如

ssh-keygen -t ed25519

仓库 A > Settings > Secrets > New Repository Secret 配置私钥

仓库 B > Settings > Deploy Keys > Add Deploy Key 配置公钥

然后在仓库A 中构造github action的 workflow 如下

name: test

on:
  push:
    branches:
      - 'main'

jobs:
  release:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout code
      uses: actions/checkout@v4
      with:
        token: ${{ secrets.GITHUB_TOKEN }}

    - name: Git init
      run: |
        git config user.name "${{ github.actor }}"
        git config user.email "${{ github.actor_id }}@users.noreply.github.com"
      ### or you can use github action bot as commit user
      # git config --local user.email "[email protected]"
      # git config --local user.name "GitHub Action

    - name: Add SSH Key
      uses: webfactory/[email protected]
      with:
        ssh-private-key: ${{ secrets.DEPLOY_SSH_KEY }}

    - name: Push to repo
      run: |
        git remote add target "[email protected]:${{ your repo path }}.git"
        git checkout --orphan temp
        git add --all
        git commit --allow-empty -m "your commit message"
        git push target temp:main --force

提交github测试即可


参考: https://stackoverflow.com/questions/68590575/github-actions-remote-repo-issues

Loading comments...