Skip to content

Commit 3f15f98

Browse files
committed
Fail build on Docker build failure
1 parent e5ced22 commit 3f15f98

File tree

3 files changed

+84
-15
lines changed

3 files changed

+84
-15
lines changed

Source/ApiTemplate/build.cake

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ Task("DockerBuild")
105105
// docker buildx inspect --bootstrap
106106
// To stop using buildx remove the buildx parameter and the --platform, --progress switches.
107107
// See https://github.com/docker/buildx
108-
StartProcess(
108+
var exitCode = StartProcess(
109109
"docker",
110110
new ProcessArgumentBuilder()
111111
.Append("buildx")
@@ -121,9 +121,13 @@ Task("DockerBuild")
121121
.AppendSwitchQuoted("--file", dockerfile.ToString())
122122
.Append(".")
123123
.RenderSafe());
124+
if (exitCode != 0)
125+
{
126+
throw new Exception($"Docker build failed with non zero exit code {exitCode}.");
127+
}
124128

125129
// If you'd rather not use buildx, then you can uncomment these lines instead.
126-
// StartProcess(
130+
// var exitCode = StartProcess(
127131
// "docker",
128132
// new ProcessArgumentBuilder()
129133
// .Append("build")
@@ -135,13 +139,22 @@ Task("DockerBuild")
135139
// .AppendSwitchQuoted("--file", dockerfile.ToString())
136140
// .Append(".")
137141
// .RenderSafe());
142+
// if (exitCode != 0)
143+
// {
144+
// throw new Exception($"Docker build failed with non zero exit code {exitCode}.");
145+
// }
146+
//
138147
// if (push)
139148
// {
140-
// StartProcess(
149+
// var pushExitCode = StartProcess(
141150
// "docker",
142151
// new ProcessArgumentBuilder()
143152
// .AppendSwitchQuoted("push", $"{tag}:{version}")
144153
// .RenderSafe());
154+
// if (pushExitCode != 0)
155+
// {
156+
// throw new Exception($"Docker push failed with non zero exit code {pushExitCode}.");
157+
// }
145158
// }
146159

147160
string GetVersion()
@@ -150,25 +163,35 @@ Task("DockerBuild")
150163
var directoryBuildPropsDocument = System.Xml.Linq.XDocument.Load(directoryBuildPropsFilePath);
151164
var preReleasePhase = directoryBuildPropsDocument.Descendants("MinVerDefaultPreReleasePhase").Single().Value;
152165

153-
StartProcess(
166+
var exitCode = StartProcess(
154167
"dotnet",
155168
new ProcessSettings()
156169
.WithArguments(x => x
157170
.Append("minver")
158171
.AppendSwitch("--default-pre-release-phase", preReleasePhase))
159172
.SetRedirectStandardOutput(true),
160173
out var versionLines);
174+
if (exitCode != 0)
175+
{
176+
throw new Exception($"dotnet minver failed with non zero exit code {exitCode}.");
177+
}
178+
161179
return versionLines.LastOrDefault();
162180
}
163181

164182
string GetGitCommitSha()
165183
{
166-
StartProcess(
184+
var exitCode = StartProcess(
167185
"git",
168186
new ProcessSettings()
169187
.WithArguments(x => x.Append("rev-parse HEAD"))
170188
.SetRedirectStandardOutput(true),
171189
out var shaLines);
190+
if (exitCode != 0)
191+
{
192+
throw new Exception($"git rev-parse failed with non zero exit code {exitCode}.");
193+
}
194+
172195
return shaLines.LastOrDefault();
173196
}
174197
});

Source/GraphQLTemplate/build.cake

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ Task("DockerBuild")
105105
// docker buildx inspect --bootstrap
106106
// To stop using buildx remove the buildx parameter and the --platform, --progress switches.
107107
// See https://github.com/docker/buildx
108-
StartProcess(
108+
var exitCode = StartProcess(
109109
"docker",
110110
new ProcessArgumentBuilder()
111111
.Append("buildx")
@@ -121,9 +121,13 @@ Task("DockerBuild")
121121
.AppendSwitchQuoted("--file", dockerfile.ToString())
122122
.Append(".")
123123
.RenderSafe());
124+
if (exitCode != 0)
125+
{
126+
throw new Exception($"Docker build failed with non zero exit code {exitCode}.");
127+
}
124128

125129
// If you'd rather not use buildx, then you can uncomment these lines instead.
126-
// StartProcess(
130+
// var exitCode = StartProcess(
127131
// "docker",
128132
// new ProcessArgumentBuilder()
129133
// .Append("build")
@@ -135,13 +139,22 @@ Task("DockerBuild")
135139
// .AppendSwitchQuoted("--file", dockerfile.ToString())
136140
// .Append(".")
137141
// .RenderSafe());
142+
// if (exitCode != 0)
143+
// {
144+
// throw new Exception($"Docker build failed with non zero exit code {exitCode}.");
145+
// }
146+
//
138147
// if (push)
139148
// {
140-
// StartProcess(
149+
// var pushExitCode = StartProcess(
141150
// "docker",
142151
// new ProcessArgumentBuilder()
143152
// .AppendSwitchQuoted("push", $"{tag}:{version}")
144153
// .RenderSafe());
154+
// if (pushExitCode != 0)
155+
// {
156+
// throw new Exception($"Docker push failed with non zero exit code {pushExitCode}.");
157+
// }
145158
// }
146159

147160
string GetVersion()
@@ -150,25 +163,35 @@ Task("DockerBuild")
150163
var directoryBuildPropsDocument = System.Xml.Linq.XDocument.Load(directoryBuildPropsFilePath);
151164
var preReleasePhase = directoryBuildPropsDocument.Descendants("MinVerDefaultPreReleasePhase").Single().Value;
152165

153-
StartProcess(
166+
var exitCode = StartProcess(
154167
"dotnet",
155168
new ProcessSettings()
156169
.WithArguments(x => x
157170
.Append("minver")
158171
.AppendSwitch("--default-pre-release-phase", preReleasePhase))
159172
.SetRedirectStandardOutput(true),
160173
out var versionLines);
174+
if (exitCode != 0)
175+
{
176+
throw new Exception($"dotnet minver failed with non zero exit code {exitCode}.");
177+
}
178+
161179
return versionLines.LastOrDefault();
162180
}
163181

164182
string GetGitCommitSha()
165183
{
166-
StartProcess(
184+
var exitCode = StartProcess(
167185
"git",
168186
new ProcessSettings()
169187
.WithArguments(x => x.Append("rev-parse HEAD"))
170188
.SetRedirectStandardOutput(true),
171189
out var shaLines);
190+
if (exitCode != 0)
191+
{
192+
throw new Exception($"git rev-parse failed with non zero exit code {exitCode}.");
193+
}
194+
172195
return shaLines.LastOrDefault();
173196
}
174197
});

Source/OrleansTemplate/build.cake

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ Task("DockerBuild")
105105
// docker buildx inspect --bootstrap
106106
// To stop using buildx remove the buildx parameter and the --platform, --progress switches.
107107
// See https://github.com/docker/buildx
108-
StartProcess(
108+
var exitCode = StartProcess(
109109
"docker",
110110
new ProcessArgumentBuilder()
111111
.Append("buildx")
@@ -121,9 +121,13 @@ Task("DockerBuild")
121121
.AppendSwitchQuoted("--file", dockerfile.ToString())
122122
.Append(".")
123123
.RenderSafe());
124+
if (exitCode != 0)
125+
{
126+
throw new Exception($"Docker build failed with non zero exit code {exitCode}.");
127+
}
124128

125129
// If you'd rather not use buildx, then you can uncomment these lines instead.
126-
// StartProcess(
130+
// var exitCode = StartProcess(
127131
// "docker",
128132
// new ProcessArgumentBuilder()
129133
// .Append("build")
@@ -135,13 +139,22 @@ Task("DockerBuild")
135139
// .AppendSwitchQuoted("--file", dockerfile.ToString())
136140
// .Append(".")
137141
// .RenderSafe());
142+
// if (exitCode != 0)
143+
// {
144+
// throw new Exception($"Docker build failed with non zero exit code {exitCode}.");
145+
// }
146+
//
138147
// if (push)
139148
// {
140-
// StartProcess(
149+
// var pushExitCode = StartProcess(
141150
// "docker",
142151
// new ProcessArgumentBuilder()
143152
// .AppendSwitchQuoted("push", $"{tag}:{version}")
144153
// .RenderSafe());
154+
// if (pushExitCode != 0)
155+
// {
156+
// throw new Exception($"Docker push failed with non zero exit code {pushExitCode}.");
157+
// }
145158
// }
146159

147160
string GetVersion()
@@ -150,25 +163,35 @@ Task("DockerBuild")
150163
var directoryBuildPropsDocument = System.Xml.Linq.XDocument.Load(directoryBuildPropsFilePath);
151164
var preReleasePhase = directoryBuildPropsDocument.Descendants("MinVerDefaultPreReleasePhase").Single().Value;
152165

153-
StartProcess(
166+
var exitCode = StartProcess(
154167
"dotnet",
155168
new ProcessSettings()
156169
.WithArguments(x => x
157170
.Append("minver")
158171
.AppendSwitch("--default-pre-release-phase", preReleasePhase))
159172
.SetRedirectStandardOutput(true),
160173
out var versionLines);
174+
if (exitCode != 0)
175+
{
176+
throw new Exception($"dotnet minver failed with non zero exit code {exitCode}.");
177+
}
178+
161179
return versionLines.LastOrDefault();
162180
}
163181

164182
string GetGitCommitSha()
165183
{
166-
StartProcess(
184+
var exitCode = StartProcess(
167185
"git",
168186
new ProcessSettings()
169187
.WithArguments(x => x.Append("rev-parse HEAD"))
170188
.SetRedirectStandardOutput(true),
171189
out var shaLines);
190+
if (exitCode != 0)
191+
{
192+
throw new Exception($"git rev-parse failed with non zero exit code {exitCode}.");
193+
}
194+
172195
return shaLines.LastOrDefault();
173196
}
174197
});

0 commit comments

Comments
 (0)