How to set nuget server apikey in TFS build server - tfs

Question
How to set nuget source api key, so it works with TFS Build running "Network Service"?
Scenario
I have TFS build server and Build Definition that executes as "Network Service".
during the build i'm trying to publish nuget packages:
<Exec Command='"nuget.exe" push "mypackages\*.nupkg" -source http://mynugetserver/'/>
however, I don't know how to set apikey for my nuget server.
What have I tried
nuget setapi "myapikey" -source http://mynugetserver/
when I log in to build server and runthis command It saves the apikey for current user and it is ignored in TFS build that runs as network service. build.proj publishes the packages when executed manually since the key is saved for current user, but it doesn't work when I queue new tfs build.
I have tried to create custom nuget.config in the root directory for my TFS project builds 'C:\Builds\1\MyProject\nuget.config` and run
nuget setapi "myapikey" -source http://mynugetserver/ -ConfigFile "C:\Builds\1\MyProject\nuget.config"
but the build ends with message:
"Key not valid for use in specified state"

You can set the apiKey with push command directly like following format:
nuget push <packages> <apiKey> -source <nuget server url>
You could also create an individual proj file with following contents and then create a build definition to build it once:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="SetApiKey">
<Exec Command='nuget.exe setapikey xxxxx -source '/>
</Target>
</Project>
After this, you should be able to push the packages without apikey under "Network Service" and you can delete the build definition and proj file. I just tried it under my environment and it works.

Related

Sonarqube ANT build azure devops for SAP Hybris

I am trying do sonarqube setup for SAP hybris, we use azure devops for Build and uses ant build to SAP hybris.
sonarqube is located in a seperate server backed by SQL DB and i confirmed it's working.
I want the ant build to communicate to sonarqube server.
In config--> local.properties i added the following sonarqube properties
sonar.projectName=xxx_xxxxxx_xx
sonar.jdbc.url=jdbc:sqlserver://Rxxxxxxxx02:1433;databaseName=soxxrxxxx;integratedSecurity=true
sonar.projectKey=xxx_xxxxxx_xxKey
sonar.projectVersion=1.0
sonar.excludedExtensions=acceleratorcms,acceleratorfacades,acceleratorservices,acceleratorstorefrontcommons,addonsupport,captchaaddon,commercefacades,platformservices
for SonarQube > 6.4
sonar.host.url=http://xxxxxxbldxx:9000
sonar.login=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
can anyone help me on how set this up??
Have you tried Measuring Code Quality with Sonar to configure SAP Commerce Cloud with SonarQube?
Also, when you use sonar.host.url, you don't need sonar.jdbc.url anymore.
the reason is that the ANT task shipped with the Hybris distribution is not compatible with Sonar version 8. In order to solve your problem there are two options:
a) Local workaround
Delete the JAR sonarqube-ant-task-XXX.jar in your platform folder ./platform/resources/ant/sonar/lib. Download a newer version (e.g. sonarqube-ant-task-2.7.0.1612.jar) and place it in the folder ./platform/resources/ant/sonar/lib.
Edit the file ./platform/resources/ant/sonar/sonar.xml and change the element for defining the sonar task. You have to include the new jar file:
<taskdef uri="antlib:org.sonar.ant" resource="org/sonar/ant/antlib.xml">
<classpath>
<path path="${platformhome}/resources/ant/sonar/lib/sonarqube-ant-task-2.7.0.1612.jar" />
</classpath>
</taskdef>
After applying this change, you should be able to run ant sonarcheck without the mentioned error.
b) CI-supported solution
If you want to automate your sonar check on a CI server (e.g. Jenkins), you have to override the sonar resources provided by the SAP platform distribution. A common way to do this is to use the customize approach:
create a customize folder in your config folder, e.g. ./hybris/config/customize
add the new jar and modified sonar.xml to this customize directory. So the structure will be:
./hybris/config/customize/platform/resources/ant/sonar/lib/sonarqube-ant-task-2.7.0.1612.jar
./hybris/config/customize/platform/resources/ant/sonar/sonar.xml
In your build job you have to ensure that you invoke this step:
ant customize
This target will override the resources in the platform distribution. It is the offial way stated by SAP to handle such customizations.
After you invoked the ant customize target on your build server, you can build the platform (ant clean all) and invoke the sonar check (ant sonarcheck).

Jenkins with msbuild

I am setting CI with .net projects but I having troubles when I try configure the restore using bath command line into Jenkins like this:
C:\nuget.exe restore "path\to\solution.sln"
Jenkins console throws "file not found" for the solution.sln in above command.
Both nuget.exe and the solution.sln on the path are correct.

environment specific db automation with tfs ci cd

I am trying automate db deployment using TFS CI/CD.
MSBuild for building the db which in turn creates dacpac
Winrm for db deployment to an on premise sql server
During build I am passing
MSBuild Arguments: /t:build /t:publish /p:SqlPublishProfilePath=db.publish.xml
Contents of db.publish.xml file
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<IncludeCompositeObjects>True</IncludeCompositeObjects>
<TargetDatabaseName>dev_DB</TargetDatabaseName>
<DeployScriptFileName>db.sql</DeployScriptFileName>
<TargetConnectionString>Data Source=xxxx;Persist Security Info=True;User ID=sa;password=xxx;Pooling=False;MultipleActiveResultSets=False;Connect Timeout=60;Encrypt=False;TrustServerCertificate=True</TargetConnectionString>
<ProfileVersionNumber>1</ProfileVersionNumber>
</PropertyGroup>
</Project>
My release is currently configured to dev. And QA.. the release is working fine for dev. And it is not working for QA.
One observation is: the SqlPublishProfilePath in build is referring to publish file which is referring to Dev.
I would like to use same dacpack file for dev. And QA. As I have mentioned specific publish file in the release task.
release configuration
In Publish Profile argument, usually specify the path to the Publish profile XML file on the target machine or on a UNC share that is accessible by the machine administrator's credentials.
It seems variable $(devpublishprofile) point to a path on your dev machine. You have two option to fix your issue:
Put the Publish profile in a UNC share, and specify variable $(devpublishprofile) to the UNC share where both Dev and QA can access.
Copy the Publish profile from Dev to QA, and locate it in the same path as variable $(devpublishprofile) points to.

TF unable to determine workspace

I get this error:
Unable to determine the workspace. You may be able to correct this by running 'tf workspaces /collection:TeamProjectCollectionUrl'.
I have checkout the workspace from Jenkins plugin (TFS-plugin). Then I run a MSBuild script where I want to checkout a file with this target:
<!-- Checkout before building -->
<Target Name="CheckoutUpdateVersion">
<Message Text="***** Checkout $(UpdateVersionFile) *****" />
<Exec WorkingDirectory="E:\WS\Jenkins\workspace\TD_-_Build_TFS\" Command="$(tf) checkout E:\WS\Jenkins\workspace\TD_-_Build_TFS\src\rubin\Scripts\update_version.sql" />
</Target>
I have checked so my workspace I mapped and it is mapped to the WorkingDirectory that is above.
Workspaces are tied to a machine and user. I suppose that the Jenkins agent is running with another user account which has no proper workspace definition.
Some tips:
you cannot use the same local directory in different workspaces
to define a workspace for build user account, logon using the latter or run-as Team Explorer (or run-as a Command Prompt and use TF to define the workspace)

How to get TFS2015 Build (Build.vnext) and NuGet package restore to use custom package sources

