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
Related
I have this build error saying pandoc command is not recognize, when I build my pipeline on Jenkins :
But when I run the exact same command using cmd.exe from the same repository it works perfectly :
So what's wrong here, my command pandoc is well installed and can perfectly be used from cmd.exe, why doesn't it works from Jenkins ?
Here is my Jenkins code (the part causing the error is in the "Build" stage):
pipeline {
agent any
stages {
stage('Prerequisites') {
steps {
//bat 'RMDIR C:\\wamp64\\www\\html\\doc'
bat 'MKDIR C:\\wamp64\\www\\html\\doc'
}
}
stage('Build') {
steps {
bat 'pandoc -s C:\\wamp64\\www\\index.md -o C:\\wamp64\\www\\index.html'
bat 'pandoc -s C:\\wamp64\\www\\index.md -o C:\\wamp64\\www\\index.docx'
}
}
stage('Deploy') {
steps {
bat 'COPY C:\\wamp64\\www\\index.html COPY C:\\wamp64\\www\\html\\index.html'
bat 'COPY C:\\wamp64\\www\\index.docx COPY C:\\wamp64\\www\\html\\doc\\index.docx'
}
}
}
}
Thanks for helping.
Jenkins doesn't automatically take your Windows (path) environment variables. Instead, what you need to do is to go to Jenkins -> Configure System -> Global properties -> Environment variables and add a new variable called Path. For the value, set $Path, and your path variables should start getting registered.
The issue has been discussed extensively in this question.
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.
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!)
Can we build the IIB Bar file( using mqsicreatebar toolkit command) without IIB installation on jenkins box that has workspace code checked out ?
No, you have to install IIB before you can use mqsicreatebar.
Here the important bits of a Jenksfile for a job that runs on Windows:
steps {
script { currentBuild.result = 'SUCCESS' }
bat '''
call "C:/Program Files/IBM/IIB/10.0.0.14/server/bin/mqsiprofile.cmd" || exit /B 1
call mqsicreatebar ...
'''
}
I am trying to create a symbolic link in my JenkinsFile script for Windows but cannot get it to work as I am not sure about the syntax. Given below is the snippet I have currently in my script file:
node {
stage('Setup SymLink') {
def workspace = pwd()
dir("c:\\work") {
bat 'if exist "config" rmdir /s /q "config"'
bat 'mklink /D config "${workspace}#script\\config"'
}
}
}
The link target seems to have been created as:
C:\work\${workspace}#script\config
It doesn't seem to be resolving to the correct ${workspace}\config location and also prefixing with C:\work
Any idea of getting the correct syntax in Jenkins for this scenario? Thanks!!
Single-quoted strings in groovy don't support interpolation. You need to use one of the string literal syntaxes that does. For example:
node {
stage('Setup SymLink') {
def workspace = pwd()
dir("c:\\work") {
bat 'if exist "config" rmdir /s /q "config"'
bat "mklink /D config \"${workspace}#script\\config\""
}
}
}