Github action how to access the inputs
Asked Answered
S

2

6

enter image description here

on: 
  workflow_dispatch:
    inputs:
      logLevel:
        description: 'Log level'     
        required: true
        default: 'warning'
      tags:
        description: 'Test scenario tags'

I have the following inputs:

  • Which branch do I want to use
  • Input fields

How do I access the information that I enter and then use it later in the github action script?

Sprage answered 9/3, 2022 at 13:14 Comment(0)
B
20

You now (June 2022) have an alternative:

You could try and take advantage of a new feature (June 2022)

GitHub Actions: Inputs unified across manual and reusable workflows

Workflows triggered by workflow_dispatch and workflow_call can now access their inputs using the inputs context.

Previously workflow_dispatch inputs were in the event payload.
This made it difficult for workflow authors who wanted to have one workflow that was both reusable and manually triggered.

Now a workflow author can write a single workflow triggered by workflow_dispatch and workflow_call and use the inputs context to access the input values.

For workflows triggered by workflow_dispatch, inputs are still available in the github.event.inputs context to maintain compatibility.

Using the inputs context in GitHub Actions.

In your case:

jobs:
  printInputs:
    runs-on: ubuntu-latest
    steps:
    - run: |
        echo "Log level: ${{ inputs.logLevel }}"
        echo "Tags: ${{ inputs.tags }}" 

Is it possible to avoid printing the inputs in the logs?

To avoid printing the inputs in GitHub Actions logs, you can still access the inputs but manipulate them in a way that prevents them from being directly output to the logs.

FOr instance, you can set the inputs as environment variables and then use them in your scripts. Environment variables are not automatically printed in logs.

GitHub Actions has a feature to mask values in logs. You can use the ::add-mask:: command to mask a value. Once a value is masked, it will be replaced with *** in the logs.

jobs:
  processInputs:
    runs-on: ubuntu-latest
    steps:
      - name: Set up environment variables
        run: |
          echo "LOG_LEVEL=${{ inputs.logLevel }}" >> $GITHUB_ENV
          echo "TAGS=${{ inputs.tags }}" >> $GITHUB_ENV

      - name: Run script with inputs
        run: |
          ./your-script.sh
        env:
          LOG_LEVEL: ${{ secrets.LOG_LEVEL }}
          TAGS: ${{ secrets.TAGS }}

Here, the inputs are set as environment variables. These variables are then passed to a script (your-script.sh) which can use these values internally.
If necessary, you can mask sensitive inputs using ::add-mask::.

Another option: You can pass the inputs to a script and process them inside the script. That way, the actual values will not be directly printed in the logs.

Bibliographer answered 10/6, 2022 at 14:45 Comment(0)
A
5

UPDATED ANSWER CAN BE FOUND HERE


To use an input from the workflow_dispatch trigger on your yaml file, you need to use the following syntax ${{ github.event.inputs.<input_name> }} or ${{ inputs.<input_name> }}.

For example, in your case:

on: 
  workflow_dispatch:
    inputs:
      logLevel:
        description: 'Log level'     
        required: true
        default: 'warning'
      tags:
        description: 'Test scenario tags'

jobs:
  printInputs:
    runs-on: ubuntu-latest
    steps:
    - run: |
        echo "Log level: ${{ github.event.inputs.logLevel }}"
        echo "Tags: ${{ github.event.inputs.tags }}" 

Here is a simple demo if you want to check:

You can also check the official documentation about it.

Ahders answered 9/3, 2022 at 14:49 Comment(3)
To know which branch is used?Sprage
When you inform the input (as on the screen you shared in your question), you can also select the branch in which the workflow will run against.Ahders
No need for github.event.inputs, inputs also works.Lexicostatistics

© 2022 - 2025 — McMap. All rights reserved.