How to pass environment variable to an input?

Hi,

How can I pass environment variables to an input ?

- run: echo "##[set-env name=NAME;]CrazyMax"
- uses: actions/hello-world-javascript-action@master
with:
who-to-greet: $NAME

1 Like

You currently can’t reference environment variables as inputs.  Unless you really want that particular value to be an environment variable I would suggest you set it as an output of the step and then you can reference that output in the input of subsiquent step.  

https://help.github.com/en/articles/development-tools-for-github-actions#set-an-output-parameter-set-output

- run: echo ::set-output name=action_fruit::strawberry
   id: fruit
- uses: actions/hello-world-javascript-action@master
   with:
     who-to-greet: ${{ steps.fruit.outputs.action_fruit }}
10 Likes

Ok it works thanks. But small typo in your solution:

${{ steps.fruit.output.action_fruit }}

should be:

${{ steps.fruit.outputs.action_fruit }}
2 Likes

Btw is it going to be implemented with something like ${{ job.env.NAME }} maybe ?

4 Likes

I can use global variables as input like this for example:

env:

    FOO: “bar”

job:

    step:

     env:

       BAR: ${{ env.FOO }}

So this is now implemented.

1 Like

Isn’t this transform-env-to-output just an ugly workaround? I want to reference things like GITHUB_WORKSPACE in my input parameters without workarounds or hacks.

6 Likes

You can now do something like this for passing in env variables.

Be aware, that env is just one of many contexts that you can use for passing in input

https://help.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#contexts

If you want to pass in certain types of information in such as the workspace, you should use the github context

https://help.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#github-context

jobs:
  example:
    runs-on: [ubuntu-latest]
    env:
      input: environment variable
    steps:
    # use env variable as input
    - uses: actions/hello-world-javascript-action@master
      with:
          who-to-greet: ${{ env.input }}

    # use input from the github context[https://help.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#github-context](https://help.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#github-context) - uses: actions/hello-world-javascript-action@master
      with:
          who-to-greet: ${{ github.repository}}

    - uses: actions/hello-world-javascript-action@master
      with:
          who-to-greet: ${{ github.ref }}
4 Likes

this is the best solution. Thanks @konradpabjan