GitHub Composite Action二次checkout导致配置文件还原的问题解决咨询
解决第二次checkout导致配置文件被还原的问题
你的问题核心在于重复执行了actions/checkout@v3步骤——第一次checkout已经把仓库代码完整拉取到了工作目录(还包含子模块和全量提交历史),第二次checkout会强制将工作目录重置为仓库的原始提交状态,自然就覆盖了你之前通过variable-substitution修改的tests.settings.json文件。
最简单的解决方案:移除多余的第二次checkout
直接删掉Workflow中第二个actions/checkout@v3步骤即可,后续的Composite Action会直接在当前已有的工作目录下执行,不需要再次拉取代码。修改后的完整Workflow配置如下:
name: Sonarcloud on: push: branches: - main pull_request: types: [opened, synchronize, reopened] workflow_call: secrets: SONAR_TOKEN: required: true workflow_dispatch: ~ jobs: build: name: Build runs-on: windows-latest steps: - uses: actions/checkout@v3 with: submodules: 'true' fetch-depth: 0 - uses: microsoft/variable-substitution@v1 with: files: 'tests/IntegrationTests/tests.settings.json' env: ConnectionString: ${{ secrets.CONNECTIONSTRING }} # 注意:本仓库中需替换连接字符串,但不同仓库可能需替换其他内容(如SAS Token),因此无法将变量替换步骤移至Composite Action中 - id: sonarcloud uses: my-org/sonarcloud@v1 with: sonar_project_key: 'my-project' sonar_token: ${{ secrets.SONAR_TOKEN }} github_token: ${{ secrets.GITHUB_TOKEN }}
额外场景补充(若未来需再次checkout但保留修改)
如果之后遇到必须再次checkout的需求(比如拉取其他分支代码),可以先通过Git暂存当前修改,checkout完成后再恢复修改内容:
- name: 暂存修改的配置文件 shell: powershell run: git add tests/IntegrationTests/tests.settings.json && git stash push -m "stash modified settings" - uses: actions/checkout@v3 with: ref: other-branch - name: 恢复修改的配置文件 shell: powershell run: git stash pop
不过在你当前的场景下,完全不需要这一步操作,移除多余的checkout就能直接解决测试失败的问题。
内容的提问来源于stack exchange,提问作者Omar Amalfi
相关产品推荐
相关产品推荐

