如何在主分支(main/master)推送时更新所有分支?

huangapple go评论53阅读模式
英文:

How can I update all branches when main/master has a push?

问题

我尝试了一些方法。这是我现在正在使用的内容。

```yaml
name: 更新分支

on:
  push:
    branches:
      - main

jobs:
  更新分支:
    runs-on: ubuntu-latest

    steps:
      - name: 检出仓库
        uses: actions/checkout@v2

      - name: 获取分支名称
        id: 分支
        run: |
                    git ls-remote --heads origin | awk -F'/' '{print $3}' | grep -vE 'main$' > ${{ github.workspace }}/branches.txt

      - name: 更新分支
        run: |
          while read -r branch; do
            git checkout $branch
            git merge origin/main --no-edit
            git push origin $branch
          done < ${{ github.workspace }}/branches.txt          

如果我使用这个出现错误:

error: 路径规范 'reddit-fix' 未匹配到任何已知于 git 的文件
Error: 进程以退出代码 1 完成。

reddit-fix 是存在的分支。

我想知道是否有任何方法可以实现这一目标。我尝试过使用GitHub Marketplace上找到的一个操作 这个,但我无法使其工作。


<details>
<summary>英文:</summary>

I&#39;ve tried a few things. This is what I&#39;m using right now.

```yaml
name: Update Branches

on:
  push:
    branches:
      - main

jobs:
  update-branches:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v2

      - name: Get branch names
        id: branches
        run: |
          git ls-remote --heads origin | awk -F&#39;/&#39; &#39;{print $3}&#39; | grep -vE &#39;main$&#39; &gt; ${{ github.workspace }}/branches.txt

      - name: Update branches
        run: |
          while read -r branch; do
            git checkout $branch
            git merge origin/main --no-edit
            git push origin $branch
          done &lt; ${{ github.workspace }}/branches.txt

I am getting an error if I use this:

error: pathspec &#39;reddit-fix&#39; did not match any file(s) known to git
Error: Process completed with exit code 1.

reddit-fix is a branch that exists.

I'd like to know if there is any way to achieve this. I've tried using an action I found on the GitHub marketplace this one, but I couldn't make this work.

答案1

得分: 0

Azeem 回答了这个问题。我将其发布为答案,以便我可以将问题标记为已解决。

使用 actions/checkout@v3,使用 fetch-depth: 0 获取所有分支。
还要添加 git config --global user.email "" git config --global user.name "<my_username>"。

这里 是一个包含完整答案的存储库,包括文件夹结构和 yml 文件。您可以在 这个存储库 中看到它正在工作。

英文:

Azeem answered this. I'm posting this as an answer so that I can mark the question as solved.

With actions/checkout@v3, use fetch-depth: 0 to fetch all the branches.
Also add git config --global user.email "<myemail>" git config --global user.name "<my_username>".

This is a repo with the entire answer i.e folder structure and yml file. You can see it working in this repo

huangapple
  • 本文由 发表于 2023年6月16日 14:57:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76487656.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定