Execute powershell script on a linux Jenkins server from groovy pipeline file? - linux

So after spending half a day as our surprise jenkins devops guy, I'm not sure if this scenario is even possible.
We have a linux Jenkins server that runs a groovy file to build some of our software. Recently we moved from dropbox to onedrive and one of my tasks is to change all our CI apps to upload files to onedrive. Using appveyor and powershell I was able to do this, but Jenkins is giving me problems. I was hoping to re-use the powershell scripts I wrote for appveyor, tweak them, and use them in the Jenkins pipeline. I've installed the Powershell plugin on our Jenkins server, but it's still not finding powershell. I've tested the GetAccessToken.ps1 script locally (but I'm on w10), and it's fine.
Groovy code snippet
stage('Deploy')
{
environment
{
ONEDRIVE_ID = credentials('OneDriveClientId')
ONEDRIVE_SECRET = credentials('OneDriveSecretKey')
ONEDRIVE_TOKEN = powershell "Common/Build/GetAccessToken.ps1 ${ONEDRIVE_ID} ${ONEDRIVE_SECRET}"
}
steps
{
echo 'Deploying Code....'
sh "chmod +x Common/Build/UploadToOnedrive.ps1"
powershell "Common/Build/UploadToOnedrive.ps1 -t ${ONEDRIVE_TOKEN} -s bootloader/redacted/Build/redacted-f redacted -v ${VERSION} -b ${BRANCH_NAME}"
}
}
Error
Caused: java.io.IOException: Cannot run program "powershell" (in directory "/var/lib/jenkins/workspace/test_branch"): error=2, No such file or directory
I've tried about 10 different ways of calling the script, based on other SO posts and jenkins.io posts, and I get varying degrees of failure. I'm wondering if I need a Windows slave to run powershell (although I thought the plugin would allow me to). Last resort I'll just try to alter the old bash scripts to upload using curl but I was really hoping to use the powershell scripts.
Oh, and here's my PATH. I think I read that pwsh/powershell gets installed in /usr/local/sbin or /usr/local/bin so that shouldn't be the issue
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

Thanks to Shane's sanity check, I was able to install pwsh on the Jenkins ubuntu server and then my groovy pwsh steps started working. I'm still having some issues passing variables but that's for another topic. I'm now able to use scripts like this in my groovy script
environment {
ONEDRIVE_TOKEN = pwsh (script: "Common/Build/GetAccessToken.ps1 ${env.ONEDRIVE_ID} ${env.ONEDRIVE_SECRET}", returnStdout: true)
}
Update; here is how I'm able to call a powershell script from groovy with variables
sh 'pwsh Common/Build/UploadToOnedrive.ps1 -t $ONEDRIVE_TOKEN -s "Release" -f "testname" -v $VERSION -b $BRANCH_NAME'

Related

Unable to use terraform's remote exec syntax to customize VM on proxmox

I recently encountered some problems when writing a script for Terrafrom automation.
In my case VM is using proxmox platform, not cloud platform
So I use Telmate/proxmox as my mod for creating VMs(CentOS7)
The VM builds smoothly, but when I want to customize the VM(CentOS7), there are some problems
There is an inline usage in terraform’s remote exec Provisioner.
According to the official documentation, this usage applies to line-by-line instructions
I followed this step and used it on my Provision script, the script did execute normally, and it also spawned the VM and executed the installation script.
The content of the install script is
yum -y install <something package>
install web service
copy web.conf, web program to /path/to/dir
restart web service
But the most important service is not up, but when I start the command in the script via SSH to the VM, the service is normal. That is, this cannot be achieved through terraform’s remote exec
So I want to ask if terraform is not suitable for customizing some services, such as web server, etc.? Only suitable for generating some resources such as VM?
And another custom script needs to be done using such as ansbile?
here is sample code
provisioner "remote-exec" {
inline = [
"yum -y install tar",
"tar -C / -xvf /tmp/product.tar",
"sh install.sh",
]
}
I found a way to understand this matter later, I am not sure if there is a problem with the program written by the developer or other reasons. Anyway, I can't enable the service (process) via script. But it is possible to enable the service by rebooting and using the built-in system service (systemctl).

Bash Script Fails During Jenkins Build But Does Not Fail Locally

I've been trying to debug this issue all day with no success.
Issue: Bash script executes successfully on a Linux CentOS7 VM (jenkins build agent) but fails during Jenkins build.
Here's the simple script.
#! /bin/bash
echo "before beyond compare command"
bcompare #"bc_report.ps1" "./Output_Base/<Folder_Name>" "./Output_Compare/<Folder Name>" "Report.html"
echo "after beyond compare command"
The output in the Jenkins build log is just:
before beyond compare command
after beyond compare command
When viewing the VM during the Jenkins build, I'm not seeing an artifact being produced. The user during the Jenkins build is Root, when executing the script locally, I'm also doing so as Root with success.
I'm pretty sure this is a Jenkins issue, just can't seem to figure it out. Anyone seen something similar?
Beyond Compare 4 for Linux requires an X-Window/GUI session to run, even for scripts. As a cron job or in an SSH terminal without an X-Window client, Beyond Compare for Linux scripts will fail to run. This might be what is causing your Jenkins job to fail.
Beyond Compare 4 for Windows doesn't have the GUI session requirement for scripts. Beyond Compare 4 for Linux and macOS both require a GUI session for scripts. Removing the GUI session requirement for scripting on Linux and macOS is on the feature request list for a future version.

