Hangfire
Hangfire copied to clipboard
Provide documentation / example for how to include the SQL Server 'install.sql' script in the build output
As I'm working on our CI/CD pipeline for deploying the application that uses Hangfire, we've decided we want to have the database schema creation/updates occur as part of the release pipeline rather than use the PrepareSchema=true approach.
What I'm struggling with is how to get the "install.sql" file to be included in the build output. Since this is embedded in the 'tools' portion of the NuGet package contents, it's not being included in the build output.
I've explored a little bit and see examples where something like the following 'should work' but it's not for me.
<Target Name="CopyHangfireAssets" AfterTargets="Build">
<Copy SourceFiles="(Hangfire.SqlServer)\tools\install.sql" DestinationFiles="$(OutDir)\Hangfire-SqlServer-Install.sql" />
</Target>
I've also tried $(Hangfire.SqlServer) and @(Hangfire.SqlServer) but neither combination of referencing the package works.
Being that a SQL script has been provided for this approach, it would be nice if the documentation provided examples of how to properly ensure that the latest version is always included in the build output so it can be captured via a build pipeline and then ran in the release pipeline ensuring the same script version that matches the Hangfire version is getting used.
In my specific case, we have our own package that wraps usage of Hangfire and it includes the actual Hangfire package reference; So I need to ensure that this copy-to-output approach works when our own package if referenced and that the behavior 'flows' to where the final consumer is being built & packaged.
All of the other approaches I've seen described are written from the perspective of the package author and manipulating things via the .nuspec file so that is another avenue potentially, but one I can't explore or implement myself.
Also note that given the generic naming, I have to consider renaming while copying so as it's clear & obvious what the script is and that it wouldn't have any name collisions where it's output to. Perhaps using a more descripting/unique name would be a good thing to consider for the future.
With further research, there's quite a few conversations out there around how to handle package contents and them being emitted/output as part of a build. The "right solutions" all seem to require the package author to define the desired contents & behavior through the .nuspec files and including specific targets.
There's also topics around the transitive behavior so that upstream consumers will be able to automatically include those defined targets/build behaviors. So I'm really thinking anything I can do on my side is basically going to be a hack / work-around at best and really the Hangfire team will need to better address these type of needs.
In the interim, I was able to find how to make it work.
(a) ensure the package reference includes the GeneratePathProperty argument.
<PackageReference Include="Hangfire.SqlServer" Version="1.7.31" GeneratePathProperty="true"/>
(b) use the generated property, noting that it is a variable ($) and that it has the prefix of "Pkg" applied as well as any periods are substituted with underscores. So I have this included in the consuming CSPROJ file - but - I have to do this in the final consuming application/host not my own intermediate Hangfire package that includes our 'common/shared' code.
<Target Name="CopyHangfireAssets" AfterTargets="Build">
<Copy SourceFiles="$(PkgHangfire_SqlServer)\tools\install.sql" DestinationFiles="$(OutDir)\Hangfire-SqlServer-Install.sql" />
</Target>
With this in place, I do see the file in my build output now. I'm going to aim to make this work for my immediate needs but would love to see a proper implementation in the package itself.