I have a simple batch file. I want to call/run/execute that file from Jenkins.
Is there are plugin in Jenkins for the same ?
How can execute the batch file from Jenkins ?
If there is any tutorial or documentation for the same.
No need to add a new plugin for that, in Jenkins , select your job name and go to the configure section.
There is a Build section , on that section there is a combo box, select Execute windows Batch command on that text box you can specify either the batch file details directly or specify the file path.
While building the job the batch file will automatically executed.
On Linux, go to your jenkins job, go to configuration, add build step "execute shell", then type the name of your script.
Please be sure that your file is executable (chmod 777 yourscript.sh) and in the right place (checkout via GIT or SVN, please check your job workspace, your current dir for execute is the job dir, base of job workspace) and to have a valid shebang (first line in your script file e.g. #!/bin/sh).
If you are using Jenkins Pipeline, you can simply use bat step.
You can generate Groovy by clicking "Pipeline Syntax".
Go to your pipeline and click "Configure"
Go to the bottom and under pipeline script click"Pipeline Syntax"
Choose Sample Step - "Windows Batch Script"
Enter your Batch Script and click "Generate Groovy"
For example you can echo the systemdrive variable:
bat 'echo %systemdrive%'
Related
I am able to execute a shell command directly on Jenkins. However, when I try to run the same command via a script, it gives me the following error:
[test_pytest] $ C:\cygwin64\bin\bash.exe -xe C:\Windows\TEMP\jenkins1276820232141896422.sh
+ C:/ProgramData/Jenkins/.jenkins/workspace/Pytest_test/test.sh
C:\Windows\TEMP\jenkins1276820232141896422.sh: line 2: C:/ProgramData/Jenkins/.jenkins/workspace/Pytest_test/test.sh: No such file or directory
I have this file avilable in folder:
1.
from the picture,the file test.sh is a text file
you should first checkout extension of the file, make sure it's Shell Script, not the Text Document
see for example here
Open Windows File Explorer
Click the "View" tab. Tick the "File name extensions" option
and then right-click the file, choose rename, and enter the new file extension
2.
And try again on your jenkins.
I'm working on R&D project where my idea is to trigger a shell script from Jenkins and write the console output I get to a file (text file) placed in a remote windows server. I tried looking for options but none of them were helpful.
Is there any way to write the Jenkins console output to a text file located in the remote server using:
Any post build action or
any external Jenkins plugins or
Any other different method
Is there any connectivity access required to write the output as required? Kindly help me figure out a way.
I can think of several ways to achieve this:
When running your shell command, redirect it to file:
echo scripting output > myScriptOutput.txt
Then copy the file to a remote file share:
copy myScriptOutput.txt \\server\share\subfolder
(can be done in either freestyle or pipeline job).
Redirect output like above, then archive the artifacts, and use one of the Publish Over...Plugins to copy it to a remote server (can be done in a freestyle job only).
Configure your remote server as a Jenkins node. Then, use a pipeline script like:
node("myBuildNodeNameOrLabel") {
bat """echo scripting output > myScriptOutput.txt"""
stash includes: 'myScriptOutput.txt', name: 'myScriptOutput'
}
node("myRemoteServerNodeNameOrLabel") {
unstash 'myScriptOutput'
// copy the file to another local folder outside the workspace
bat """copy myScriptOutput.txt d:\\some\\other\\path"""
}
Same as above, but capture the script output using Groovy then manipulate it...
myOutput = bat returnStdout: true, script: """echo scripting output"""
// now do whatever you want with the Groovy var myOutput...
The Email-Ext Plugin can send Jenkins' Console Output via e-mail.
I found a way for this. We can use the Jenkins REST API and it will help us read the console output and we can write it to a file and do the necessary actions.
I am using Jenkins 2.46.1
I have a build pipeline plugin installed and I want to execute a Windows batch file in it. The batch file should execute in a new command window and not on jenkins console output. I give the below Jenkins pipeline groovy script:
node {
stage 'Init'
bat '''
call C:\\myprj\\mybat.bat stop
EXIT /B 0
'''
stage 'Deploy'
bat '''call C:\\myprj\\mybat.bat'''
}
In the init stage, I want to kill the process if it is already open and in stage deploy it should open a new command window and run my batch file.
The problem is that the above does not work. The build is succesful but no command window opens up. Pls suggest
Technically, to do what you're asking you should be able to run
bat 'start cmd.exe /c C:\\myprj\\mybat.bat'
This will launch a new command windows (cmd.exe) and run the batch file given. Depending how your Jenkins slave is running you may not see anything. (eg if it's running as a windows service or different user, you won't see anything)
Alternative solution if the agent is running in a service and you would like to get output:
bat(readFile("mybat.bat"))
Note: The bat file will need to be in your workspace.
Additional Note: You are no longer running the bat file from its original location. Instead it is being run from a temp location created by the underlying durable task system. This means things like %~dp0 in your script are not going to return the paths you might expect.
I have a Jenkins project with one "Execute shell" that executes some bash commands and exports one variable to the env. variables:
#!/bin/bash
...
TARGET_FULLPATH="blablabla"
export TARGET_FULLPATH
In addition, I have configured to "Post-build Actions" with:
"Trigger parameterized build on other projects"
"Current build parameters"
"Predefined parameters" -> FULLPATH=$TARGET_FULLPATH
But, when I run this project the other project that will start as soon the first one is finished seems that it doesn't get the parameter FULLPATH at all!
At least in the "Execute shell" (of the second project) the bash script print nothing for echo $FULLPATH! Also at the "Environment Variables" of the second project the FULLPATH is not included!
Any clue what am I doing wrong?
One more thing, i can't use a properties file to store the parameters, since the two projects are running on different servers and there are restrictions on copying files between those servers!
Did you have a look to this solution?
Jenkins: How to use a variable from a pre-build shell in the Maven "Goals and options"
Using a shell pre-build step + the InjectEnv plugin, you should be able to solve your problem.
Update from June 22nd, I add some screen copies.
1/ Add a first "Execute shell" script to create the properties file and an "Inject environment variables" step to load the properties file:
2/ For the demo, I add a "post-build task" step to read the variable
3/ Here is the console output, it works :)
I'd like to execute a batch file when I run the solution which will prepare the environment that I'm deploying to. I want this to run first thing when I hit F5 before anything else happens. Is this possible? If so, how?
In the project's property pages, under Debugging tab, set the proper value for Command option. Default value is $(TargetPath), which will run the output executable. Just specify your batch file there.
You will have to run the executable on your own in this case. I suggest passing $(TargetPath) as a parameter to your batch script and then executing the parameter.