Currently I have framework which is hosted in GitHub. Inside the framework (Maven) there is an shell .sch script which Jenkins will execute.
I know its possible to pass parameters from Jenkins (Via This project is parameterised option) but is there a way to pass run time parameters from Jenkins into an .SH script?
It should work just like passing any other value to a shell script.
sh '''
yourscript.sh ${PARAM1} ${PARAM2} ${PARAM3}
'''
Related
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.
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.
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.
I am not able to get output of the Environment-Variable BUILD_URL through Shell command.
Configured Manage Jenkins-->Configure System-->Jenkins Location-->Jenkins URL as http://xxx.xxx.xxx.xxx:8080/
Using below script within Shell command.
#!/bin/bash
echo ${BUILD_URL}
Do i have to set jenkins_url within the script?
In a Shell you can't access environment variables like that. you will need to read here: https://www.digitalocean.com/community/tutorials/how-to-read-and-set-environmental-and-shell-variables-on-a-linux-vps.
What you can do is in the Jenkins's job session you can send a parameter to your script to gain access to the env parameter. in the Shell section you will have access to the environment parameter, in the script itself it'll work differently.
Good luck
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']