Jenkins Symbolic Link Script Syntax - jenkins

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\""
}
}
}

Related

Jenkins Parameterized with batch commands or powershell script

I want to add to a pipeline parameters of folders and files in a directory on an agent.
e.g when I click on Build with Parameters it will show my checkbox of all the folders&files at c:\project and then I can choose which files I want for the job.
I try using plugin Active Choices Parameter, and to run a groovy script
node (node1){
stage('folders'){
bat "dir /b /s c:\\project"
}
}
I've tried also with powershell script
Get-ChildItem -path c:/project1 -Recurse -Name
You can do something like below. I don't have WIndows to test. But this should work on Windows as well. Just provide the correct Path.
pipeline {
agent any
parameters { choice(name: 'CHOICES', choices: listFiles("/var/jenkins_home/test"), description: '') }
stages {
stage('Test') {
steps {
echo "Run!!!"
}
}
}
}
#NonCPS
def listFiles(def path) {
def files= []
new File(path).traverse(type: groovy.io.FileType.FILES) { file ->
files.add(file)
}
return files
}

Jenkins build failed due to command not being recognized

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.

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\""

How to set environment PATH in Jenkins Declarative Pipeline bat block

I'm trying to set the environment PATH variable in a Jenkins Declarative Pipeline and am trying to use the same in a bat block on a windows machine. (I'm trying to modify the path so that I can use the same to call an executable without explicitly specifying the path.)
The path does not get passed to the bat block for some reason.
Any pointers to what could be the issue is highly appreciated from all you experienced developers out there. Thanks in advance!
Following is my code.
pipeline {
agent { label 'docker' }
environment {
PATH = "/hot/new/bin:$PATH"
}
stages {
stage ('build') {
steps {
echo "PATH is: $PATH"
bat """
echo PATH is: %PATH%
"""
}
}
}
}
Output is as follows:
PATH is: /hot/new/bin:blah:blah:my_env_path_content_remianing
PATH is: blah:blah:blah:my_env_path_content_remianing
What about using this syntax to make groovy able to interpolate the variable ?
bat """
echo PATH is: ${env.PATH}
"""
Or like this:
bat "echo PATH is: ${env.PATH}"

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

Resources