Add Package to dotnet core project at build time Azure Pipelines - azure-devops

I am setting up an Azure Build pipeline to automatically build when a NuGet package get's published. In that build definition - I want to install the latest version of the package.
According to this answer, I need to restore packages first to authenticate against Dev Ops feeds.
I have set-up a build.yml file to include these tasks:
- task: DotNetCoreCLI#2
displayName: 'Restore Packages'
inputs:
command: restore
projects: web/*.csproj
vstsFeed: organizational-packages
- task: DotNetCoreCLI#2
displayName: Install Custom Internal Package Package
inputs:
command: 'custom'
custom: 'add'
arguments: 'package Custom.Internal.Package --prerelease'
projects: 'Web/*.csproj'
feedsToUse: 'select'
feedRestore: 'organizational-packages'
The NuGet feed is hosted in Azure Dev ops and has organizational scope.
When I go to run the build - the Azure Package feed is not included in the search for the package:
info : Adding PackageReference for package 'Custom.Internal.Package' into project
'/home/vsts/work/1/s/Project.Web/Web.csproj'.
info : GET https://api.nuget.org/v3/registration5-gz-semver2/custom.internal.package/index.json
info : NotFound https://api.nuget.org/v3/registration5-gz-semver2/cbh.surescripts.business/index.json 202ms
error: There are no versions available for the package 'Custom.Internal.Package'.
The package is located at https://myorg.pkgs.visualstudio.com/_packaging/organizational-packages/nuget/v3/index.json
I even tried accomplishing this with just the command line tasks:
- task: NuGetAuthenticate#0
displayName: Authenticate NuGet Feed
- task: CmdLine#2
displayName: Add Organizational Package Source
inputs:
script: 'dotnet nuget add source https://myorg.pkgs.visualstudio.com/_packaging/organizational-packages/nuget/v3/index.json -n credible'
- task: CmdLine#2
displayName: Install Custom Internal Package
inputs:
script: 'dotnet add package Custom.Internal.Package--prerelease'
workingDirectory: '$(Build.SourcesDirectory)/Web'
In the later case the package source is added - but I am getting an unauthorized error:
info : Adding PackageReference for package 'Custom.Internal.Package' into project '/home/vsts/work/1/s/Project.Web/Web.csproj'.
info : GET https://api.nuget.org/v3/registration5-gz-semver2/custom.internal.package/index.json
info : NotFound https://api.nuget.org/v3/registration5-gz-semver2/custom.internal.package/index.json 64ms
error: Unable to load the service index for source https://myorg.pkgs.visualstudio.com/_packaging/organziational-packages/nuget/v3/index.json.
error: Response status code does not indicate success: 401 (Unauthorized).

There is no feedsToUse in custom command in DotNetCoreCLI task, you could remove it, and try to add -v|--version <VERSION> to version the package:
- task: DotNetCoreCLI#2
displayName: 'dotnet custom'
inputs:
command: custom
projects: 'Web/*.csproj'
custom: add
arguments: 'package Custom.Internal.Package --prerelease -v 1.0.0'

Related

Azure pipeline - install module from artifact

I am creating a pipeline on Azure devOps to deploy an angular application. Inside the project dependencies there is a library published on the artifact of another project (under the same organization). How to configure my yaml file so that only that library is installed from the artifact registry?
This is my yaml file
pool:
name: Azure Pipelines
steps:
- task: NodeTool#0
displayName: 'Use Node 14.x'
inputs:
versionSpec: 14.x
- task: Npm#1
displayName: 'npm install angular-cli'
inputs:
command: custom
verbose: false
customCommand: 'install -g #angular/cli#12.1.1'
- **TASK TO INSTALL MY LIBRARY FROM ARTIFACT**
- task: Npm#1
displayName: 'npm install'
inputs:
verbose: false
- task: Npm#1
displayName: 'create build'
inputs:
command: custom
verbose: false
customCommand: 'run build'
- ...task to deploy
You can set the Artifact feed containing your required npm packages as upstream sources for the current project Artifact feed. To add upstream sources to your feed you can refer to Configure upstream sources.
Then you can add the packages you need to the dependencies of your package.json. And use npm task to install the packages.

Azure pipeline for Azure Function does not find project

I've got a build pipe for an Azure Function using .Net Core 3.1.x. All the steps until the publishing are doing fine. I can get the publish step working by using script, but not through the yaml task. What am I missing?
Script (works)
- script: dotnet publish --configuration Release .\af-process-mds-vehicle-output-to-deviation\af-process-mds-vehicle-output-to-deviation.csproj
Task (does not work)
- task: DotNetCoreCLI#2
displayName: 'Publish Project'
inputs:
command: 'publish'
configuration: 'Release'
projects: '.\af-process-mds-vehicle-output-to-deviation\af-process-mds-vehicle-output-to-deviation.csproj'
zipAfterPublish: true
It doesn't find the project.
Here's the error message.
2021-10-29T05:21:44.3024816Z ##[section]Starting: dotnet publish
2021-10-29T05:21:44.3150367Z ==============================================================================
2021-10-29T05:21:44.3150726Z Task : .NET Core
2021-10-29T05:21:44.3151190Z Description : Build, test, package, or publish a dotnet application, or run a custom dotnet command
2021-10-29T05:21:44.3151475Z Version : 2.187.0
2021-10-29T05:21:44.3151733Z Author : Microsoft Corporation
2021-10-29T05:21:44.3152035Z Help : https://learn.microsoft.com/azure/devops/pipelines/tasks/build/dotnet-core-cli
2021-10-29T05:21:44.3152373Z ==============================================================================
2021-10-29T05:21:44.7797987Z [command]C:\Windows\system32\chcp.com 65001
2021-10-29T05:21:44.7903026Z Active code page: 65001
2021-10-29T05:21:44.7927221Z Info: .NET Core SDK/runtime 2.2 and 3.0 are now End of Life(EOL) and have been removed from all hosted agents. If you're using these SDK/runtimes on hosted agents, kindly upgrade to newer versions which are not EOL, or else use UseDotNet task to install the required version.
2021-10-29T05:21:44.8938257Z ##[error]No web project was found in the repository. Web projects are identified by presence of either a web.config file, wwwroot folder in the directory, or by the usage of Microsoft.Net.Web.Sdk in your project file. You can set Publish web projects property to false (publishWebProjects: false in yml) if your project doesn't follow this convention or if you want to publish projects other than web projects.
2021-10-29T05:21:44.9001249Z Info: Azure Pipelines hosted agents have been updated and now contain .Net 5.x SDK/Runtime along with the older .Net Core version which are currently lts. Unless you have locked down a SDK version for your project(s), 5.x SDK might be picked up which might have breaking behavior as compared to previous versions. You can learn more about the breaking changes here: https://learn.microsoft.com/en-us/dotnet/core/tools/ and https://learn.microsoft.com/en-us/dotnet/core/compatibility/ . To learn about more such changes and troubleshoot, refer here: https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/build/dotnet-core-cli?view=azure-devops#troubleshooting
2021-10-29T05:21:44.9003648Z ##[error]Project file(s) matching the specified pattern were not found.
2021-10-29T05:21:44.9182124Z ##[section]Finishing: dotnet publish
After the tips from the answer I got the pipe working. Here's the full working pipe. (Still don't know why it didn't work earlier.)
Working pipe:
name : af-vehicle-sync-to-deviation
## if there is a change is the deviation folder for the main branch. Then trigger.
trigger:
branches:
include:
- main
paths:
include:
- af-process-mds-vehicle-output-to-deviation/*
pool:
vmImage: 'windows-latest'
variables:
buildConfiguration: 'Release'
SolutionPath: '**\*.sln'
stages:
- stage: Build
displayName: Build solution
jobs:
- job: Build
displayName: Build and publish solution
steps:
- checkout: self
- task: NuGetToolInstaller#1
- task: NuGetCommand#2
inputs:
restoreSolution: $(SolutionPath)
- task: UseDotNet#2
inputs:
packageType: 'sdk'
version: '3.1.x'
displayName: 'Use .NET Core SDK 3.1.x'
- task: DotNetCoreCLI#2
inputs:
command: 'build'
configuration: $(buildConfiguration)
projects: '$(SolutionPath)'
displayName: 'Build solution'
- task: DotNetCoreCLI#2
displayName: 'Publish Project'
inputs:
command: 'publish'
configuration: 'Release'
projects: '**\*.csproj'
publishWebProjects: false
zipAfterPublish: true
arguments: '--output $(Build.ArtifactStagingDirectory)/$(buildConfiguration)'
- task: PublishBuildArtifacts#1
inputs:
PathtoPublish: '$(Build.ArtifactStagingDirectory)'
ArtifactName: 'drop'
publishLocation: 'Container'
You could use '**/*.csproj' but honestly, I would do something like this answer and add a script to list out all the files and folders recursively before this step that fails.
Assuming that you have a restore or build step before this publish you could add it after those, or just as the first step after your checkout one.
You can also inspect the logs of earlier steps to see the file path/s., instructions on doing this are available here.
Using $(System.DefaultWorkingDirectory) as your root is also recommended, rather than .\, so you would have '$(System.DefaultWorkingDirectory)\af-process-mds-vehicle-output-to-deviation...'.
Edit
If you look at the logs for your build step you will see entries like /home/vsts/work/1/s/XXX.YYY.ZZZ/XXX.YYY.ZZZ.csproj that refer to the different projects inside your solution. By default most commands will be run in $(System.DefaultWorkingDirectory) which would equate to /home/vsts/work/1/s/ in this instance, you can think of it as the root of your repository - there is more information on this structure here.
The error you were encountering is actually about the lack of a web project, rather than a path issue though, for the build step it is best practice to use the --output <output-directory-here> flag to output the compile files into a specific folder, that way you can easily publish that folder.

