Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to make env vars available to JVMs? #189

Open
autonomousapps opened this issue Jul 17, 2020 · 9 comments
Open

How to make env vars available to JVMs? #189

autonomousapps opened this issue Jul 17, 2020 · 9 comments

Comments

@autonomousapps
Copy link

I am working on a new step that would let people execute arbitrary Groovy scripts (https://github.com/hiyainc/bitrise-step-groovy-script-runner). During testing in my own pipeline, though, I am unable to access environment variables I expect to be present. I use the standard JVM approach of calling System.getenv('BITRISE_BUILD_NUMBER') (for example). When I test that in a bash script (with $BITRISE_BUILD_NUMBER), I get the expected value. When I test it in my Groovy script, it's always a blank string (not even null). I have tested this same Groovy script locally, and System.getenv(...) works as expected.

How does envman -- and indeed, Bitrise -- expose environment variables to steps?

To provide further examples, I have a step that has this output:

Screen Shot 2020-07-17 at 10 48 24 AM

and I cannot access that env var in my groovy step which runs immediately afterwards. Just for the hell of it, I inserted a bash step between the two that does this:

#!/usr/bin/env bash
set -ex
echo -n $BITRISE_APK_PATH | envman add --key PROD_RELEASE_APK_PATH

and I also could not access PROD_RELEASE_APK_PATH. This works when consumed from a bash script.

Thanks for your time.

@pigeondotdev
Copy link

Hi @autonomousapps,

I have shared this with our engineering team.

@mjones-droid
Copy link

Hi! I apologize that nobody has gotten back to you yet. Are you still having issues with this?

Once an environment variable is exposed via envman it should be accessible just like any other environment variable...

@autonomousapps
Copy link
Author

Still having issues, although honestly I haven't tried for weeks. It has completely blocked progress on this new step, which I had hoped to share to the wider community.

The last thing I tried before giving up was printing all of the env vars from my groovy script. In pseudocode, I ran

System.getenv().forEach {
  println "$key -> $value"
}

and when I looked at the logs, I actually saw DOZENS of nearly blank lines. The only thing on those lines was an ->. So it looked like there were actually quite a lot of env vars, but for some reason they all had blank keys and blank values (which is impossible). No idea what is going on.

@mjones-droid
Copy link

I haven't actually dug into your code to see what's going on, but I suspect this has to do with the fact that environment variables on Bitrise are scoped. Sometimes they are scoped to their workflow, sometimes they are scoped to a specific step. Parent processes can’t access environment variables exposed by child processes.

Please take a look at the documentation here:
https://devcenter.bitrise.io/bitrise-cli/most-important-concepts/

@mjones-droid
Copy link

Also, you may want to take a look at this thread:
#130

Envman doesn't load the values in the same step, only in subsequent steps.

@autonomousapps
Copy link
Author

Conceptually, my step is very simple:

  1. Download and install SDKMan
  2. Use SDKMan for downloading and installing Groovy
  3. Launch user-provided groovy script via groovy path_to_script.groovy.

So if anything the groovy script is the child process and should inherit from its parent(s), which I assume are (1) the bitrise workflow, (2) bitrise step, and (3) shell that launched the script, in that order.

It's been a while since I tried to do anything with this, so I can't now recall if the shell which launched the groovy script had access to the env vars.

@mjones-droid
Copy link

When you get a chance, please check that the shell launching the groovy script has access to the environment variables.

@autonomousapps
Copy link
Author

Honestly not sure what to do anymore. I can see the env var in my bash script, which invokes groovy. In my groovy script, it always comes up blank. Yet, if I do

System.getenv().each { println it }

it will print all the env vars. But trying to do System.getenv('SOME_VAR') never works. In fact, even this doesn't work:

def env = System.getenv()
println env['SOME_VAR'] // prints nothing

I have also tried

System.getenv().each { 
  println it 
  println "  class name = ${it.class.simpleName}"
}

and the second line is always blank. This should be impossible. Groovy is a JVM language and everything has a type.

I have tried running this same groovy script locally, not via a bitrise step, and everything works as expected. I have also tried invoking my groovy script from a bash script, and everything works as expected. Something about the context of running inside bitrise has irrevocably (apparently) broken Groovy.

Shot in the dark: could this be a character encoding issue? How could I check that?

@autonomousapps
Copy link
Author

Quick follow-up: I wrote a gradle task -- in groov -- and executed it using the gradle script runner step. It works perfectly.

tasks.register("royalPenguin") {
    doLast {
        def path = System.getenv('BITRISE_APK_PATH')
        println "BITRISE_APK_PATH=$path"
    }
}

not sure why this works but the "raw" groovy script doesn't.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants