diff --git a/source/Calamari.Tests/CommitToGitCommandTest.cs b/source/Calamari.Tests/CommitToGitCommandTest.cs index eda7d0092..16026feb6 100644 --- a/source/Calamari.Tests/CommitToGitCommandTest.cs +++ b/source/Calamari.Tests/CommitToGitCommandTest.cs @@ -570,6 +570,24 @@ public void CommitToGit_FailsWithCommandException_WhenCustomPropertiesFileDoesNo .Should().NotBe(0, "the command must reject runs whose --customPropertiesFile path does not exist"); } + [Test] + public void CommitToGit_SkipsCommitting_WhenTransformationScript_ReturnsNonZeroExitCode() + { + const string packageReferenceName = "my-configs"; + const string destinationPath = "output-dir"; + + var zipPath = CreateZipWithEntry(packageReferenceName, "configs/settings.json", "{\"setting\": \"value\"}"); + AddInputPackageVariables(packageReferenceName, zipPath, destinationPath); + variables.AddRange([ + new CalamariExecutionVariable(ScriptVariables.ScriptBody, "exit 1", false), + new CalamariExecutionVariable(ScriptVariables.Syntax, ScriptSyntax.Bash.ToString(), false), + ]); + + RunCommitToGit().Should().NotBe(0); + GetCommittedFileContent($"{destinationPath}/configs/settings.json") + .Should().BeNull("the package files should not have been committed into the repository under the destination path, when the transformation script returns non-zero exit code"); + } + // --- Helpers --- int RunCommitToGit(params string[] extraArgs) diff --git a/source/Calamari/Commands/CommitToGitCommand.cs b/source/Calamari/Commands/CommitToGitCommand.cs index 30f7722fb..641ae10bb 100644 --- a/source/Calamari/Commands/CommitToGitCommand.cs +++ b/source/Calamari/Commands/CommitToGitCommand.cs @@ -286,6 +286,12 @@ IEnumerable BuildCommitToRemoteConventions(CommitToGitRepositorySet { new DelegateInstallConvention(d => { + var exitCode = d.Variables.GetInt32(SpecialVariables.Action.Script.ExitCode) ?? 0; + if (exitCode != 0) + { + throw new CommandException($"Transformation script exited with code {exitCode}. Changes will not be committed."); + } + var commitParams = repositoryConfig!.CommitParameters; var updater = new RepositoryUpdater(commitParams, log, new UserDefinedCommitMessageGenerator(commitParams.Description));