From 208c52986a9369b0dab196eb6ab0618b521e2f98 Mon Sep 17 00:00:00 2001 From: Jonathan Pobst Date: Thu, 9 Jan 2025 06:08:02 -1000 Subject: [PATCH] [ci] Fail build if any git tracked files were modified. (#9661) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When running `xaprepare` on Windows, your local `git` tree ends up dirty due to line ending changes in `THIRD-PARTY-NOTICES.txt`. Update `xaprepare` to always normalize line endings to `Environment.NewLine` in `THIRD-PARTY-NOTICES.txt` to prevent this issue. Additionally, these scenarios where the build unintentionally modifies git tracked files on some platforms are frustrating, so let's prevent them from happening in the first place. Add a CI check to fail a build if any git tracked files were modified. Example: > ❌ **git gree has modified tracked files.** ![image](https://github.com/user-attachments/assets/ec8869f5-75a5-4234-b7f4-20c2cabd743e) Clicking the error will show the modified file(s): > "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInte… > HEAD detached at b1a5f77f8 > Changes not staged for commit: > (use "git add ‹file›..." to update what will be committed) > (use "git restore ‹file›..." to discard changes in working directory) > modified: THIRD-PARTY-NOTICES. TXT > … > ##[error]git tree has modified tracked files. ![image](https://github.com/user-attachments/assets/395ee30e-8a37-450c-94ec-106221c40db3) --- .../yaml-templates/build-linux.yaml | 4 ++++ .../yaml-templates/build-macos.yaml | 4 ++++ .../yaml-templates/build-windows.yaml | 2 ++ .../yaml-templates/fail-on-dirty-tree.yaml | 22 +++++++++++++++++++ .../xaprepare/Steps/Step_ThirdPartyNotices.cs | 2 +- 5 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 build-tools/automation/yaml-templates/fail-on-dirty-tree.yaml diff --git a/build-tools/automation/yaml-templates/build-linux.yaml b/build-tools/automation/yaml-templates/build-linux.yaml index 29f35e1907d..6e098e9bb40 100644 --- a/build-tools/automation/yaml-templates/build-linux.yaml +++ b/build-tools/automation/yaml-templates/build-linux.yaml @@ -104,4 +104,8 @@ stages: includeBuildResults: true use1ESTemplate: ${{ parameters.use1ESTemplate }} + - template: /build-tools/automation/yaml-templates/fail-on-dirty-tree.yaml + parameters: + xaSourcePath: $(System.DefaultWorkingDirectory)/xamarin-android + - template: /build-tools/automation/yaml-templates/fail-on-issue.yaml diff --git a/build-tools/automation/yaml-templates/build-macos.yaml b/build-tools/automation/yaml-templates/build-macos.yaml index 0a29943c4d8..c13fc2ef93f 100644 --- a/build-tools/automation/yaml-templates/build-macos.yaml +++ b/build-tools/automation/yaml-templates/build-macos.yaml @@ -86,3 +86,7 @@ stages: artifactName: ${{ parameters.buildResultArtifactName }} includeBuildResults: true use1ESTemplate: ${{ parameters.use1ESTemplate }} + + - template: /build-tools/automation/yaml-templates/fail-on-dirty-tree.yaml + parameters: + xaSourcePath: ${{ parameters.xaSourcePath }} diff --git a/build-tools/automation/yaml-templates/build-windows.yaml b/build-tools/automation/yaml-templates/build-windows.yaml index 0a69a37b602..832cddfbede 100644 --- a/build-tools/automation/yaml-templates/build-windows.yaml +++ b/build-tools/automation/yaml-templates/build-windows.yaml @@ -117,4 +117,6 @@ stages: artifactName: ${{ parameters.buildResultArtifactName }} includeBuildResults: true + - template: /build-tools/automation/yaml-templates/fail-on-dirty-tree.yaml + - template: /build-tools/automation/yaml-templates/fail-on-issue.yaml diff --git a/build-tools/automation/yaml-templates/fail-on-dirty-tree.yaml b/build-tools/automation/yaml-templates/fail-on-dirty-tree.yaml new file mode 100644 index 00000000000..866e460669c --- /dev/null +++ b/build-tools/automation/yaml-templates/fail-on-dirty-tree.yaml @@ -0,0 +1,22 @@ +# Ensure the build did not produce any modified checked in files + +parameters: + condition: succeeded() + xaSourcePath: + +steps: +- powershell: | + # Run this to log the output for the user + git status + + # Run this to error the build if untracked files + $process= git status --porcelain --untracked-files=no + + if ($process) + { + Write-Host "##vso[task.logissue type=error]git tree has modified tracked files." + Write-Host "##vso[task.complete result=Failed;]" + } + displayName: Ensure no modified committed files + workingDirectory: ${{ parameters.xaSourcePath }} + condition: ${{ parameters.condition }} diff --git a/build-tools/xaprepare/xaprepare/Steps/Step_ThirdPartyNotices.cs b/build-tools/xaprepare/xaprepare/Steps/Step_ThirdPartyNotices.cs index 6b28b261528..47c2690e4cb 100644 --- a/build-tools/xaprepare/xaprepare/Steps/Step_ThirdPartyNotices.cs +++ b/build-tools/xaprepare/xaprepare/Steps/Step_ThirdPartyNotices.cs @@ -151,7 +151,7 @@ string FetchTPNLicense (string relativeFilePath) if (!File.Exists (path)) throw new InvalidOperationException ($"License file {path} does not exist"); - return File.ReadAllText (path); + return File.ReadAllText (path).ReplaceLineEndings (); } void EnsureValidTPNType (Type type)