skip to Main Content

Is it possible to set the "environment" of a job only if a defined condition is true ? So for example only if it was passed as INPUT ?

jobs:
  test_workflow:
    runs-on: ubuntu-latest
    environment: EXAMPLE <-- This one
    steps:
      - uses: actions/checkout@v3

My use case: Im using a reusable_workflow and i only want to add the environment if it was passed to the workflow.
environment does not accept null/false but only string or object.

Would it work to set the default of the input to an empty string and to "example" in the other call ? What is the consequence of setting it to an empty string ?

Ref : https://docs.github.com/en/actions/using-jobs/using-environments-for-jobs

2

Answers


  1. Chosen as BEST ANSWER

    SOLUTION:

    It is possible to set the ENVIRONMENT to an empty string and it will be treated like its not set. So i defined my INPUT with the empty string default and only pass a value if needed!.

          ENVIRONMENT:
            type: string
            default: ""
    

  2. You can set an If statement at the beginning of a step that can set an environment variable with $GITHUB_ENV. Here is an example:

    jobs:
      test_workflow:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v3
          - name: set environment variable
            if: ${{ inputs.EXAMPLE_INPUT }} == 'true'
            run: |
              echo "EXAMPLE=VALUE" >> $GITHUB_ENV
    

    Then you can access this variable using ${{ env.EAMPLE }}

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search