How to get all environment variables of a running task?

1,131 views
Skip to first unread message

zuk...@gmail.com

unread,
Jan 30, 2021, 10:44:44 AM1/30/21
to go-cd
Hi all,

I am deploying an image to AWS ECS using task definitions. In order for me to inject the environment variables from Go into the task definition, I require all the environment variables that Go sets when a pipeline is running.

For instance:
envvars.png
I want to get all those environment variables. I am calling a bash script after env vars are set above. I have tried using linux' "/proc/<pid>/environ" which works successfully, but it gets other system process variables, that I would have to filter for, which can create a brittle deployment process, and comes with maintenance overhead.

How do I extract those environment variables that Go CI/CD sets as pictured above?

Thank you.

Ashwanth Kumar

unread,
Jan 30, 2021, 10:48:59 AM1/30/21
to go...@googlegroups.com
If you're on a Linux instance try using the env command. 

--
You received this message because you are subscribed to the Google Groups "go-cd" group.
To unsubscribe from this group and stop receiving emails from it, send an email to go-cd+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/go-cd/e944146e-e37f-4316-839d-65a06dc4f003n%40googlegroups.com.

zuk...@gmail.com

unread,
Jan 30, 2021, 11:02:04 AM1/30/21
to go-cd
From my understanding, "env" prints out all the system environment variables, rather than the process environment variables. If I used "env" and a parallel job was running, wouldn't it overwrite the environment variables (non thread-safe env vars)?

Ashwanth Kumar

unread,
Jan 30, 2021, 11:11:37 AM1/30/21
to go...@googlegroups.com
The process environment variables overrides the system variables (if any). If you're interested only in the environment variables that are shown above you don't have to worry about value being changed or overwritten by another process because GoCD uses bash -c to run tasks with the new variables injected as part of the run, so these values can't be changed from outside. 

Ashwanth Kumar

unread,
Jan 30, 2021, 11:19:03 AM1/30/21
to go...@googlegroups.com
Okay, I just realised from this post once again. I don't think there is any way to get only those values that are set directly, instead if you know what those values are then you can write a script to write these values into a text file and use that as a artifact for the remaining Tasks. 

Having said that I don't know why would you need a list of what are all available and not specifically look for what's needed? 

On Sat, 30 Jan, 2021, 21:14 zuk...@gmail.com, <zuk...@gmail.com> wrote:
--

zuk...@gmail.com

unread,
Jan 30, 2021, 11:22:03 AM1/30/21
to go-cd
I understand they are running in a separate bash shell, let me clarify the question. If I run "env" other environment variables will be printed to stdout. I am only interested in the specific environment variables that are posted above without filtering it.

zuk...@gmail.com

unread,
Jan 30, 2021, 11:23:30 AM1/30/21
to go-cd
Ah you re-posted. The reason why I don't want to explicitly state what is required is because it means you can set any env var in Go CI/CD (in stage, job etc..) and you won't require changes to the bash script underneath. It's a cleaner approach.
Message has been deleted

zuk...@gmail.com

unread,
Jan 30, 2021, 11:30:12 AM1/30/21
to go-cd
I can write them to a file, but they require knowledge of the default set env vars i.e. "GO_SERVER_URL", "GO_*" etc..

It seems there are a few approaches:
1. `cat` the go-agent log and extract env var key and value using regex against "setting environment variable 'GO_SERVER_URL' to value 'www.gourl.com'". That would give me the exact env vars set by Go CI/CD in that task, which I can then inject into ECS.
2. call `env` or `declare -xp` and for every env var I add a "filter key" such as "@APP_NAME", filter for all that have "@" and "GO" set as keys in env.

I thought there may be a approach that Go CI/CD has to get specifically the environment variables that is set in that task. Which would be perfect.

zuk...@gmail.com

unread,
Jan 30, 2021, 1:12:27 PM1/30/21
to go-cd
Are the logs from a Go task stored anywhere? I can't seem to find the log.

zuk...@gmail.com

unread,
Jan 30, 2021, 1:29:31 PM1/30/21
to go-cd
Found it, it's stored on the go-server itself not in the go-agent. For googlers in the future, go stores console output to 
"godata/artifacts/pipelines/"
Reply all
Reply to author
Forward
0 new messages