How to provide an optional argument with value?

Hello all,

Please take a look at my action.yml:

name: 'Composer (php-actions)'
description: 'Use the Composer CLI in your Github Actions.'
inputs:
  command:
    description: 'Composer command to run'
    required: true
    default: install
outputs:
  time:
    description: 'The time the action was run'
runs:
  using: 'docker'
  image: 'Dockerfile'
  args:
    - ${{ inputs.command }}
    - --no-progress
branding:
  icon: 'package'
  color: 'purple'

The Composer documentation shows how the --working-dir argument can be passed as an optional parameter to change the current working directory. The default is whatever the current working directory is of the shell that’s executing it. 

--working-dir (-d): If specified, use the given directory as working directory.

I would like to include this optional input the my action.yml, but the trouble I’m having is with the “if specified” part. The expected behaviour of this argument is to provide a new working directory only if the argument is passed. There is no default value for this argument.

I can’t seem to work out how to achieve adding an optional argument to the runs.args section. All advice is greatly appreciated. Thanks!

1 Like

I am not very clear about your requirement, could you please share an example in detail ? Such as how you use this action in your workflow yml ? How do you specific the argument?   

I tried to pass an empty input value to runs.args, it will not be accepted when run this action. It is not possible when the argument  has no default value

actionyml arg.png

Ran into the same issue and solved it via a tiny entrypoint script as suggested here: https://help.github.com/en/actions/building-actions/dockerfile-support-for-github-actions#example-entrypointsh-file

This is an old question, but wouldn’t this work? Specify the input parameter as optional, set the current working directory as the default, and then always use the --working-dir option? Like this:

inputs:
  command:
    description: 'Composer command to run'
    required: true
    default: install

  workingdir:
    description: 'Working directory'
    required: false
    default: ${{ github.workspace }}

runs:
  using: 'docker'
  image: 'Dockerfile'
  args:
    - ${{ inputs.command }}
    - --no-progress
    - --working-dir
    - ${{ inputs.workingdir }}