githubasp.net-corecakebuildmyget

Define version and Push project to Github and MyGet using Cake


I am building an ASP.NET Core 1.1 with .csproj file using Cake. I need to push the source to Github and publish the package to MyGet. At the moment I have the script:

String target = Argument<String>("target", "Default");

Task("Clean").Does(() => {    
  if (DirectoryExists("./build")) 
    DeleteDirectory("./build", true);
  CreateDirectory("./build");      
});

Task("Restore").Does(() => {  
  FilePathCollection projects = GetFiles("./**/*.csproj");
  foreach(FilePath project in projects) 
    DotNetCoreRestore(project.FullPath);
});

Task("Build").IsDependentOn("Clean").IsDependentOn("Restore").Does(() =>   {  
  FilePathCollection projects = GetFiles("./**/*.csproj");
  foreach(FilePath project in projects) 
    DotNetCoreBuild(project.FullPath);
});

Task("Test").IsDependentOn("Build").Does(() => {
  FilePathCollection projects = GetFiles("./test/**/*.csproj");
  foreach(FilePath project in projects) 
    DotNetCoreTest(project.FullPath);
});

Task("Pack").IsDependentOn("Test").Does(() => {  
  DotNetCorePack("./src/MyProject.csproj", new DotNetCorePackSettings { OutputDirectory = "./build/MyProject/" });
});

Task("Default").IsDependentOn("Pack");

RunTarget(target);

How can I do the following:
1. Increment the revision number based on the version of the csproj file;
2. Push the project to Github;
3. Push the package to MyGet.


Solution

    1. For this sort of thing I use a combination of GitVersion and MagicChunks both of which have Cake Aliases available. Gitversion is used to assert the version number of the repo, and MagicChunks is used to update different sections of xml, json files, etc.
    2. Can you confirm exactly what you mean here? Are you talking about commiting changes back into the GitHub repository? If so, you might want to take a look at the Cake.Git addin. Or, if you are talking about pushing files to a GitHub release, you might want to take a look at the GitReleaseManager aliases.
    3. Once you have the NuGet package, you should be able to use the NuGetPush alias to push the package to MyGet. You will need to set the Source property of the NuGetPushSettings class.

    Based on some of your comments, I am updating this answer slightly...

    I typically don't use Cake to push changes back to the source control repository. Rather, the commits that I make into the source control repository cause my build to be triggered, typically using some form of Continuous Integration Server, like AppVeyor or TeamCity. That way, the commit message that you are asking about will always be known by the user, as they are making the commit, and will know what has changed. By using this approach, there are no automated commits in to the repository, and personally, I think that this is how it should be.

    Based on the above, you would only need the Release target that you mention. As part of the build process, GitVersion will assert the version number, and allow you to create NuGet packages with that version number (which then doesn't require an additional commit into the repository) and then you can push out to NuGet.org, and GitHub.