How to set a task variable from a remote powershell script? - tfs

From a release definition,
When I execute the following powershell script with the powershell task, it works
successfully and I can retrieve the value from the following tasks in the release definition :
Write-Host "##vso[task.setvariable variable=sslThumbprint]toto"
When I execute the exact same script thanks to the "Powershell on target machine task", I don't get any value.
How can I set a variable from a powershell script that do not execute on the agent machine ?
Regards,

This can only be done in case of hosted agents. If you are using hosted agents then only you will be able to run the commands on the agent's machine.
Microsoft as of now provides no feature for doing this.

Related

User Interactive shell script not running in Jenkinsfile

I have a user interactive shell script that runs successfully on my Linux server. But when I try to run it via jenkins, it doesn't run.
I have created a Jenkinsfile.
Jenkinsfile
node('slaves') {
try
{
def app
stage('Remmove Docker service') {
sh 'sshpass ssh docusr#10.26.13.12 "/path/to/shell/script"'
}
}
}
Shell Script
#!/bin/bash
read -p "Hi kindly enter the api name : " api
docker service logs $api --raw
The shell Scipt runs successfully on my local server, when I try to run it on Jenkins using Jenkinsfile, it doesn't accept $api variable in my shell script which is user interactive.
What you are trying to achieve doesn't serve any purpose of automating your job by jenkins, if I correctly understood. So, your job is actually seeking a user input and it's in best interest to have a parameterized jenkins build in this case.
For your case, you can still give an argument to the sshpass command $api and have it read from the jenkins environment itself Or, better make your jenkins build parameterized and use your user input $api as the parameter.

In Azure Devops or TFS is it possible to pass a PowerShell argument that is variable on the pipeline environment?

I am trying to create a Task Group that can be used interchangeably between Build and Release pipelines.
However, I am having an issue passing one of the arguments to my Powershell Task Group.
The reason for this is that the argument data that I'm trying to pass doesn't exist within the Build environment.
- envName "$(Release.EnvironmentName)"
For example, can I setup logic to send a different variable if this Task Group is executed within the Build environment?
Is there a way for me to pass this argument only if it exists? Or to pass this argument OR a different argument?
Thanks!
In Azure Devops or TFS is it possible to pass a PowerShell argument that is variable on the pipeline environment?
I am afraid there is no such way that we could pass a PowerShell argument that is variable on the pipeline environment directly.
But as a workaround, you can add a another powershell task in your Powershell Task Group to set the variable based on the pipeline environment, like:
Write-Host "Current Release Name is $Env:Release_EnvironmentName"
$ReleaseName= $Env:Release_EnvironmentName
Write-Host "Current Environment Release Name is $ReleaseName"
if ($ReleaseName-eq "")
{
Write-Host ("##vso[task.setvariable variable=envName]TestValue1")
}
else
{
Write-Host ("##vso[task.setvariable variable=envName]TestValue2")
}
The envName will be set different value based on the value of the $Env:Release_EnvironmentName, we could use it in the next Powershell Task in the Powershell Task Group.
Check the Logging Command during the build/release for some more details.
Hope this helps.

Does groovy script support PowerShell syntax?

Does groovy script support PowerShell syntax?
I have a PowerShell script that I would like to use in Jenkins pipeline. I was not able to find any PowerShell module under "Generate Pipeline Script".
Of course not. PowerShell is a completely different scripting language than Groovy. You can exeucte your PowerShell script through Groovy though if you have a PowerShell interpretor available and Jenkins Pipeline Scripts allow to call it.
It all depends what you want to do. If you have an existing build script you want to translate into Jenkins pipeline, you would normally want to translate to Jenkinsfile-style groovy. However if you have a script to want to call, there is a standard step to do that to do that. It is important to realise that this comes up as a single step wrt jenkins, but it might be what you want to do. As said, all depends.
These answers are slightly confusing based on the question.
Can you run PowerShell in a Jenkins pipeline? Absolutely
Are their caveats and weird catches to get Groovy to interpolate you script correctly? Yes!
I think you can only run PowerShell on a Windows based Jenkins host or build node (even though you can run pwsh on all *nix machines). To do this is you just call powershell instead of sh in the pipeline step.
powershell """
Write-Host "PowerShell stuff in the pipeline"
Write-Host "Because escaping vars can get gnarly I do this..."
Set-Variable -Name "LonghandVar" -Value "Some Value"
"""

Can't access build parameters in excute shell in jenkins

I'm trying to call a python script through an execute shell step in a Jenkins parametrized build. The problem is I need to pass the build parameters to the python script which doesn't happen. Here is how I call the python script in execute shell:
python2.7 C:\test\my_script.py -m $module
$module is passed as an empty string.
I've tried in Execute Windows batch command with %module% and it worked fine.
But I need to run it on an excute shell not a windows batch command.
It looks like you are running on Windows ("C:\test\my_script.py ..."), so "Execute shell" will not work properly.
Should either use Execute Windows batch command or move your job to a Unix/Linux machine (can use a Jenkins-Agent for that).
Try this
python2.7 C:\test\my_script.py -m $MODULE
From the documentation
Note that because of the case sensitivity difference of environment
variables in Windows and Unix, all the environment variables added by
parameters are in upper case.
Hence please try using $MODULE instead of $module

Environment variable retrieve from NAnt script is not recognize in Jenkins nant build step

In my nant script I retrieve my environment variable in this way:
property name="ProjectSolutionPath" value="${environment::get-variable('MAIN_PROJECT_PATH')}"
but when I run it through jenkins using nant as build step I got an error like this.
Expression: ${environment::get-variable('MAIN_PROJECT_PATH')}
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Environment variable "MAIN_PROJECT_PATH" does not exist.
Is there configuration for this? so that Jenkins will recognize environment variables access by my nant script?
Help is greatly appreciated.
Make sure you define this environment variable in "System variables".
Since the Jenkins process usually runs as "NT AUTHORITY\SYSTEM" user, the environment variables associated with your user account do not get appended to the env-vars of the process.
I've seen this answer on how to add env-vars to a Jenkins build (though I don't like spawning a cmd line process), if you do not want to use sys-env-vars.

Resources