pipenv shell working but an error is displayed

If I run the command:
pipenv shell
in my Mac shell, everything works fine and a new virtual environment is created and activated:
.../django_celery_rabbit_flower$ pipenv shell
Launching subshell in virtual environment...
bash: parse_git_branch: command not found
.../django_celery_rabbit_flower$ . /.../django_celery_rabbit_flower-rEt8HW1V/bin/activate
bash: parse_git_branch: command not found
(django_celery_rabbit_flower) .../django_celery_rabbit_flower$
but a bash error is displayed:
bash: parse_git_branch: command not found
I do not understand where it come from. Any idea?
UPDATE
Jen answer trigger a little thought. I have checked my ./bash_profile and I can see the line:
export PS1="\[\033[36m\]\u\[\033[m\]#\[\033[32m\]\h:\[\033[33;1m\]\w\[\033[m\]\$(parse_git_branch)\$ "
This shows the git branch on the bash prompt. So I believe vscode uses this settings. The folder I am working on is not a git folder. Can I write an if statement to avoid the error being displayed when running the python virtual environment?
Instead make sure that the command does actually exists before running it.
PS1="...."'$(if hash parse_git_branch >/dev/null 2>&1; then parse_git_branch; fi)'"..."

Executing Newman REST API tests using Jenkins on Linux

I found this answer Integrate Postman test scripts with Jenkins build server (and possibly there are other ones that could answer my question but I just suck at finding them) but I am still running into issues. I am trying to execute the following with newman v3 using jenkins as a way to automate our REST API Testing.
My Execute Shell script looks like this under the Build steps in the jenkins job
#!/bin/bash
pwd
cd ${PROJECT_NAME}/${GIT_SUBFOLDER}
pwd
ls
newman run ${COLLECTION_NAME} -e ${ENVIRONMENT_NAME} -r html,cli,json,junit --insecure
Ignore the pwds and ls as I originally thought I wasn't in the right directory for my collection and environment exports.
The error I keep getting back in jenkins is:
/tmp/hudson1153303836033593524.sh: line 6: newman: command not found
Any help would be greatly appreciated!
Got your problem, Seems that you are trying to execute your project as shell script in jenkins and jenkins cant get newmanHere is the solution, might solve your problemIn your script export path where npm modules get installedLocate node_modules for your system generally its at /usr/local/lib/node_modulesNow add line in your script which will export PATH:
#!/bin/bash
export PATH="<Your node_module Path>:$PATH"
pwd
cd ${PROJECT_NAME}/${GIT_SUBFOLDER}
pwd
ls
newman run ${COLLECTION_NAME} -e ${ENVIRONMENT_NAME} -r html,cli,json,junit --insecure

how to run npm/grunt command from jenkins

I'm new to this area and was trying to run the following commands from jenkins:
npm install
grunt quickStart
So far I've a jenkins running on a window machine as a window service and I've also installed NodeJs plugin for jenkins.
However, I'm stuck and quit confused following instructions here, its asking me to to add one or mode nodeJs installation and I could not find those setting and not even sure if I even need them in the first place.
Here is the bit that's asking me to do:
I cannot see this setting for the jenkins job I create. Is there an easy way to run those command in jenkins from a .bat or .sh script, a .bat would be recomended since I'm on window machine.
Note : I've already checked out a project using git in jenkins!!!
Thanks
"its asking me to to add one or mode nodeJs installation and I could not find those setting and not even sure if I even need them in the first place."
I don't think you need that I have pointed jenkins to the node installation folder an nothing more.., for this you go to Manage Jenkins->Configure System->NodeJS->NodeJS installations.. type in any name you like and point to where the node home folder is.
...cannot see this setting for the jenkins job I create...
Once you have configure that in your jenkins configuration you should have that configuration available like so:
...Is there an easy way to run those command in jenkins from a .bat or .sh script, a .bat would be recomended since I'm on window machine
I'm sorry don't get what commands are you referring to...
so summarizing :
you have to tell jenkins where you have your node installation
use that configuration in your jobs
hope this helps...
The way I made it, was trough execute shell, as the build tool for nodejs is npm, I simply wrote a shell script that instructs jenkins to run npm install in the workspace directory inside jenkins where it clones the git repository and then to zip and move the package if successful to another folder.

Resources