Create publish zip or nuget for ASP.NET MVC project under azure CICD build pipeline

109 Views Asked by At

I am new to Azure build pipeline and some extent I have managed to create a build and publish artifact.

Issue: when build is ready and I am copying artifact and it goes to some weird path which I can see after downloading it.

Like ==> My.Web\Content\D_C\a\1\s\My.Web\obj\Release\Package\PackageTmp

I just need zip that contain publish version that can be directly copy to IIS.

trigger:
- '*'

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

steps:
- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    restoreSolution: '$(solution)'

- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    msbuildArgs: '/t:build /p:DeployOnBuild=true /p:OutputPath="$(Build.ArtifactStagingDirectory)"'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

- task: PublishPipelineArtifact@1
  inputs:
    targetPath: '$(build.artifactStagingDirectory)\_PublishedWebsites\My.Web_Package\My.Web.zip'
    publishLocation: 'pipeline'
    artifact: 'drop'
    name: 'WebArtifact-$(Build.BuildNumber)'

Let me know what wrong I am doing, Also under my solution there are multiple project library but I need single publish version for ASP.NET MVC 5 web project application which is in my solution .

Please guild if there is better way to write this pipeline.

Also let me know if we can add build number / version number for the same.

Really appreciate your any suggestion and input.

1

There are 1 best solutions below

0
SiddheshDesai On

The Artifact will be Published in the Default

The VS Build task will build your app similar to the Visual Studio that uses MSBuild to build your task.

Visual Studio:-

Ms Build is used to build the app:-

enter image description here

I built the app and published it into a local folder and this is how the build folder looks like:-

enter image description here

Azure Build Pipeline:-

The same thing happens in your Azure DevOps pipeline, The Build is done with Ms Build when you use Vs Build and the published artifact is stored in - $(Build.ArtifactStagingDirectory) > According to this MS Document: Build.ArtifactStagingDirectory is the predefined variables of Azure DevOps Agent that you are using to run the pipeline. As you are using windows-latest as an agent, the Build.ArtifactStagingDirectory routes to c:\agent_work\1\a. Thus it is getting stored to the default location above.

I have used below YAML pipeline to build the Asp.Net MVC 5 project and publish it as zip with published artifact name set to the build-number.

Reference for VsBuild here.

trigger:
- master

pool:
  vmImage: 'windows-latest'

variables:
  solution: '**/*.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

steps:
- task: NuGetToolInstaller@1

- task: NuGetCommand@2
  inputs:
    restoreSolution: '$(solution)'

- task: VSBuild@1
  inputs:
    solution: '$(solution)'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:DesktopBuildPackageLocation="$(build.artifactStagingDirectory)\WebApp.zip" /p:DeployIisAppPath="Default Web Site"'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

- task: VSTest@2
  inputs:
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

- task: PublishBuildArtifacts@1
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)\WebApp.zip'
    ArtifactName: 'WebArtifact-$(Build.BuildNumber)'
    publishLocation: 'Container'

Output:-

enter image description here

enter image description here

enter image description here

But, Instead of using VSbuild task, And as you need to manage multiple packages and libraries, I recommend using .NET Core CLI task to build your application and then publish it into IIS like below:-

trigger:
  branches:
    include:
      - main

jobs:
- job: Build
  displayName: 'Build and Publish'
  pool:
    vmImage: 'windows-latest'
  steps:
  - task: UseDotNet@2
    displayName: 'Install .NET Core SDK'
    inputs:
      version: '7.x'

  - task: DotNetCoreCLI@2
    displayName: 'Restore Dependencies'
    inputs:
      command: 'restore'
      projects: '**/*.csproj'

  - task: DotNetCoreCLI@2
    displayName: 'Build Project'
    inputs:
      command: 'build'
      projects: '**/*.csproj'
      arguments: '--configuration Release'

  - task: DotNetCoreCLI@2
    displayName: 'Publish Project'
    inputs:
      command: 'publish'
      projects: '**/*.csproj'
      publishWebProjects: true
      arguments: '--configuration Release --output $(Build.ArtifactStagingDirectory)'
      zipAfterPublish: true

  - task: PublishPipelineArtifact@1
    displayName: 'Publish Artifact'
    inputs:
      targetPath: '$(Build.ArtifactStagingDirectory)'
      artifactName: 'WebArtifact-$(Build.BuildNumber)'
      publishLocation: 'pipeline'

There is zipAfterPublish: true parameter which will zip the build artifact after its built is complete.

Output:-

enter image description here

enter image description here