I have created a pipeline in Jenkins like that:
pipeline {
agent any
stages {
stage('Clean') {
steps {
cleanWs()
}
}
stage('Checkout code') {
steps {
git branch: 'test', credentialsId: <credentialsId>, url: <githubUrl>
}
}
stage('Restore Nuget') {
steps {
bat '"C:\\Nuget\\nuget.exe" restore <ProjectName>.sln'
}
}
stage('Build') {
steps {
bat "\"${tool 'MSBuild'}\" <ProjectName>.sln /p:Configuration=Release;Platform=\"Any CPU\""
}
}
}
}
I have installed MSBuild plugin in Jenkins. In "Global Tool Configuration", I added msbuild path like that: C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Current\Bin\MSBuild.exe
In the build stage I am getting this error :
MSBUILD : error MSB1009: Project file does not exist.
When I check Jenkins workspace folder, I can see all project files (.sln and .csproj files). What am I doing wrong?
You can try MSBuild path looks like this:
C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Current\Bin\amd64\MSBuild.exe
I think MSBuild comes versioned. Can you check if you have the right path?
I have VS2019 as well and my MSBuild path looks like this:
C:\Program Files (x86)\MSBuild\15.0
Related
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!
My setup: main node runs on Linux and an agent on Windows. I want to compile a library on an agent, archive those artifacts and copy them on the main node to create a release togather with the Linux compiled binaries.
This is my Jenkinsfile:
pipeline {
agent none
stages {
stage('Build-Windows') {
agent {
dockerfile {
filename 'docker/Dockerfile-Windows'
label 'windows'
}
}
steps {
bat "tools/ci/build.bat"
archiveArtifacts artifacts: 'build_32/bin/mylib.dll'
}
}
}
post {
success {
node('linux') {
copyArtifacts filter: 'build_32/bin/mylib.dll', flatten: true, projectName: '${JOB_NAME}', target: 'Win32'
}
}
}
}
My problem is, when I run this project for the first time, I get the following error
Unable to find project for artifact copy: mylib
But when I comment the copyArtifacts block and rerun the project, it is successful and I have artifacts vivible in the project overview. After this I can reenable the copyArtifacts and then the artifacts will be copied as expected.
How to configure the pipeline so it can access the artifacts on the initial run?
The copyArtifacts capability is usually used to copy artifacts between different builds and not between agents on the same build. Instead, to achieve what you want you can use the stash and unstash keywords which are designed exactly for passing artifacts from different agents in the same pipeline execution:
stash: Stash some files to be used later in the build.
Saves a set of files for later use on any node/workspace in the same Pipeline run. By default, stashed files are discarded at the end of a pipeline run
unstash: Restore files previously stashed.
Restores a set of files previously stashed into the current workspace.
In your case it can look like:
pipeline {
agent none
stages {
stage('Build-Windows') {
agent {
dockerfile {
filename 'docker/Dockerfile-Windows'
label 'windows'
}
}
steps {
bat "tools/ci/build.bat"
// dir is used to control the path structure of the stashed artifact
dir('build_32/bin'){
stash name: "build_artifact" ,includes: 'mylib.dll'
}
}
}
}
post {
success {
node('linux') {
// dir is used to control the output location of the unstash keyword
dir('Win32'){
unstash "build_artifact"
}
}
}
}
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 can I use MSBuild in a Jenkins multi-branch project?
Here's my Jenkinsfile:
pipeline {
agent any
stages {
stage('restore') {
steps {
sh "echo 'TODO RUN TEST'"
}
}
stage('build') {
steps {
bat "\"${tool 'MSBuild'}\" .\\src\\MySollutionName.sln /p:Configuration=Release /p:Platform=\"Any CPU\" /p:ProductVersion=1.0.0.${env.BUILD_NUMBER}"
}
}
stage('test') {
steps {
sh "echo 'TODO RUN TEST'"
}
}
}
}
But I'm getting an error message:
No tool named MSBuild found
Is it possible to use MSBuild in Jenkins multi-branch project?
You need to first tell Jenkins what the MSBuild tool is:
https://wiki.jenkins.io/display/JENKINS/Custom+Tools+Plugin
Have you installed the MSBuild plugin?
https://wiki.jenkins.io/display/JENKINS/MSBuild+Plugin
I am trying to build a Jenkins pipeline for my company to pull from Gitlab, do a MSBuild, Publish Artifacts to a directory and then zip up all the artifacts to push to a repository or file share. The application is a web app.
My issue occurs when I try to implement the zip function from AntBuilder. No matter what I specify for my basedir, the job fails saying that directory is not found. My code is as follows.
#!groovy
node('dotnet') { //select any node with a doctnet label
stage('Checkout') { //checkout the code from source
checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], gitTool: 'Windows_Gtt', submoduleCfg: [], userRemoteConfigs: [[credentialsId: '99999999-9999-9999-99999', url: 'git#repos.somecompany.com:api/sharedservices.git']]])
}
stage('Build') { //build steps go here
bat 'nuget restore LandingPageSvc.sln' //restore nuget sources
bat "\"${tool 'MSBUILD46_64_Pipeline'}\" LandingPageSvc.sln /m /p:VisualStudioVersion=14.0" //msbuild
}
// stage('NUnit Tests') { //Unit testing steps go here
// bat '"C:\\Program Files (x86)\\NUnit.org\\nunit-console\\nunit3-console.exe" ./LandingPageSvc.Test/bin/Debug/LandingPageSvc.Tests.dll --result nunit-result.xml'
// }
stage('Packaging') {
bat "\"${tool 'MSBUILD46_64_Pipeline'}\" LandingPageSvc\\LandingPageSvc.csproj /m /p:VisualStudioVersion=14.0;Configuration=Debug;PublishDestination=..\\Publish\\Dev /t:PublishToFileSystem"
def ant = new AntBuilder()
ant.zip(destfile: "test.zip", basedir: "./Publish/Dev")
}
stage('Publish') {
// build nuget package
//push nuget pcakage to artifactory
}
}
So, everything else resolves to the workspace on the dotnet build agent just fine and I have even tried using ${WORKSPACE} in the path and it still complains.
Not sure why or what context this particular function is running under, but it appears to not be on the dotnet build agent or in the project workspace.
Appreciate any suggestions.
Could it be that you are referencing two different file locations? In one you use ..\Publish\Dev and in the other you use ./Publish/Dev. I think the "." and the ".." are going to resolve to two different file locations. The ".." refers to the parent folder of the folder you are in whereas the "." refers to the folder you are in.
Jenkins/ProjectBasedir/Publish/Dev (.)
vs
Jenkins/Publish/Dev (..)