2018年7月4日 星期三

[Gulp] Use gulp-msbuild to publish


 gulp    gulp-msbuild   yargs    ASP.NET Web Deploy


Introduction


gulp-msbuild : msbuild plugin for gulp
yargs : parsing arguments and generating an elegant user interface

We will use gulp-msbuild and yargs to execute a web deploy of ASP.NET to remote server.
Notice that gulp-msbuild takes the benefits of msbuild commands and thus it can be used for integration of continuous delivery.



Environment


gulp.js 3.9.1
gulp-msbuild 0.5.5
yargs 12.0.1
ASP.NET MVC5



Implement


Install packages

$ npm install gulp-msbuild --save-dev
$ npm install yargs --save-dev


yargs

First let’s see an easy sample of using yargs to get the arguments in gulp command.

var gulp = require('gulp');
var argv = require('yargs').argv;

gulp.task("default", function () {
    console.log(`User=${argv.user}`);
    console.log(`Password=${argv.pwd}`);
})


Test it like following,

$ gulp --user=JB --pwd=123456

User=JB
Password=123456

$ gulp --user JB --pwd 123456

User=JB
Password=123456

$ gulp --user=JB

User=JB
Password=undefined

$ gulp --user=JB --pwd

User=JB
Password=true

$ gulp --user=JB --pwd=

User=JB
Password=


Publish files

An example for publishing all projects (which can be published) in a solution to specified folder.

var buildOptions = {
    outputDir: "./gulp_deploy",
    configuration: "Test" //Configuration name (組態名稱)
};

gulp.task("msbuild", function () {

    return gulp.src("./MyProject.sln")
        .pipe(msbuild({
            targets: ['Clean', 'Build'], //Clean and Build
            toolsVersion: 'auto', //the .NET Tools-Version, such as 1.0, 1.1, 2.0, 3.5, 4.0, 12.0, 14.0, 15.0, 'auto'
            errorOnFail: true, //Cause the gulp task to fail
            stdout: true, //Show output of msbuild
            properties: {
                Optimize: true,
                DebugSymbols: true, 
                Configuration: buildOptions.configuration,
                OutDir: buildOptions.outputDir,
                UseWPP_CopyWebApplication: true,
                PipelineDependsOnBuild: false,
            },
        })
        );
});

Here are the descriptions of the properties.

Property name
Description
Value
Optimize
Optimize the compiler
boolean
DebugSymbols
Enable or disable generating PDB(.pdb) file
boolean
Configuration
Configuration name (組態名稱)

OutDir
Output publish files to specified directory

UseWPP_CopyWebApplication
Allow to leverage the web.config transformation
boolean
PipelineDependsOnBuild
Generate build files by projects
boolean

Find more details about MSBUILD properties on Common MSBuild Project Properties.


$ gulp msbuild

Will results in the following files,




You can find the published web sites in _PublishedWebsites.


Deploy to remote server with web deploy

Before using this feature, make sure you had installed web deploy agent on remote server and create a web deploy profile in the ASP.NET project. See more information in Web Deployment Overview for Visual Studio and ASP.NET.






var buildOptions = {
    outputDir: "../gulp_deploy",
    publishProfile: "MyDeployProfile",
    configuration: "Test"
};


gulp.task("msbuild", function () {

    return gulp.src("./MyWebsite.csproj")
        .pipe(msbuild({
            targets: ['Clean', 'Build'],
            toolsVersion: 'auto', //the .NET Tools-Version
            errorOnFail: true, //Cause the gulp task to fail
            stdout: true, //Show output of msbuild
            properties: {
                Optimize: true,
                DebugSymbols: true,
                Configuration: buildOptions.configuration,
                Platform: "AnyCPU",
                //OutDir: buildOptions.outputDir, //Optional when deploying
                UseWPP_CopyWebApplication: true,
                PipelineDependsOnBuild: false,

                DeployOnBuild: true, //Trigger the deploy
                PublishProfile: buildOptions.publishProfile,
                EnableMSDeployAppOffline: true,
                AllowUntrustedCertificate: true, //For Web deploy
                UserName: argv.user, //User name
                Password: argv.pwd //Password
            },
        })
        );
});



Property name
Description
Value
DeployOnBuild

true
PublishProfile
Profile name

EnableMSDeployAppOffline
Make sure placing App_Offline.htm in the root directory of your web application but exclude it from .csproj.
boolean
AllowUntrustedCertificate
Allow untrusted certificate.
true
UserName
The user name for publish profile

Password
The password for publish profile


Tips. Notice that you can set the above information in the publish profiles as well.
For example,

<PropertyGroup>
    <EnableMSDeployAppOffline>True</EnableMSDeployAppOffline>
</PropertyGroup>



Finally, we can trigger a web deploy to remote server like this,

$ gulp msbuild --user=jb --pwd=xxxxx




Reference




沒有留言:

張貼留言