GitHub Actions: Action Inputs and Outputs
1. Introduction
GitHub Actions allows developers to automate workflows for their projects. Understanding action inputs and outputs is crucial for creating reusable and dynamic actions.
2. Action Inputs
Inputs are parameters that are passed to an action. They allow users to customize the action's behavior.
2.1 Defining Inputs
Inputs are defined in the action’s metadata file, usually action.yml
.
inputs:
my_input:
description: 'This is a sample input'
required: true
default: 'default_value'
2.2 Accessing Inputs
Inside your action, you can access inputs using the github.action
context.
echo "Input value: ${{ inputs.my_input }}"
3. Action Outputs
Outputs are values that an action can return, which can be used by subsequent actions in a workflow.
3.1 Defining Outputs
Outputs are also defined in the action’s metadata file.
outputs:
my_output:
description: 'This is a sample output'
3.2 Setting Outputs
You can set outputs in your action code. For example, in a shell script:
echo "::set-output name=my_output::Hello, World!"
4. Best Practices
- Always validate inputs to ensure they meet expected formats.
- Clearly document inputs and outputs in the action metadata file.
- Use default values wisely to enhance usability.
- Test actions in isolation to ensure they handle inputs and outputs correctly.
5. FAQ
What if an input is not provided?
If an input is marked as required and not provided, the action will fail with an error.
How can I reference outputs from a previous action?
You can reference outputs using the syntax: ${{ steps.step_id.outputs.output_name }}
.