23

Is there a way to disallow publishing of debug builds with ClickOnce?

I only want to allow release builds through, but right now human error causes a debug build to slip through once in a while.

We're publishing the build from within Visual Studio.

3 Answers 3

61

I have started to modify the .csproj files to include the following code to throw an error for debug deploys, effectively preventing the deploy from happening:

<!-- The following makes sure we don’t try to publish a configuration that defines the DEBUG constant -->
<Target Name="BeforePublish">
    <Error Condition="'$(DebugSymbols)' == 'true'" Text="You attempted to publish a configuration that defines the DEBUG constant!" />
</Target>

Just place it at the end of the file, right before the </Project> tag.

(original source: http://www.nathanpjones.com/wp/2010/05/preventing-clickonce-publishing-a-debug-configuration/comment-page-1/#comment-625)

5
  • 5
    I like this answer because a) it relies on the symbol being defined instead of the build being used and b) it doesn't presuppose a .bat being used to build
    – j.i.h.
    May 13, 2013 at 13:24
  • 8
    This is the best answer and doesn't require a batch file or some NuGet package to operate. I have further tweaked it so that all publishes must be Release/AnyCPU or it'll fail. <Target Name="BeforePublish"> <Error Condition="'$(Configuration)' != 'Release' Or '$(PlatformName)' != 'AnyCPU'" Text="You attempted to publish a configuration that is not Release and AnyCPU architecture!" /> </Target>
    – charlie
    Sep 26, 2013 at 19:58
  • chuck: it works as expected. However, element "Error" is not defined in the schema for "Target", msdn.microsoft.com/en-us/library/vstudio/t50z2hka.aspx. Has element "Error" been deprecated? However, it is in msdn.microsoft.com/en-us/library/8b08t3s4.aspx. Nov 5, 2013 at 14:19
  • 1
    To prevent to the error number field from being empty in the error message from MSBuild ("... .vbproj(245,5): error : You attempted to publish ..."), use element "Code", e.g. "<Target Name="BeforePublish"> <Error Condition="'$(Configuration)' != 'Release' Or '$(PlatformName)' != 'AnyCPU'" Code="9999" Text="You attempted to publish a configuration that is not Release and AnyCPU architecture!" /> </Target>". The error message would then be "... .vbproj(245,5): error 9999: You attempted to publish ..." Nov 5, 2013 at 14:28
  • I implemented the solution a couple of months ago and it just saved me (I used chuck's solution). I came back to this answer just to make sure I upvoted. I also really feel the need to give someone a high five. Mar 16, 2015 at 16:01
9

One thing you can do is add a condition to the .csproj or .vbproj file that MSBuild will check when doing a build.

The condition would check if a publish is occurring and check if the build is a debug build, then do something like run an external tool or otherwise interrupt the build process or cause it to fail.

An example might be something like this:

<Choose>
    <When Condition=" '$(Configuration)'=='Debug' ">
        <Exec Command="C:\foo.bat" ContinueOnError="false" />
    </When>
 </Choose>

Where foo.bat is a batch file that return non-zero, thus stopping the publish from occurring.

4
3

I have chosen another solution that worked for me:

I couldn't change my build process. So I did ToolsCustomize... and change the text of the action, adding an alert like "Publish [CONFIGURE TO RELEASE!]", and placing the Publish button next to the Debug/Release configuration option. It's easy!

With this I considerably reduced the risk of human error. Those buttons should always be together.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.