github 复合操作结帐分离

github composite action checkout detach

我已经创建了一个复合操作,请参阅 link for 运行 a Sonarcloud analysis for dotnet projects。

name: Sonarcloud
description: Sonarcloud
inputs:
  sonar_project_key:
    required: true
    type: string
  github_token:
    required: true
    type: string
  sonar_token:
    required: true
    type: string

runs:
  using: "composite"
  steps:
    - name: Set up JDK 11
      uses: actions/setup-java@v1
      with:
       java-version: 1.11

    - name: Install dotnet SonarCloud scanner
      shell: powershell
      run: |
        dotnet tool install --global dotnet-sonarscanner
     
    - name: Build and analyze
      shell: powershell 
      env:
        GITHUB_TOKEN: ${{ inputs.github_token }}
        SONAR_TOKEN: ${{ inputs.sonar_token }}
      run: |
            dotnet sonarscanner begin /k:"${{ inputs.sonar_project_key }}" /o:"my-org" /d:sonar.login="${{ inputs.sonar_token }}" /d:sonar.host.url="https://sonarcloud.io" /d:sonar.cs.opencover.reportsPaths="**/coverage.opencover.xml"
            dotnet build --configuration Release
            dotnet test --no-restore --configuration Release --verbosity normal /p:CollectCoverage=true /p:CoverletOutputFormat=opencover
            dotnet sonarscanner end /d:sonar.login="${{ inputs.sonar_token }}"

然后按照 link 我必须创建一个带有标签“v1”的版本,例如:“my-org/sonarcloud@v1”,然后在另一个存储库中使用它,如下所示:

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 }} # please note that in this repo is connection string but it could vary from repo to repo, 
                                                              # maybe in another repo I need to substitute a Sas token for example
                                                              # so I cannot move the variable substitution to the composite action
      - uses: actions/checkout@v3
      - id: sonarcloud
        uses: my-org/sonarcloud@v1
        with:
            sonar_project_key: 'my-project'
            sonar_token: ${{ secrets.SONAR_TOKEN }}
            github_token: ${{ secrets.GITHUB_TOKEN }}

检查我是否需要修改“tests.settings.json”文件,以便为测试提供有效的连接字符串。

现在是问题。转换正在正确进行,但在这里:

- uses: actions/checkout@v3
- id: sonarcloud    
  uses: my-org/sonarcloud@v1

git 意识到“test.settings.json”已被修改并将其恢复到原始版本(不包含连接字符串)并且测试失败。 这是工作流程的日志:

2022-04-29T10:56:04.3078283Z [command]"C:\Program Files\Git\bin\git.exe" checkout --detach
2022-04-29T10:56:04.8735279Z M  tests/IntegrationTests/tests.settings.json
2022-04-29T10:56:04.8736695Z HEAD is now at 5e6cf4b fix

那么,如何才能在获得复合操作所需的第二次结帐中避免这种行为?

谢谢

我找到问题了!第二个

  • uses: actions/checkout@v3

不需要。要修复它,只需删除它。

替换为:

- uses: actions/checkout@v3
- id: sonarcloud
  uses: my-org/sonarcloud@v1
    with:
     sonar_project_key: 'my-project'
     sonar_token: ${{ secrets.SONAR_TOKEN }}
     github_token: ${{ secrets.GITHUB_TOKEN }}

作者:

- id: sonarcloud
  uses: my-org/sonarcloud@v1
    with:
     sonar_project_key: 'my-project'
     sonar_token: ${{ secrets.SONAR_TOKEN }}
     github_token: ${{ secrets.GITHUB_TOKEN }}