Use TeamCity environment variable in shell script - bash

I have a TeamCity environment variable env.TIER_SUFFIX whose value is set as A. I want to be able to use this in my script build step where the script content is
#!/bin/bash
tierSuffix=%env.TIER_SUFFIX%
echo "TierSuffix is [${'$'}tierSuffix]"
export ENV_TIER=%env.DEV_tierSuffix%
The build fails because export ENV_TIER=%env.DEV_tierSuffix% creates env.DEV_ and the build stops. Is there a way to use this in the export command so that it substitutes the value of %env.DEV_A% to ENV_TIER

The way I use TeamCity variables in bash scripts is:
buildCounter="%build.counter%"
where build.counter is TeamCity variable.

Related

How to pass bash task in Azure devops as filePath

I'm trying to create a linux script and run that in Azure devops as bash task .
My script is running fine when I add the code as inline Option but while trying to give the script as filePath ,the variables $(System.DefaultWorkingDirectory) and $(Release.PrimaryArtifactSourceAlias) variables inside the script is failing as Unable to get the variables .
How to pass the script as filePath and how to access the two variables if I pass the filepath and not as inline option.
When you pass the script as inline value, the pipeline engine automatically replaces $(System.DefaultWorkingDirectory) with the correct value.
When you run a script file, the engine doesn't analyze or change it, so it wont replace the variables. But the variables are still available, as environment variables. So $(System.DefaultWorkingDirectory) should become $SYSTEM_DEFAULTWORKINGDIRECTORY. This will work for inline script as well.

change Jenkins shell for only one pipeline

How can I make Jenkins use Bash rather than its native shell for just one Jenkins pipeline/Jenkinsfile? Does the "agent" help me to do this?
I wrote a shell script for deployment but some of the parameters contain whitespace which messes up the resulting command I generate by losing some args. I've found how to avoid this problem by globally configuring Jenkins shell type to be Bash. But when I change the global shell type, my other Jenkins pipelines that use the Jenkins docker plugin syntax get broken when they use the 'sh' command within a docker container. My workaround is to ping pong the global setting for shell type depending on which Jenkins build I want to run. Its a royal PITA.
I'm embarrassed to say all I needed was a shebang.
My Jenkinsfile runs a custom (bash) shell script, using Jenkin's sh command, like in the following:
sh "./deploy.sh \"arg1\" \"arg 2\" \"arg3\""
In order to force deploy.sh to run within Bash, the contents of deploy.sh need to include #!/bin/bash on the first line, as in:
#!/bin/bash
echo "deploy args: $#"
Regardless, I think there should be a way to tell a Jenkins pipeline that it should run with specific settings, like sh=bash.

How to export or set a make variable from a shell file

I am running a shell script from make environment
I execute the script with input parameters as make variables:
/shell_script.sh $(make_var1) $(make_var2)
I process these variables in shell. I want to assign the result from a shell command to the make variable and export back to shell.
make_var=shell_command
How can I do this?
It is not trivial to change the parent environment of a shell-script, one approach is to echo the export statements and source the output of the script in your parent environment:
...
echo "export make_var1=${make_var1}"
...
and when you launch your script do it using eval:
eval $(./shell_script.sh $make_var1 $make_var2)
this is the approach taken by for example ssh-agent.
A second option is to source the script, in that case the script will be run line-by-line in the current shell:
. shell-script.sh
any export statements in the script will be run in the current shell.

jenkins - how to pass a value from bash to groovy?

I'm working on a jenkins install with two script components. The bash bits run first and then groovy. I'd like to be able to pass a value (property? Other?) from the bash script->groovy script.
Is this possible? Do I need to write the value to a property file and read it back in groovy?
EDIT: my goal from this was to generate a build # in bash and pass this to groovy so I could set the description and build # in the jenkins display. It appears that groovy isn't available on the build server so I'm looking for another direction. Currently experimenting with the 'postbuild' plugin and the 'env-inject' plugin. Open to suggestions.
Here are a few things to consider to make this successful:
Make sure you're trying to accomplish this with one "Execute shell" in Jenkins or from a script.
Export the shell variable so that the variable will be present in the child process that will execute your groovy script.
# foo.sh
export foo=bar
groovy myscript.groovy
# myscript.groovy
def env = System.getenv()
String myvar=env['foo']
println myvar
Running foo.sh should produce the following:
./foo.sh
bar
If for some reason you prefer not to export the variable (there may be good and valid reasons for this), you can still explicitly pass the variable to the groovy script as a "system property":
# foo.sh
foo=bar
groovy -Dfoo="$foo" yourscript.groovy
# yourscript.groovy
String yourvar = System.properties['foo']
println yourvar
which produces the following results:
$ ./foo.sh
bar
$
I just worked on this problem for days and thought I might share what I discovered. I had to access a variable in a groovy file from a .sh file and had difficulty at first grabbing the variable. There is a simple way to do it, though. Here's what I did:
In your bash file, save the value in a variable. Then in the groovy script, do this:
variableToGet = sh(returnStdout: true, script: """
. ${DIRECTORY}/bash_file.sh
echo \$VARIABLE
""").trim()
Hope this helps. This problem was a good challenge! It's important to note, however, that standard out will return a String, regardless of what type of variable you are grabbing. If you need to use an integer value, you can then use the integer value with Integer.parseInt(variableToGet)
The best way is setting an environment variable to share the information from bash into groovy. You could pipe things as well using standard in/out as well.
So if you are setting the env in a bash script it wont be available outside of that script. Instead of doing a bash script put the script inline in your command in jenkins. Run your bash code then call the groovy script.
Something like below
#do somebash scripting
VARIABLE="something"
#call your groovy code
groovy util.groovy
your groovy code (util.groovy):
String variable = env['VARIABLE']

How to access GIT_COMMIT env variable in bash file

I am creating bash file in which i want to access only one environment variable of Jenkins i.e GIT_COMMIT.
$GIT_COMMIT should be available from your "Execute shell" build step.

Resources