This article will show how to set Github actions parameters to a value that is based on another parameter's value. This is a very flexible approach for inputs with specific options.

Recently, I had to implement some functionality to a Github actions pipeline, more specifically a "workflow_dispatch" pipeline that can be manually triggered and runs end-to-end tests. We wanted to enable the pipeline user to choose a specific mobile device to run tests against.

However, due to (current) limitations of Github actions, it is not possible to create radio buttons, dropdown boxes or checkboxes so all parameters have to be entered as strings when starting the pipeline.

name: my pipeline
"on":
  workflow_dispatch:
    inputs:
      device:
        description: 'The device to run tests against.'
        required: true
        default: 'iPhone'

This would turn into a UI like this:

Github UI

There are two main problems with this approach:

  • The user has to enter a lot of characters which is error prone
  • The user has to know all possible options

So we decided to index the available options and put them in the description so it is only required to enter a number:

Index

In order to do that, we have to take the value of this field and turn it into the acording string that can be passed to the subsequent pipeline steps.

name: my pipeline

"on":
    workflow_dispatch:
        inputs:  
            device:
                description: '[1] iPhone, [2] Samsung Galaxy'
                required: true
                default: '1'

jobs
    test:
    steps:
        - name: Set device
          id: setDevice
          run: |
            if [[ "${{ github.event.inputs.device }}" == "1" ]]; then
                echo "::set-output name=deviceString::iPhone"
            fi
            if [[ "${{ github.event.inputs.device }}" == "2" ]]; then
                echo "::set-output name=deviceString::Samsung Galaxy"
            fi

The "Set device" step can access the input parameter through the variable ${{ github.event.inputs.device }}. It uses a shell script to compare it to either 1 or 2 and sets the deviceString parameter by a special command line:

echo "::set-output name=deviceString::iPhone"

The nice thing is that you can have multiple if conditions here which is otherwise not easy to achieve using just Github actions syntax.

The following steps of the pipeline can then use the deviceString acordingly by accessing it via the "Set device" id (setDevice):

${{ steps.setDevice.outputs.deviceString }}

Previous Post Next Post