Conditional build steps depending on results - teamcity

I want to create a build step in Teamcity using Powershell, but was wondering, is there a way to put an if/else condition in Teamcity? So for example, I can use the if/else statement to read the result of the Powershell script.
Is this possible?

Yes. When you add your build step (Runner type = Powershell), choose Script = "Source code" instead of "File". That allows you to write some Powershell code in the text area, so you can manually invoke your PS script, do so conditionally, and capture its output.

Related

Choice paramter in jenkins as an output from shell script (pipeline)

I'm looking for a way to use an output from shell as a jenkins parameter but in pipeline, don't want to use any of UI plugins.
For example I want to use output from command
ls /home
as a choice parameter (so I would be able to choose users from the list), is it possible to do something like that?
It must be done in pipeline, I'm not looking for some plugins which allow you to do something like that, but you need to do all in UI, if plugin support pipelines then it's also ok.
For a pipeline to run, its parameters need to be completely defined before the pipeline is started.
For the pipeline to parse the output of ls /home, it needs to run and allocate a node and a workspace, which can only happen after the pipeline is started.
So you are in a kind of a chicken-and-egg problem, where you need to run some code before you start a pipeline, but you can't run pipeline before you run that code.
So your issue boils down to "How can I run some arbitrary Groovy script before I start my pipeline?"
There are two options for this:
ActiveChoice plugin allows you to define a parameter that returns a script. Jenkins will then run the script (don't forget to approve it) in order to show you your "Build with parameters" page. Debugging this is notoriously hard, but this can go to great lengths.
Alternatively, you may want to run a scripted pipeline before you run the Declarative (main) one, as outlined e.g. in this answer. This may look a bit like this:
def my_choices_list = []
node('master') {
stage('prepare choices') {
// read the folder contents
def my_choices = sh script: "ls -l /home", returnStdout:true
// make a list out of it - I haven't tested this!
my_choices_list = my_choices.trim().split("\n")
}
}
pipeline {
parameters {
choiceParam('OPTION', my_choices_list)

Powershell auto-completion using script file

I wanted to execute a PowerShell script file script.ps1 like this:
powershell ./script.ps1
How will I simulate a 'tab' (auto-complete) feature in script file?
Eg : on the PowerShell prompt, I can do
PS> get [TAB]
for getting suggestions for commands starts with 'get-'
Same thing : how will I write in a script file, means saving the script.ps1 file like:
get-\t
and executing PowerShell ./script.ps1 resulting error instead of getting suggestion for command which starts with "get-"
Any idea to encode the tab event inside a script file to achieve the command auto completion ?
powershell get-command get-*
Is working
It's very unclear what you intend to do but here are the basics on tab-completion:
Save the PS1 file then you can type part of the file name and autocomplete will offer it among the suggestions (though this is done in alphabetical order for all possible suggestions that PowerShell knows.)
If you wish to get parameter tab completion you need to enabled the cmdlet (script) or function as an "advanced" function with a Param block which defines all of the Parameters you cmdlet or function accepts.
The typing: Get-MyCommand - # will result in the possible param and switch names being offered in turn.
Running PSReadline (available from the PowerShell Gallery an enhance the autocomplete feature for all commands and parameters.
If you are seeking to perform some other function you'll need to edit your question to be more specific and how clear examples....

How to execute next batch windows command if previous execution fails?

I am trying to execute multiple windows batch commands in jenkins one after the other. The problem is that if any the of the project/build fails, it never gonna execute next windows batch commands.
My question is How to execute next windows batch command if previous execution fails?
Help me with this regard.
When you say "multiple windows batch commands", do you mean:
Multiple Execute Windows Batch Command build steps
Or multiple lines of commands within a single build step?
If configuring multiple build steps, you just need to make sure that the last command of the build step does not return anything other than 0. You can do this by adding either of the following as the very last statement in your build step:
either exit /b 0
or echo "All done"
As for multiple lines within the same build step, default implementation of Execute Windows Batch Command does not break if one line/statement fails (which is different from default Execute Shell implementation). As long as the very last statement returns 0, the build step will not fail, and any lines failing in between does not matter.
Once again, you can reference to the above list to make sure that the last line always returns 0
Re "if any the of the project/build fails"
Do you mean "if any of the batch commands fails"?
See Conditional BuildStep Plugin.
In your job config scroll down to:
Build
[ For each command that can fail Add build step at the bottom of this Build section ]
Conditional step (single)
Run? | Not
! | Execute Windows batch commands
Commands | ... your commands ...
[ Click Advanced... ]
On evaluation failure | Fail the build
Builder | Set the build result
Result | Success
Or add just one Conditional step (single) section and write all of your commands into:
Commands | ... your commands ...
Or maybe Conditional step (multiple) is the way to go for you. I haven't used this so far, so I'm not much of a help there.

Pass variable from Jenkins to Ruby script (Jenkins newbie)

I've pulled a few scripts into Jenkins for a proof of concept and think I'd like to move that direction for all of our scripts. Right now I keep an environment.rb file with my code (watir-webdriver, cucumber) which tells the script which environment we're testing and which browser to use (global variables). Jenkins fires off the script using rake.
I'd love to let the user choose the environment and browser through Jenkins 'choice' variable or similar, and then pass that to the script. While I see the framework in that for Jenkins and set up a choice list for environment, I'm having trouble determining what the next step is.
I could write to environment.rb, I could pass a variable to rake - I have many options for how to pass the information, I just need some assistance finding the first step to find the Jenkins way of accomplishing them. Google results and previous Stack questions weren't what I was looking for.
Thanks
Sure. Give the user either a text entry field a dropdown after telling Jenkins that this is a parameterized build. You'll give them a name, something like BuildEnvironment. Then when you call the build, you can pass these from the environment variables. For example, if you were using ANT, you'd add a line to the parameters that said environment = ${MyEnvironment} Jenkins will then pass the value along for your build tool to use.
There is a way to pass Jenkins Environment Variable to Ruby script. Please see the following example:
workspace_path = `echo $WORKSPACE`.strip # note the use of backticks
puts workspace_path
In the "echo $WORKSPACE".strip # the code work only if you replace quotes with backticks
This code example works in Jenkins on a Linux system.

Result of shell script as build setting

Is it possible to run a shell script and use its result as a user defined macro in Xcode?
Basically I just want the result of a shell script to be put in a variable so it gets set in Info.plist (just like ${EXECUTABLE_NAME} etc.)
For example:
If I add $(/usr/bin/whoami) as a build setting condition (at the bottom of settings of the build configuration) it just sets an empty string.
See this question for a couple of different approaches. All of them require add a "Run Script" build phase.
Assuming a bash like shell, and given an almost complete lack of context for your problem, try
EXECUTABLE_NAME=$( scriptToGetEXEC_NAME )
PRODUCT_NAME=$( scriptToGetPROD_NAME)
The $( ... cmd ... ) construct is called command substitution. What this means is the when the shell processor scans each line of code, if first looks to see if there are any $(...) embedded (and other things to). If there are, it spawns a new shell, executes the code inside, and if any text is returned, it is embedded in the command line and THEN the shell scans the line again, and eventually executes everything from left to right, assuming that the first word will turn into a built-in command or a command in the PATH.
I hope this helps.
P.S. as you appear to be a new user, if you get an answer that helps you please remember to mark it as accepted, and/or give it a + (or -) as a useful answer.

Resources