Manually Trigger GitHub Actions Workflow with Inputs/Arguments
Introduction
GitHub Actions allow developers to automate, customize, and execute their software development workflows in their repositories. Often, there’s a need to manually trigger these workflows and pass in specific inputs or arguments for more control and customization. This guide will show you how to set up your GitHub Actions workflows to manually trigger with inputs/arguments.
Brief Overview of GitHub Actions
GitHub Actions provide a CI/CD platform to define workflows in response to various events in a repository. These events can include committing code, initiating pull requests, creating or deleting branches or tags, and more.
Workflows, defined in YAML files located in the .github/workflows
directory of the repository, detail when and how these workflows should run.
Setting Up a Manually Triggered Workflow with Inputs
Before proceeding, ensure you have a basic understanding of YAML syntax and GitHub Actions. If you’re new to GitHub Actions, you might find the official documentation useful.
Step 1: Create a Workflow YAML File
Begin by creating a new YAML file within the .github/workflows
directory of your repository. This file will contain your workflow configuration. Let’s call it manual-input-workflow.yml
.
1 |
name: Manual Input Workflow |
The name
field sets a name for your workflow, which is displayed on your repository’s actions page.
Step 2: Define the on
Field
Next, you need to define the on
field, which indicates the events that trigger the workflow.
1 2 3 4 5 6 7 |
on: workflow_dispatch: inputs: name: description: 'Person to greet' required: true default: 'GitHub User' |
Here, we’re using the workflow_dispatch
event, which allows workflows to be run manually from GitHub’s UI. Under the inputs
field, we’ve defined an input called name
. This input requires a value (as indicated by required: true
) and defaults to ‘GitHub User’ if no value is provided.
Step 3: Define the jobs
Field
Finally, define what your workflow does using the jobs
field. In this example, we will print a greeting message using the echo
command.
1 2 3 4 5 6 |
jobs: greet: runs-on: ubuntu-latest steps: - name: Greet the person run: echo "Hello, ${{ github.event.inputs.name }}!" |
Here, we’re defining a single job named greet
that runs on the latest version of Ubuntu. The job includes one step that prints a greeting message, utilizing the name
input defined earlier.
The Complete Workflow
After combining all the steps, our complete workflow file looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
name: Manual Input Workflow on: workflow_dispatch: inputs: name: description: 'Person to greet' required: true default: 'GitHub User' jobs: greet: runs-on: ubuntu-latest steps: - name: Greet the person run: echo "Hello, ${{ github.event.inputs.name }}!" |
With this configuration, the workflow can be manually triggered from GitHub’s UI. When triggered, it will prompt for a name, and then print a greeting message using the provided name.
Conclusion
GitHub Actions provides developers with the flexibility to manually trigger workflows and pass in specific inputs or arguments. This ability is especially useful for tasks such as deployment or for running scripts that require specific parameters.
As always, remember that the simplicity or complexity of your workflows is dependent on your project’s needs. GitHub Actions provide a flexible and powerful tool for automating your software development workflows.