如何在并发 Github Actions 工作流运行中提交更改并将其推送到存储库?

问题描述 投票:0回答:1

我有一个 Github Actions 工作流程,它从我的存储库中的文件 (config.json) 中获取值,递增该值并将更改推送到存储库。

config.json 文件:

{
    "sequence_number": 1
}

sequence_number 将增加 1。

当我运行工作流程一次时,它会正确完成工作。

当我连续运行两个工作流程时,运行会排队。第一次运行再次正确地将更改推送到文件(我可以在第一次运行后看到更新的值),但在(排队的)第二次运行期间,我收到以下错误:

 ! [rejected]        main -> main (non-fast-forward)
error: failed to push some refs to 'https://github.com/repo_x'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. If you want to integrate the remote changes,
hint: use 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

我的工作流程看起来像这样:

name: workflow

concurrency:
  group: ${{ github.workflow }}

...

jobs:
  extracting-value-from-config-file:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
      with:
        fetch-depth: 0
  
    - name: Reading The JSON Configuration File & Extracting The Value
      id: extracted-value
      run: |
        json=$(cat config.json)
        number=$(echo "$json" | jq -r '.sequence_number')
...

  updating-config-file:
    needs: extracting-value-from-config-file
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - run: |
          echo "`jq '.sequence_number=${{ needs.extracting-value-from-config-file.outputs.SEQUENCE_NUMBER }}+1]' config.json`" > config.json
          git config user.name github-actions
          git config user.email [email protected]
          git add .
          git commit -m "AUTO updated sequence number"
          git push

基本上,第二次运行期间的错误表明它尚未拉取最新版本,但我在两个工作流程作业中执行了 git pull (actions/checkout@v4)。

有人知道我在这里做错了什么吗?

我尝试执行各种拉取请求,但不知何故它永远不会拉取最新版本的配置文件。

git github github-actions workflow
1个回答
0
投票

我解决了问题,但我不确定这个解决方案是否好。

这是我在代码中更改的内容:

jobs:
  extracting-value-from-config-file:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
    - run: |
        git config user.name github-actions # added
        git config user.name [email protected] # added
        git fetch -all # added
        git reset --hard origin/main # added

...

  updating-config-file:
    needs: extracting-value-from-config-file
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: |
          git fetch --all # added
          git reset --hard origin/main # added
          echo "`jq '.sequence_number=${{ needs.extracting-value-from-config-file.outputs.SEQUENCE_NUMBER }}+1]' config.json`" > config.json
          git config user.name github-actions
          git config user.email [email protected]
          git add .
          git commit -m "AUTO updated sequence number"
          git push
© www.soinside.com 2019 - 2024. All rights reserved.