How to display jenkins default environment variable from shell command? - shell

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

Related

Make variables exist outside a shell script

I'm trying to make variables exits outside of a shell script without using source.
The variables are declared in shell script with
export varA=3
and I run the script with ./filename.sh
I want
echo $varA
in the terminal to return 3 (i.e. the value of varA). So extend the scope of the variable to outside of the script
To sum up: how do I make the variables inside a shell script exist outside.
Thank you in advance
You can run your script on this way:
. ./filename.sh
This mean when run it will not spawn new shell but run it in current. And variables you set in your script will be available in your shell. This is kind of "source" as mentioned in comments.

AWS CLI Commands not setting ENV Variables with Bash Script

So I have created a very simple Shell Script. All it does it set 3 ENV variables for AWS so that I can send a .csv file to S3 in a separate script.
#!/usr/bin/env bash
export AWS_DEFAULT_REGION=us-east-1
export AWS_SECRET_ACCESS_KEY=ksjdnkjsdnfkjsndfksjnfd
export AWS_ACCESS_KEY_ID=kjsdnfkjsndfkjsndfkjsdn
The SECRET KEY and KEY_ID have been changed for this post, just so we're clear
But for some reason, when I run this Shell Script, nothing seems to happen at all. When I run the ENV command, none of the variables are set. HOWEVER, when I copy and paste each command into my terminal individually, it sets the variable without issue. And then I am able to send my .csv file to S3 without issue.
I feel like I'm doing something extremely trivial that's causing the script to simply not run the commands.
Any ideas?
System: Raspberry Pi 3
Release: 10
-Joe
If you're just executing your script ./myscript.sh, the environment variables won't be set in your shell just the subshell it executes in.
You can execute your script with a "dot space script" syntax or use source. Both of which will execute the script in the current shell instead of launching a subshell.
. myscript.sh
source myscript.sh

Access custom environment variable from Jenkins file

I need to access system environment variable from my Jenkins file. I know that there are some predefined variables (e.g. JOB_NAME or BUILD_NUMBER), but I need to access custom environment variable which I set previously. What are the way to do this? It seems that env.MY_VARIABLE and env['MY_VARIABLE'] but those don't work. I need this to have access to the variable which would be specified during the pipeline build inside a bash script. Probably there are more convenient ways to pass information from bash script to Jenkins file, which called this bash script.
You access environment variables like ${DB_ENGINE} or $DB_ENGINE from bash or in your Groovy job/pipeline DSL script where DB_ENGINE is the custom environment variable you set.
Check documentation.

Is there a command to dump local variables in a bash script?

I have many scripts where I make use variables within the scripts. While the script is running, can I issue a command within the script to dump all of the variables and their values?
The command env on its own should display all the variables.
The builtin command set on its own will also dump the variables.
Note that inherited variables will also be dumped.
If you are after a way of helping you debug your script, you could put
set -x
At the beginning of your script that will display command traces including how your variables are used.

Changing the value of a export variable from a bash script

I did the following from a bash shell.
export myPath="/home/user/dir"
Then I verified the value of this by 'echo'ing this value from both shell and a inside a bash script. Both worked fine.
Then I tried setting the value of this variable from inside a script like this.
myPath="/home/user/newdir"
and tried printing this variable from shell. I thought the variable will hold the updated value, but it was showing the old value.
How can I update the value from a script? I am looking to do it without using source if possible.
To make the variables persist after the script finishes, you have to run it using the source command:
When a script is run using source it runs within the existing shell, any variables created or modified by the script will remain available after the script completes. In contrast if the script is run just as filename, then a separate subshell (with a completely separate set of variables) would be spawned to run the script.

Resources