Orchestrating Visual Studio Code : Part 7 : NuGet Deployment

In this part, we will learn how to integrate NuGet package packing and deployment into our project.

The NuGet Script

We will add the method nugetPublish() to .vscode/tasks.json. This will search all projects in the solution that contain a .nuspec file, run dotnet pack, and curl upstream to NuGet.

nugetPublish () {

    echo -en "${YELLOW} Using Key: $nugetKey ${RESTORE}\n"

    buildEnvironment=@1

    if [[ -z buildEnvironment ]]; then
        buildEnvironment="debug"
    fi

    shopt -s nullglob # hide hidden

    cd src

    for dir in */ ; do # iterate projects
        [ -e "$dir" ] || continue

        cd $dir

        for nuspec in *.nuspec; do

            projectName=${dir::-1}
            echo -e "${YELLOW}Found nuspec for ${projectName} ${RESTORE}"

            dotnet pack \
            -c $buildEnvironment \
            --include-source \
            --include-symbols

            echo -e "${YELLOW}Publishing: ${projectName}.$nugetVersion ${RESTORE}"

            curl \
            -H 'Content-Type: application/octet-stream' \
            -H "X-NuGet-ApiKey: $nugetKey" \
            $nugetFeedUri \
            --upload-file bin/$buildEnvironment/${projectName}.$nugetVersion.nupkg

        done

        cd $ROOT_DIR

    done
}
project-tasks.sh

You can also use a MyGet feed with the variable $nugetFeedUri in this script.


The NuGet Task

We will add the task nuget-publish to the .vscode\tasks.json file:

{
    "label": "nuget-publish",
    "type": "shell",
    "osx": {
        "command": "bash ./scripts/project-tasks.sh nugetPublish"
    },
    "presentation": {
        "echo": true,
        "reveal": "always",
        "focus": true,
        "panel": "dedicated"
    },
    "problemMatcher": [],
    "windows": {
        "command": ".\\scripts\\project-tasks.ps1 -NugetPublish"
    }
}
tasks.json


Deploying

After adding a project with a .nuspec to your solution, press F1 and select the task nuget-publish. You will see the project get built, packed, and uploaded to NuGet or MyGet.

++++++++++++++++++++++++++++++++++++++++++++++++
+ Deploying nuget packages to nuget feed
+ https://www.myget.org/F/envoice/api/v2
++++++++++++++++++++++++++++++++++++++++++++++++

 Using Key: 00000000-0000-0000-0000-000000000000

Found nuspec for MyApp.NuGetProject
Microsoft (R) Build Engine version 15.6.82.30579 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

  Restoring packages for ...
   Publishing: MyApp.NuGetProject.1.0.0
    The package has been created.

++++++++++++++++++++++++++++++++++++++++++++++++
Uploaded nuspec for MyApp.NuGetProject
++++++++++++++++++++++++++++++++++++++++++++++++

Instead of storing your api-key in source code, the script looks for an environment variable NUGET_KEY. You also need to set the NuGet|MyGet feed uri to the one provided from your account.


The Source Code

You can find the source code for this article in the following repository under the part-7-nuget-deployment branch:

https://github.com/christophla/VSCode-Orchestration/tree/part-7-nuget-deployment

Next Post : Part 8 : CI Integration

In the next post we will learn how integrate our tasks and scripts in our Continuous Build Server.


Let's build this!!

Previous Post : Unit Test Code Coverage

https://christophertown.com/orchestrating-vscode-test-code-coverage