When working with GitHub Actions, you often want your workflows to be flexible rather than hard-coded. One powerful way to achieve this is by using custom inputs with the workflow_dispatch event.
The github.event.inputs context allows you to access values that are provided manually at the time of triggering a workflow. This makes your automation dynamic and reusable across different scenarios.
🔹 What is workflow_dispatch?
workflow_dispatch is a trigger that lets you manually run a workflow from the GitHub UI. While triggering it, you can pass custom input values, which can then be used inside the workflow.
🔹 Why use github.event.inputs?
Instead of hardcoding values like environment names or version numbers, you can:
- Provide inputs at runtime
- Make workflows reusable
- Reduce duplication
- Improve control over deployments and scripts
🔹 Example 1: Basic Input Usage
This example demonstrates how to define a simple input and print it during workflow execution.
on:
workflow_dispatch:
inputs:
logLevel:
description: 'Log level'
required: true
default: 'warning'
jobs:
print-log:
runs-on: ubuntu-latest
steps:
- name: Print the input
run: echo "The log level is ${{ github.event.inputs.logLevel }}"
✅ What happens here?
- A user manually triggers the workflow
-
They can enter a value for
logLevel(or use the default) - The workflow prints the selected value
🔹 Example 2: Advanced Inputs with Choices
You can also define more structured inputs, such as dropdowns and optional parameters.
1. Defining Inputs
on:
workflow_dispatch:
inputs:
environment:
description: 'Target environment'
required: true
default: 'development'
type: choice
options:
- development
- staging
- production
db_patch_version:
description: 'Version number for the patch'
required: false
type: string
2. Accessing Inputs in Workflow
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Run Migration
run: |
echo "Deploying to: ${{ github.event.inputs.environment }}"
echo "Applying patch: ${{ github.event.inputs.db_patch_version }}"
✅ What happens here?
- The user selects an environment from a dropdown
- Optionally provides a database patch version
- The workflow uses these inputs during execution
🔹 Key Takeaways
-
github.event.inputsis used to access user-provided values in manually triggered workflows -
Inputs must be defined under
on: workflow_dispatch -
Values are accessed using:
${{ github.event.inputs.<input_name> }} -
Supports multiple types like:
- string
- choice (dropdown)
- boolean (in newer versions)
No comments:
Post a Comment