How can i read and execute a file from the workspace in jenkins pipeline - jenkins

I'm trying to read and execute the file from workspace for a jenkins pipeline. I tried using "readFile" function but it is just reading the contents of the file. I want the contents to get executed.

You are probably looking for the 'sh' step: https://www.jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#sh-shell-script

Related

how to execute a bash script in a jenkins declarative pipeline

I have started recently using Jenkins in windows 10. I have a freestyle job that sync from the SCM, build a C++ solution and then it runs a batch script to upload to steam. I am trying to convert it to pipeline as I realized reading the documentation how much more powerful it is. My problem is that on the step to run the .bat file it gets stuck forever, this is the step:
...
Stage('batch script'){
steps{
bat 'start C:/Users/User/.jenkins/workspace/Project/Steam Build Scripts/scripts/build_dev.bat'
}
}
...
and this is the simple .bat file:
"C:\Users\User\.jenkins\workspace\Project\Steam Build Scripts\builder\steamcmd.exe" +login "someUser" "somePassword" +run_app_build "C:\Users\User\.jenkins\workspace\Project\Steam Build Scripts\scripts\app-build-813780-dev.vdf" +quit
running the same file from the freestyle job works fine like this:
I found out the way in case somebody has the same issue, due to the path containing spaces it has to be called like this:
bat ('call "C:/Users/User/.jenkins/workspace/Project/Steam Build Scripts/scripts/build_dev.bat"');

Reading config file in DSL build on agent host

I try to configure Jenkins' seed job, where whole business is in provided DSL script. I want to seperate that script from its configuration, which I want to locate in additional yml file. When I try to read that file:
#Grab('org.yaml:snakeyaml:1.17')
import org.yaml.snakeyaml.Yaml
def workDir = SEED_JOB.getWorkspace()
def config = new Yaml().load(("${workDir}/config.yml" as File).text)
I receive error
java.io.FileNotFoundException: /var/lib/jenkins/workspace/test.dsl/config.yml (No such file or directory)
I suppose that Jenkins is looking for the file on a master host, not an agent node where workspace is located.
Is it possible to read yml file in DSL build step on the agent node? Or maybe I have to execute that seed job always on my master host?
This seems not possible as the jobDsl script is executed on master. You can try force to run the job on master with label master.
From the documentation in section Script location:
Job DSL scripts are executed on the Jenkins master node, but the seed job's workspace which contains the script files may reside on a build node. This mean that direct access to the file specified by FILE may not be possible from a DSL script. See Distributed builds for details.

Copy file from Jenkins master to slave in Pipeline

I have some windows slave at my Jenkins so I need to copy file to them in pipeline. I heard about Copy To Slave and Copy Artifact plugins, but they doesn't have pipeline syntax manual. So I don't know how to use them in pipeline.
Direct copy doesn't work.
def inputFile = input message: 'Upload file', parameters: [file(name: 'parameters.xml')]
new hudson.FilePath(new File("${ENV:WORKSPACE}\\parameters.xml")).copyFrom(inputFile)
This code returns and error:
Caused: java.io.IOException: Failed to copy /var/lib/jenkins/jobs/_dev/jobs/(TEST)job/builds/107/parameters.xml to d:\Jenkins\workspace\_dev\(TEST)job\parameters.xml
Is there any way to copy file from master to slave in Jenkins Pipeline?
As I understand copyFrom is executed on your Windows node, therefore the source path is not accessible.
I think you want to look into the stash/unstash steps (Jenkins Pipeline: Basic Steps), which work across different nodes. Also this example might be helpful.
Pipeline DSL context runs on master node even that your write node('someAgentName') in your pipeline.
Try to use stash/unstash, but it is bad for large files.
Try External Workspace Manager Plugin. It has
pipelines steps and good for large files.
Try to use an intermediate storage. archive() and sh("wget $url") will be helpful.
If the requirement is to copy an executable to the test slave and to publish the test results, this is easy to do without the Copy to Slave plugin.
A shared folder should be created on each test slave (normal Windows shared folder).
After build: Build script copies the executable to the shared directory on each slave. A simple batch script using copy command is sufficient for this.
stage ('Copy to slaves') {
steps {
bat 'call "copy-to-slave.bat"'
}
}
During test: The test script copies the executable to another directory and runs it.
After test: Post-build action "Publish Robot Framework test results" can be used to report the test results. It is not necessary to copy the test result files back to the master first.
I recommend on Pipeline: Phoenix AutoTest plugin
Jenkins plugin website:
https://plugins.jenkins.io/phoenix-autotest/#documentation
GitHub repository of plugin:
https://github.com/jenkinsci/phoenix-autotest-plugin

jenkins pipeline unable to read files

I have a simple Jenkinsfile where I want to load some data from the workspace. I am using the pipeline plugin to leverage the Jenkinsfile inside of the repository. The build is farmed off to a matching Jenkins agent. When I try to use "readFile" I get the following message:
java.io.FileNotFoundException: /path/to/jenkins/workspace/XXXXX/project/data.json (No such file or directory)
I also get the same message when trying to load a Groovy file from the workspace.
My Jenkinsfile looks like:
node('master') {
stage "Start"
echo "Starting"
stage "Load File"
def myJson = readFile "data.json"
}
Any ideas why I can't read these files?
Thanks,
Tim
When Jenkins processes a Jenkinsfile it does not automatically pull down the entire source repository. You need to execute "checkout scm" to pull down the contents of the repository. If you fail to do so no other files will be available to the pipeline script.

setting Environment from a file in Jenkins

My question is there any way to set up environment variables from a file during Jenkins build. I have a file called .xyz which has env variables.
I know there is a jenkins plugin but can we do it inside the execute shell box in jenkins??
I tried . .xyz but that doesn't work!
Thanks in advance
If you want to do it from inside Execute Shell step, you've got to write a shell script that will read the file line by line and add them to environment variables.
Assuming your properties file is of format:
var=value
A very basic version of the script would be:
while read line; do
export $line
done <your_props_file_name
The problem is, these variables will only be retained for the duration of that "Execute Shell" build step. Once you move to a different Build step or Post-build step (within the same job), they will be gone. This is Jenkins's cleanup by design.
That's why there is that EnvInject plugin, it takes care of maintaining the environment variables between the build steps.

Resources