Azure DevOps Pipeline issue

I am struggling to get a simple build and deploy working and was hoping for some assistance. Could anyone review the steps and also why the Publish Artifacts does not work? It's a simple Angular 7 project.
Error:
[section]Starting: Publish Artifact: dist
========================================================================== Task: Publish Build Artifacts Description: Publish build
artifacts to Azure Pipelines/TFS or a file share Version:
1.142.2 Author : Microsoft Corporation Help : More Information
[warning]Directory 'D:\a\1\s\dist' is empty. Nothing will be added to build artifact 'dist'.
[section]Finishing: Publish Artifact: dist
YAML:
pool:
vmImage: Hosted VS2017
demands: npm
steps:
- script: |
echo Write your commands here
mkdir dist
echo Use the environment variables input below to pass secret variables to this script
displayName: 'Command - mkdir dist'
- task: Npm#1
displayName: 'npm install'
inputs:
verbose: false
- task: Npm#1
displayName: 'npm build'
inputs:
command: custom
verbose: false
customCommand: 'build --prod'
- task: PublishBuildArtifacts#1
displayName: 'Publish Artifact: dist'
inputs:
PathtoPublish: dist
ArtifactName: dist
- task: FtpUpload#1
displayName: 'FTP Upload: dist'
inputs:
credentialsOption: inputs
serverUrl: ‘xxx’
username: Tester2
password: 'Tester$2'
rootDirectory: dist
filePatterns: '*'
remoteDirectory: /
trustSSL: true
Azure DevOps Pipeline issue
The Publish Build Artifacts task is used to publish build artifacts to Azure Pipelines, TFS, or a file share.
But, just like Daniel and Andrey said, although you add the npm build, you did not set the installed folder to be dist. So the result of npm build will not be saved in the dist folder. In this case, the folder dist is empty.
Besides, to save the build result to the dist folder, you can try to use the option -- -op like following:
run ng build --prod -- -op ..\..\dist
The ..\..\dist should use relative path based on the project.json file.
Check the document JavaScript frameworks: AngularJS for some more details info.
Hope this helps.

How to generate specific version of nuget package from azure pipelines using yaml configuration

I have a build configuration defined here:
https://github.com/cpoDesign/APIFramework/blob/master/azure-pipelines.yml
I have managed to generate a nuget package using the following command
- task: DotNetCoreCLI#2
inputs:
command: pack
projects: '**/*ApiFramework.csproj'
A subset of the task output of the script is
Task "PackTask"
2018-11-27T23:02:32.4459067Z Successfully created package '/home/vsts/work/1/a/CPODesign.ApiFramework.1.0.0.nupkg'.
Next step resolved:
I do not want to create a build with release into nuget as those steps have to be logically separated. Therefore I have created a new step Create a drop.
Configuration:
My drop task definition:
- task: PublishBuildArtifacts#1
inputs:
pathtoPublish: '$(Build.ArtifactStagingDirectory)'
artifactName: drop
contents: '**/$(BuildConfiguration)/**/?(*.nupkg)'
Build output:
2018-11-27T23:04:24.6351310Z ##[section]Starting: PublishBuildArtifacts
2018-11-27T23:04:24.6353582Z ==============================================================================
2018-11-27T23:04:24.6353896Z Task : Publish Build Artifacts
2018-11-27T23:04:24.6353944Z Description : Publish build artifacts to Azure Pipelines/TFS or a file share
2018-11-27T23:04:24.6354007Z Version : 1.142.2
2018-11-27T23:04:24.6354046Z Author : Microsoft Corporation
2018-11-27T23:04:24.6354091Z Help : [More Information](https://go.microsoft.com/fwlink/?LinkID=708390)
2018-11-27T23:04:24.6354156Z ==============================================================================
2018-11-27T23:04:26.1357631Z ##[section]Async Command Start: Upload Artifact
2018-11-27T23:04:26.1357755Z Uploading 1 files
2018-11-27T23:04:26.6373194Z File upload succeed.
2018-11-27T23:04:26.6373313Z Upload '/home/vsts/work/1/a' to file container: '#/1558454/drop'
2018-11-27T23:04:27.9231805Z Associated artifact 91 with build 806
2018-11-27T23:04:27.9231947Z ##[section]Async Command End: Upload Artifact
2018-11-27T23:04:27.9232436Z ##[section]Finishing: PublishBuildArtifacts
Note: The UI for azure-devops has changed and the artefacts (artifacts) are no longer created as a new tab but rather badly added into the report summary
The question:
How do I generate a specific version of nuget package IE: 1.0.%(Build.BuildId)?
My last attempt is
- task: DotNetCoreCLI#2
inputs:
command: pack
projects: '**/*ApiFramework.csproj'
# packageVersion:'1.0.$(Build.BuildId)'
where
packageVersion:'1.0.$(Build.BuildId)'
Will cause the build to fail
(the current branch is published here:https://github.com/cpoDesign/APIFramework/blob/cpoDesign-build-mods-1/azure-pipelines.yml)
after frustrating couple hours I have found the answer
Publish the generated nuget package into build artefacts
Generate a release to publish the nuget package
Build configuration
pool:
vmImage: 'Ubuntu 16.04'
variables:
buildConfiguration: 'Release'
steps:
- script: dotnet build --configuration $(buildConfiguration)
displayName: 'dotnet build $(buildConfiguration)'
- task: DotNetCoreCLI#2
inputs:
command: restore
projects: '**/*.csproj'
- script: dotnet test **/*.Tests.Unit.csproj --logger trx
- task: PublishTestResults#2
inputs:
testRunner: VSTest
testResultsFiles: '**/*.trx'
- script: dotnet pack /p:PackageVersion='1.0.$(Build.BuildId)' --configuration $(buildConfiguration) -o $(Build.ArtifactStagingDirectory)
- task: PublishBuildArtifacts#1
inputs:
pathtoPublish: '$(Build.ArtifactStagingDirectory)'
artifactName: drop
contents: '**/$(BuildConfiguration)/**/?(*.nupkg)'
Release section
I have updated the release to trigger after each successful build
dotnet nuget push artefactName.APIFramework/drop/CPODesign.ApiFramework.1.0.$(Build.BuildId).nupkg -k $(myapiKey) -s https://api.nuget.org/v3/index.json
I can't see the contents as a valid input in the YAML for PublishBuildArtifacts#1
Do you mean to do a copy task first before you do the publish task. Like shown in the publish documentation?
https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/publish-build-artifacts?view=vsts

##[error]Project file(s) matching the specified pattern were not found

I am having problems with a build pipeline.
The agent pools is hosted VS2017
The YAML is
pool:
vmImage: 'VS2017-Win2016'
variables:
buildConfiguration: 'Debug'
steps:
- task: DotNetCoreInstaller#0
displayName: 'Use .NET Core sdk 2.1.5'
inputs:
version: 2.1.403
- task: DotNetCoreCLI#2
displayName: Restore
inputs:
command: restore
projects: '**/Api*.csproj'
#Your build pipeline references an undefined variable named ‘Parameters.projects’. Create or edit the build pipeline for this YAML file, define the variable on the Variables tab. See https://go.microsoft.com/fwlink/?linkid=865972
#Your build pipeline references the ‘BuildConfiguration’ variable, which you’ve selected to be settable at queue time. Create or edit the build pipeline for this YAML file, define the variable on the Variables tab, and then select the option to make it settable at queue time. See https://go.microsoft.com/fwlink/?linkid=865971
- task: DotNetCoreCLI#2
displayName: Build
inputs:
projects: '$(Parameters.projects)'
arguments: '--configuration $(BuildConfiguration)'
#Your build pipeline references an undefined variable named ‘Parameters.projects’. Create or edit the build pipeline for this YAML file, define the variable on the Variables tab. See https://go.microsoft.com/fwlink/?linkid=865972
#Your build pipeline references the ‘BuildConfiguration’ variable, which you’ve selected to be settable at queue time. Create or edit the build pipeline for this YAML file, define the variable on the Variables tab, and then select the option to make it settable at queue time. See https://go.microsoft.com/fwlink/?linkid=865971
- task: DotNetCoreCLI#2
displayName: Publish
inputs:
command: publish
publishWebProjects: false
projects: '$(Parameters.projects)'
arguments: '--configuration $(BuildConfiguration) --output $(build.artifactstagingdirectory)'
- task: PublishBuildArtifacts#1
displayName: 'Publish Artifact'
inputs:
PathtoPublish: '$(build.artifactstagingdirectory)'
When it runs, the build task has the following log
2018-10-29T18:28:43.7087338Z ##[section]Starting: Build
2018-10-29T18:28:43.7093502Z ==============================================================================
2018-10-29T18:28:43.7093580Z Task : .NET Core
2018-10-29T18:28:43.7093785Z Description : Build, test, package, or publish a dotnet application, or run a custom dotnet command. For package commands, supports NuGet.org and authenticated feeds like Package Management and MyGet.
2018-10-29T18:28:43.7093818Z Version : 2.141.0
2018-10-29T18:28:43.7093864Z Author : Microsoft Corporation
2018-10-29T18:28:43.7093895Z Help : [More Information](https://go.microsoft.com/fwlink/?linkid=832194)
2018-10-29T18:28:43.7093925Z ==============================================================================
2018-10-29T18:28:44.4833128Z [command]C:\Windows\system32\chcp.com 65001
2018-10-29T18:28:44.4926077Z Active code page: 65001
2018-10-29T18:28:45.1965225Z ##[error]Project file(s) matching the specified pattern were not found.
2018-10-29T18:28:45.2037015Z ##[section]Finishing: Build
I note that the log refers to 2.141.0 where as I am restoring the latest SDK 2.1.403 Why would that be? Could it be that the hosted VS2017 agent does not support the latest version of .netcore?
[Update]
I added a variable for Parameters.projects
However the build task has an error still.
2018-10-29T21:07:38.6774331Z ##[section]Starting: Build
2018-10-29T21:07:38.6781540Z ==============================================================================
2018-10-29T21:07:38.6781632Z Task : .NET Core
2018-10-29T21:07:38.6781676Z Description : Build, test, package, or publish a dotnet application, or run a custom dotnet command. For package commands, supports NuGet.org and authenticated feeds like Package Management and MyGet.
2018-10-29T21:07:38.6781762Z Version : 2.141.0
2018-10-29T21:07:38.6781807Z Author : Microsoft Corporation
2018-10-29T21:07:38.6781853Z Help : [More Information](https://go.microsoft.com/fwlink/?linkid=832194)
2018-10-29T21:07:38.6781915Z ==============================================================================
2018-10-29T21:07:39.5030735Z [command]C:\Windows\system32\chcp.com 65001
2018-10-29T21:07:39.5157531Z Active code page: 65001
2018-10-29T21:07:39.5840366Z ##[error]Project file(s) matching the specified pattern were not found.
2018-10-29T21:07:39.5916864Z ##[section]Finishing: Build
Posting just in case this helps anyone in the future
I had my vmImage set as ubuntu with the following set:
projects: '**\*.csproj'
The backslash was throwing things off, I had to switch to the following:
projects: '**/*.csproj'
I copied from a guide that was assuming Windows. Subtle enough, it took me a while to notice 😅
You need to define in the build task which .csproj files you want to build.
In your case, the definition is in a variable $(Parameters.projects).
- task: DotNetCoreCLI#2
displayName: Build
inputs:
projects: '$(Parameters.projects)'
arguments: '--configuration $(BuildConfiguration)'
You can replace this variable in your .csproj (e.g. **/*.csproj for all .csproj files in all subfolders):
- task: DotNetCoreCLI#2
displayName: Build
inputs:
projects: '**/*.csproj'
arguments: '--configuration $(BuildConfiguration)'
Or go to the variables tab and add the Parameters.projects variable:
I am trying to build and publish a multiproject solution in dotnet core.
The solution structure is:
sampleTest.API
sampleTest.API.csproj
sampleTest.Data
sampleTest.Data.csproj
sampleTest.Identity
sampleTest.Identity.csproj
sampleTest.Web
sampleTest.Web.csproj
sampleTest.sln
Now, I just want to build the sampleTest.API dotnet core project. I just need to define in the build task, the path to the project as shown below.
The yaml file for build task for the pipeline in Azure Deveops is shown below:
steps:
- task: DotNetCoreCLI#2
displayName: Build
inputs:
projects: 'sampleTest.API/*.csproj'
arguments: '--configuration $(BuildConfiguration)'
If we need to build based on searching for all the 'cs.proj' files - we do
projects: '**/*.csproj'
Sample build task using the classic Editor in Azure Devops is shown below: