Compiling Visual Studio 2015 solutions from Jenkins declarative pipeline - jenkins

I have a simple declarative pipeline as follows:
pipeline {
/* continuous build pipeline for jenkins */
agent any
environment {
path_visualstudio = 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.exe'
path_solutionfile = 'c:\foo\bar.sln'
}
stages {
stage ('solution') {
steps {
echo 'building solution'
bat '${env.path_visualstudio} ${env.path_solutionfile} /rebuild'
}
}
}
}
I am unable to successfully start the devenv.exe because of the following error in the console output:
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
WorkflowScript: 5: unexpected char: '\' # line 5, column 26.
path_visualstudio = 'C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.exe'
^
1 error
Is this a matter of incorrect excaping the slashes, or am I always supposed to use forward slashes in Jenkins regardless of platform?

Actually, you have the answer in your question: escape the slashes with another one. Using backslash instead should also work (not tested!)

Related

jenkins pipeline docker is unable to delete msbuild obj binaries on build agent

I am trying to build my .net framework 4.7.2 project (a WCF and WPF application) using jenkins pipeline using docker image (mcr.microsoft.com/dotnet/framework/sdk:4.8-20220215-windowsservercore-ltsc2019) on my machine. This is a POC BTW.
The msbuild command is failing with the below error, I have tried to find solutions over the internet but the solutions I found aren't working for me. :-
"C:\ProgramData\Jenkins\.jenkins\workspace\MyLocalServicePipeline\MyLocalService.sln" (Rebuild target) (1) ->
"C:\ProgramData\Jenkins\.jenkins\workspace\MyLocalServicePipeline\MyWPFApp\MyWPFApp.csproj" (Rebuild target) (8) ->
(CleanupTemporaryTargetAssembly target) ->
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.WinFx.targets(480,10): error MSB3061:
Unable to delete file "obj\x64\Release\MyWPFApp.exe".
Access to the path 'C:\ProgramData\Jenkins\.jenkins\workspace\MyLocalServicePipeline\MyWPFApp\obj\x64\Release\MyWPFApp.exe' is denied.
[C:\ProgramData\Jenkins\.jenkins\workspace\MyLocalServicePipeline\MyWPFApp\MyWPFApp.csproj]
0 Warning(s)
1 Error(s)
jenkins pipeline code is as below :-
pipeline {
agent {
docker
{
image 'mcr.microsoft.com/dotnet/framework/sdk:4.8-20220215-windowsservercore-ltsc2019'
}
}
stages {
stage('Checkout') {
steps {
git url: 'https://lasnab82#bitbucket.org/lasnab82/mylocalservice.git'
}
}
stage('NugetRestore') {
steps {
bat 'nuget restore "%WORKSPACE%\\MyLocalService.sln"'
}
}
stage('Build') {
steps {
bat '"C:\\Program Files (x86)\\Microsoft Visual Studio\\2022\\BuildTools\\MSBuild\\Current\\Bin\\MSBuild.exe" "%WORKSPACE%\\MyLocalService.sln" /t:"Clean" /p:Configuration=Release /p:Platform="x64"'
bat '"C:\\Program Files (x86)\\Microsoft Visual Studio\\2022\\BuildTools\\MSBuild\\Current\\Bin\\MSBuild.exe" "%WORKSPACE%\\MyLocalService.sln" /t:"Rebuild" /p:Configuration=Release /p:Platform="x64"'
}
}
}
}
Logically, I think whats going wrong here is that the jenkins docker container agent may not have delete access to delete files which are actually checked out on my machine in jenkins workspace but I dont know how to resolve this access issue on my setup of jenkins.
Appreciate your help.
Thanks!

'.' is not recognized as an internal or external command for Gradle command in Jenkins when gradlew script is executed

I try to run SonarQube analysis for a Gradle project in a Jenkins Pipeline using the following code:
stage('SonarQube') {
withGradle {
withSonarQubeEnv('SonarQube Env') {
bat "./gradlew sonarqube"
}
}
}
The Gradle plugin is installed in Jenkins but I am getting the following error:
05:15:05 D:\*\*\*\*\*\*>./gradlew sonarqube
05:15:05 '.' is not recognized as an internal or external command,
Two things are incorrect in your code. On Windows machines you have to:
use backslashes instead of slashes in paths (./command → .\command)
execute script written for Windows (gradlew is a Unix script, gradlew.bat is a Windows script)
This code should work:
stage('SonarQube') {
withGradle {
withSonarQubeEnv('SonarQube Env') {
bat '.\\gradlew.bat sonarqube'
}
}
}
Gradle Wtapper by default is provided with two script gardlew and gradlew.bat. If your project doesn't have the gradlew.bat file, execute on your Unix machine ./gradlew wrapper. The missing file will be generated.
Btw. You don't need the Jenkins Gradle plugin, when you use Gradlew Wrapper. The plugin is required when you want to provide Gradle installations for jobs, example:
stage('SonarQube') {
withGradle {
withSonarQubeEnv('SonarQube Env') {
bat "${tool(name: 'toolId', type: 'gradle')}\\bin\\gradle.bat sonarqube"
}
}
}
toolId must much the identifiers used in the Jenkins Global Tool Configuration, examples: gradle-6.X, gradle-6.8.3 etc.

How to add a configuration parameter with spaces on Jenkins

I have a pipeline which builds a C++ project for that I am using MSBuild, untill now we were using the "Final" configuration parameter, but now I need to switch it to "Release Steam D3D11", when I try to do that I get an error on Jenkins when building the project I guess it is because of the spaces, how can I make Jenkins to take this parameter? this is what I have tried:
stage('Build'){
steps{
script {
def msbuild = tool name: 'MSBuild', type: 'hudson.plugins.msbuild.MsBuildInstallation'
bat "\"${msbuild}\" AoC/Source/project-GRDK.sln /t:Rebuild /p:configuration=Release Steam D3D11"
}
}
}
I have also tried adding ' ' to the configuration name such as :
bat "\"${msbuild}\" AoC/Source/project-GRDK.sln /t:Rebuild /p:configuration='Release Steam D3D11'"
but it does not work neither as I get this error:
00:00:01.407 MSBUILD : error MSB1008: Only one project can be specified.
I sorted it out in case somebody have the same issue, wrapping the string with spaces within \" \" does the job
bat "\"${msbuild}\" AoC/Source/age2-GRDK.sln /t:Rebuild /p:configuration=\"Release Steam D3D11\""

Jenkins Groovy Script not recognizing windows Drive

Using the following groovy script in my Jenkinsfile to do some file operations for preparing my build package:
pipeline {
agent any
stages {
stage('package-windows') {
when {
expression { isUnix() == false && env.JOB_NAME == 'my-job-webapi'}
}
steps {
bat label: 'unzip all files', script: 'FOR /R .\\archive %%I IN (*.zip) DO "C:\\Program Files\\7-Zip\\7z.exe" x "%%I" -aou -o"%%~dpI\\*"'
}
}
}
}
When i run the job its failing with the following error:
\Program was unexpected at this time.
C:\Program Files (x86)\Jenkins\workspace\my-job-webapi>FOR /R .\archive \Program Files\7-Zip\7z.exe" x "~dpI\*"[Pipeline] }
For some reason its unable to recognize the drive letter C: in the path "C:\\Program Files\\7-Zip\\7z.exe". What is the right way to provide the path with windows drive letter in Groovy script ? Or is there a different way this needs to be handled ?
Just needed to use / instead of \\. C:/Program Files/7-Zip/7z.exe worked

Using jenkins variables in Pipelane plugin

sorry for bad English
i`m trying to use my jenkins BuildNumber in my pipelane, but jenkins dont use it.
My pipe:
node {
stage ('Build')
{
bat '"C:/Program Files (x86)/Microsoft Visual Studio/2017/BuildTools/MSBuild/15.0/Bin/msbuild.exe" D:/rbp-maintenance/_buildscripts/Targets.proj /p:BuildNumber=${env.BUILD_NUMBER} /p:SolutionName=Maintenance.sln'
}
}
In the console i have some text:
D:\rbp-maintenance>"C:/Program Files (x86)/Microsoft Visual Studio/2017/BuildTools/MSBuild/15.0/Bin/msbuild.exe" D:/rbp-maintenance/_buildscripts/Targets.proj /p:BuildNumber=${env.BUILD_NUMBER} /p:SolutionName=Maintenance.sln
Also i`m try to use
env.BUILD_NUMBER
env.$BUILD_NUMBER
What i`m doing wrong?
Use double quotes to enable string interpolation
bat "\"C:/Program Files (x86)/Microsoft Visual Studio/2017/BuildTools/MSBuild/15.0/Bin/msbuild.exe\" D:/rbp-maintenance/_buildscripts/Targets.proj /p:BuildNumber=${env.BUILD_NUMBER} /p:SolutionName=Maintenance.sln"

Resources