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

workflow level env. is unrecognised on job level's 'if'-expression when calling reusable workflow #1661

Open
arauchberger opened this issue Feb 4, 2022 · 21 comments
Assignees
Labels
Actions Feature Feature requires both runner, pipelines service and launch changes context

Comments

@arauchberger
Copy link

the idea is to have a workflow_dispatch input to choose wether or not to execute tests after the build part is done.

Describe the bug
environment variable defined on workflow level is not available for if-condition on job level - in case the job is calling another "reusable" workflow.
everything is fine within a regular job and it's steps.

To Reproduce
Example:

name: ay_test_condition

on:
  workflow_dispatch:
    inputs:
      build_skip:
        description: 'skip maven and chart builder'
        required: true
        default: false
        type: boolean  
      test_skip:
        description: 'skip product integration tests'
        required: true
        default: false
        type: boolean    

# workflow level env
env:
  BUILD_SKIP: ${{ github.event.inputs.build_skip == 'true' }}
  TEST_SKIP: ${{ github.event.inputs.test_skip == 'true' }} 

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Display build environment
        id: displayBuildEnv
        shell: bash
        run: |
          echo "build_skip: ${{ github.event.inputs.build_skip }}";
          echo "env.BUILD_SKIP: ${{ env.BUILD_SKIP }}";
          echo "test_skip : ${{ github.event.inputs.test_skip }}";
          echo "env.TEST_SKIP: ${{ env.TEST_SKIP }}";
      
      - name: build something
# works fine here (at workflow.job.step level)      
        if: ${{ env.BUILD_SKIP == 'false' }}
        run: |
          echo "Building something"   

  test:
    needs: build
# does not work here (at workflow.job-workflow_caller)    
    if: ${{ env.TEST_SKIP == 'false' }}
    uses: ./.github/workflows/ay_test-workflow2.yml
    with:
      teststatus: true   

Expected behavior
the env should be available everywhere, since it's declared on workflow level.

Runner Version and Platform

Current runner version: '2.287.1'
Operating System
Ubuntu
20.04.3
LTS
Virtual Environment
Environment: ubuntu-20.04
Version: 20220131.1
Virtual Environment Provisioner
1.0.0.0-main-20220201-1

What's not working?

Errormessage:

The workflow is not valid. .github/workflows/ay_test_condition.yml (Line: 45, Col: 9): Unrecognized named-value: 'env'. Located at position 1 within expression: env.TEST_SKIP == 'false'
@arauchberger arauchberger added the bug Something isn't working label Feb 4, 2022
@arauchberger arauchberger changed the title workflow level env. is unrecognised on job level if expression when calling reusable workflow workflow level env. is unrecognised on job level's 'if' expression when calling reusable workflow Feb 4, 2022
@arauchberger arauchberger changed the title workflow level env. is unrecognised on job level's 'if' expression when calling reusable workflow workflow level env. is unrecognised on job level's 'if'-expression when calling reusable workflow Feb 4, 2022
@ruvceskistefan
Copy link
Contributor

Hi @arauchberger,
Thanks for opening this issue. We will try to reproduce it to understand the cause of the issue and we will get back to you as soon as possible!

@nikola-jokic nikola-jokic self-assigned this Feb 11, 2022
@thboop thboop added context Actions Feature Feature requires both runner, pipelines service and launch changes and removed bug Something isn't working labels Feb 11, 2022
@thboop
Copy link
Collaborator

thboop commented Feb 11, 2022

Thanks for the report @arauchberger!

If it is helpful, we publish a list of contexts and what yaml fields they are available in on the GitHub Docs Website. Currently, we don't support this feature, but I've added the appropriate labels so we may consider it in the future.

@arauchberger
Copy link
Author

thanks for the feedback and the hint to the Context-Documentation. If i read it right the following should work instead of what i tried, since i can use the context inputs on the workflow key jobs.<job_id>.if

name: ay_test_condition

on:
  workflow_dispatch:
    inputs:
      build_skip:
        description: 'skip maven and chart builder'
        required: true
        default: false
        type: boolean  
      test_skip:
        description: 'skip product integration tests'
        required: true
        default: false
        type: boolean    

# workflow level env
env:
  BUILD_SKIP: ${{ github.event.inputs.build_skip == 'true' }}
  TEST_SKIP: ${{ github.event.inputs.test_skip == 'true' }} 

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Display build environment
        id: displayBuildEnv
        shell: bash
        run: |
          echo "build_skip: ${{ github.event.inputs.build_skip }}";
          echo "env.BUILD_SKIP: ${{ env.BUILD_SKIP }}";
          echo "test_skip : ${{ github.event.inputs.test_skip }}";
          echo "env.TEST_SKIP: ${{ env.TEST_SKIP }}";
      
      - name: build something
# works fine here (at workflow.job.step level)      
        if: ${{ env.BUILD_SKIP == 'false' }}
        run: |
          echo "Building something"   

  test:
    needs: build