I'm trying to get our TFS2015 Build (TFSBuild vnext) going along with VS Team Services.
Thus far most blogs and documentation has been helpful, except for when trying to build a project that uses custom package sources for NuGet automatic package restore.
I've successfully deployed an a Team Services Build Agent (the new mechanism for builds) and all seemed to go well until the build task to Restore NuGet packages failed.
The failure was due to custom/private package source not being picked up from the installed VS configuration, so those packages could not be found. The traditional packages from the default NuGet package feed did restore without issue.
How do I specify the additional package sources for NuGet to use when using an agent?
EDIT: vcastro asked about the NuGet Installer build step and defining paths in the configuration of that build step. The above is using the Visual Studio Build step and the integrated option to have NuGet restore packages. Refer this image for reference:
Alternatively you could also add a NuGet Installer build step before the Visual Studio Build step in your build configuration to restore all NuGet packages.
There you can pass the location of your private repo as argument to nuget.exe:
-source "https://www.nuget.org/api/v2/;http://mynugetserver"
I've scrounged the web with little success, but after fiddling the following will help:
OK It seems that the package sources configured for NuGet.config is stored per user account, e.g.
c:\Users\<<username>>\AppData\Roaming\NuGet\NuGet.config
My issue was harder to resolve, because the build agent was running as a Windows Service under the Local System account. So to get NuGet configuration to for the build, I had to use the following path instead:
64-bit Windows C:\Windows\SysWOW64\config\systemprofile\AppData\Roaming\NuGet\NuGet.Config
32-bit Windows C:\Windows\System32\config\systemprofile\AppData\Roaming\NuGet\NuGet.Config
You may need to have elevated permissions in order to create the NuGet subfolder and NuGet.Config file.
Note: I have no solution for using the Local Service account. The above only works for the Local System (or an actual user) account.
Add a NuGet.config to your project that specifies an alternate package location. The rules for resolution are well-defined and explained in the official documentation.
There is a new VSTS Task called "NuGet Installer" this allows you to check in your NuGet.config file and specify the different package sources. Run this task before you run MSBuild.
If you are using the VSTS NuGet Feed you will need to add the build service account to the feed to enable downloading of packages https://www.visualstudio.com/get-started/package/use/common-identities
One solution (works for me) is change account for tfs 2015 build agent service (on my build machine VSO Agent tsf.Agent-PC) to tfsagent, for example, and add Nuget.config to the C:\Users\tfsagent\AppData\Roaming\Nuget. That's all!
Specify your custom NuGet feed URL’s in the solution’s nuget.config file. Do not store any usernames & passwords in this file.
<add key="nuget.org" value="https://www.nuget.org/api/v2/" />
<add key="MyCompany" value="https://nuget.mycompany.com:443/nuget" />
Create username & password variables in your build definition in VSTS. Variables can be encrypted and will not be displayed in any of the build log outputs. Here I'll create MyCompanyNugetUser and MyCompanyNugetPwd variables.
In our build steps, we add a Powershell script as the first action, this will read the username & password variables and update the user level nuget.config file on the build machine. Below is the code snippet from my inline Powershell script:
Arguments:
$(MyCompanyNugetUser) $(MyCompanyNugetPwd)
Script:
param($user, $pwd)
$nugetFile = "$ENV:AGENT_HOMEDIRECTORY\agent\worker\tools\nuget.exe"
Write-Output "Looking for nuget.exe in $nugetFile"
if (-not (Test-Path $nugetFile))
{
Write-Error "nuget.exe could not be located."
return
}
Write-Output "nuget.exe located"
$cmd = "$nugetFile sources add -name MyCompany -source https://nuget.mycompany.com:443/nuget -username $user -password $pwd -StorePasswordInClearText"
Write-Output $cmd
iex $cmd
Next, we just continue to execute the default NuGet Restore step from Microsoft’s templates
More here: https://codingcase.com/2016/07/27/vsts-build-setup-custom-nuget-feeds-with-authentication/
HTH
In the RTM of Team Foundation Server 2015 you have to add a build step of the type "NuGet Installer", and restore the packages of the Solution file before you run the actual build process. In this task you can pass the argument -ConfigFile path/to/nuget.config which contains your repository path.
For Example:
<configuration>
<packageSources>
<add key="Internal Nuget" value="\\srv-nuget\Repo" />
</packageSources>
</configuration>
If you are having trouble getting this to work on UWP ONLY, then ensure that you have the CASE of the package name spelt correctly. If the case is wrong, then (for UWP only) our build server fails the build.
for instance, if you have a package called Com.Company.Components and update the package using "install-package com.company.components" (note the case of the initial letter) then the UWP build on the build server may fail to find the package in your local store.

Resources