# does not work here (at workflow.job-workflow_caller)    
#    if: ${{ env.TEST_SKIP == 'false' }}
# this should work ?
    if: ${{ github.event.inputs.test_skip == 'false' }}
    uses: ./.github/workflows/ay_test-workflow2.yml
    with:
      teststatus: true 

thanks in advance for conformation

@arauchberger
Copy link
Author

arauchberger commented Feb 16, 2022

i just gave it a try - this works as expected now - that's a workaround i can live with. even though i do not really understand the reason for not having the workflow-level env context availably everywhere in the workflow.

many thanks for your response.

ps: should i close this issue now, or keep it open as a "feature-request"?

@nikola-jokic
Copy link
Contributor

Hi @arauchberger,

Let's keep it open as a feature request 😊

@arauchberger
Copy link
Author

i just found out that this does not work as expected!
it works if i trigger the build manually via workflow_dispatch -> then the github.event.inputs._skip are set to true|false.
but when the workflow is triggered via a push the github.event.inputs.
_skip are "undefined" so the if-condition does not match :-(.

@nikola-jokic
Copy link
Contributor

Thanks for the update,

We will try to reproduce it to figure out what is going on.

@nikola-jokic
Copy link
Contributor

Hi @arauchberger,

Please correct me if I did not understand this correctly, you expected on push to propagate inputs. But push does not allow inputs. Inputs are reserved for workflow_dispatch and workflow_call. Note this page in the docs.
On the other hand, if you try to specify on.push.inputs in the in-browser editor, it will show an error since the on.push object does not allow for inputs. I mean you can still create it by ignoring that, but inputs would not be taken into account in the workflow.

But please, correct me if I did not understand this properly.

@arauchberger
Copy link
Author

Hi @nikola-jokic,

well, i did not really expect to propagate inputs on push, but some default-values in this case.
what i really want to achieve is the following:

on workflow_dispatch i have boolean-inputs to choose whether to run the build and test steps/jobs. so the idea is to say "don't build but test" and the other way round "just build, skip tests".

on push: these booleans should just say skip=false so both is executed build and test

i don't see a way how to get this working. global env's are not available on job.if and the inputs just don't exist on.push so don't get evaluated.

i'm thankful for any help. maybe i do follow a totally wrong approach?

@nikola-jokic
Copy link
Contributor

Thanks for your quick response! Maybe, you can use this inside your if condition to accomplish that: github.event_name != 'push'. That way, you can check if the event is push, so you need to run all, and if not, you can trigger other checks. You can even create conditions to look if github.event_name == 'workflow_dispatch' and then apply conditions regarding your inputs.

Does this seem ok, @arauchberger?

@arauchberger
Copy link
Author

thanks @nikola-jokic for your support/help,

this finally works: if: ${{ github.event_name == 'push' || github.event.inputs.test_skip == 'false' }} (on job.if and job.step.if)
HURRAY!

since is use this expression on multiple steps/jobs in my workflow it still would make sense that workflow-level env's can be used everywhere in the workflow.
reading the workflow get's a lot easier and the risk of typos can be reduced. (see the sample in my first post).

thanks

@nikola-jokic
Copy link
Contributor

Of course @arauchberger, I just wanted to provide a quick solution to your problem, so you can continue your work before the official fix is released.

@leighmcculloch
Copy link

I'm seeing the same problem with env at the workflow level and reusable workflows.

For example, this does not work:

name: Rust

on:
  push:
    branches: [main]
  pull_request:

env:
  CARGO_HACK_ARGS: --feature-powerset --exclude-features default --group-features base64,serde,arbitrary,hex

jobs:
  publish-dry-run:
    if: startsWith(github.head_ref, 'release/')
    strategy:
      matrix:
        sys:
        - os: ubuntu-latest
          target: wasm32-unknown-unknown
        - os: ubuntu-latest
          target: x86_64-unknown-linux-gnu
    uses: stellar/actions/.github/workflows/rust-publish-dry-run.yml@main
    with:
      runs-on: ${{ matrix.sys.os }}
      target: ${{ matrix.sys.target }}
      cargo-hack-feature-options: ${{ env.CARGO_HACK_ARGS }} # 👈 Does not work

@theumannsuncor
Copy link

theumannsuncor commented Dec 20, 2022

Not sure if the "hack" would do for you, but it's working for my situation right now. Basically, I set a workflow.env variable (JSON string in this situation), then I have the first job solely take those variables and turn them into outputs that all the other jobs can injest using the "needs" context.

`
env:
env_vars:
'{
"level1":{
"item1":"abcdef",
"item2":"ghijkl"
}
}'

jobs:
workflow_variables:
runs-on: ubuntu-latest
outputs:
env_vars: ${{ steps.save_workflow_variables.outputs.env_vars }}
steps:
- name: save_workflow_variables
id: save_workflow_variables
run: |
echo "env_vars=${{ toJson(env.env_vars) }}" >> $GITHUB_OUTPUT

test_varaibles:
runs-on: ubuntu-latest
needs:
- workflow_variables
env:
global_env: ${{ needs.save_workflow_variables.outputs.env_vars }}
`

@manas86
Copy link

manas86 commented Dec 31, 2022

any update on this issue ?

@andrea-cassioli-maersk
Copy link

Same problem here


env:
  FOO: foo
  

jobs:
  build:
    name: Build
    uses: ./.github/workflows/build.yaml
    with:
      azureTenantId: ${{ env.FOO}}

fails The workflow is not valid. .github/workflows/build.yaml (Line: --, Col: --): Unrecognized named-value: 'env'. Located at position 1 within expression: env.FOO

@lfarrel6
Copy link

Seeing this with inputs in a reusable workflow.

If it is helpful, we publish a list of contexts and what yaml fields they are available in on the GitHub Docs Website. Currently, we don't support this feature, but I've added the appropriate labels so we may consider it in the future.

The errors seem to directly contradict the docs. Snippet below:

on:
  workflow_call:
    inputs:
      environment:
        required: true
        type: string
name: Publish Result
jobs:
  publish-metrics:
    runs-on: ubuntu-latest
    steps:
      - name: Configure credentials for ${{ inputs.environment }}
        ...

Results in: Error: <REPO_ROOT>/.github/workflows/metrics/action.yml (Line: 29, Col: 15): Unrecognized named-value: 'inputs'.

@1k-off
Copy link

1k-off commented Feb 26, 2023

Hey!

Using env variables in reusable workflows still unavailable. Using like this (see code below) still throws an error.

env:
  FOO: bar
  

jobs:
  build:
    name: Build
    uses: user/repo/.github/workflows/workflow.yml@main
    with:
      some-input: ${{ env.FOO}}

Is there any plans or deadlines to fix this?

@WolfspiritM
Copy link

I'm having a similar issue. I can't get environment vars to the reusable workflow.

I have this workflow:

name: Test

on:
  workflow_dispatch:
  push:
    branches: [ "main" ]
    paths:
    - .github/workflows/test.yml
    - .github/workflows/build.yml
  pull_request:
    branches: [ "main" ]
    paths:
    - .github/workflows/test.yml
    - .github/workflows/build.yml


jobs:
  build:
    uses: ./.github/workflows/build.yml
    with:
      env: ${{ github.event_name != 'pull_request' && 'dev' || null }}
      some-var: ${{ vars.TESTVAR }}
    secrets:
      some-secret: ${{ secrets.TESTSECRET }}

and this reusable workflow:

name: Test Build

on:
  workflow_call:
    inputs:
      some-var:
        required: false
        type: string
      env:
        required: false
        type: string
    secrets:
      some-secret:
        description: 'Some secret'
        required: false

jobs:
  build:
    runs-on: ubuntu-latest
    name: Test for env ${{ inputs.env }} with some-var ${{ inputs.some-var }}
    environment:
      name: ${{ inputs.env }}
    env:
      TESTVARENV: ${{ inputs.some-var || 'some-var was empty' }}
      TESTSECRETENV: ${{ secrets.some-secret || 'some-secret was empty' }}

    steps:
      - name: Test
        run: |
          echo $TESTVARENV
          echo $TESTSECRETENV
          echo Secret: ${{ secrets.some-secret }} inputs.some-var: ${{ inputs.some-var }}
          echo Var direct access: ${{ vars.TESTVAR }} secret direct access: ${{ secrets.TESTSECRET }}
          set

The "dev" environment is set up like this:
image

I get this output from the test stage:

some-var was empty
***
Secret: *** inputs.some-var:
Var direct access: This is a var secret direct access:
...

That means the secret works and it gets send over to the reusable workflow successfully, but the vars is empty.

According to:
https://docs.github.com/en/actions/learn-github-actions/contexts#context-availability

jobs.<job_id>.with.<with_id> | github, needs, strategy, matrix, inputs, vars

The "vars" should be available in this case.

After setting a repository variable it works with the wrong variable. Not using the environment one. I assume it doesn't have access to the environment yet when evaluating the "with" statements, while on the otherhand it seems to have access when using the secrets.

@abdullah-shaikh
Copy link

following, as I have same issue.

@opp-svega
Copy link

+1

Hugoch added a commit to huggingface/text-generation-inference that referenced this issue Feb 2, 2025
Narsil pushed a commit to huggingface/text-generation-inference that referenced this issue Feb 3, 2025
* hotfix: fix trtllm CI build on release

* fix: test release.

* fix: test release.

* fix: test release. env not recognized actions/runner#1661

* fix: test release. Works.
danieldk pushed a commit to huggingface/text-generation-inference that referenced this issue Feb 4, 2025
* hotfix: fix trtllm CI build on release

* fix: test release.

* fix: test release.

* fix: test release. env not recognized actions/runner#1661

* fix: test release. Works.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Actions Feature Feature requires both runner, pipelines service and launch changes context
Projects
None yet
Development

No branches or pull requests

14 participants