Triggering GitHub workflow manually

Vinay Sharma
TestVagrant
Published in
3 min readAug 23, 2022

After my previous blog, where I demonstrated getting a workflow triggered automatically upon a code push to the specified branch, I’m back with another one to showcase how can we run the same workflow manually or on-demand.

What is the need of triggering the workflow manually?

Imagine,

  1. You have written a lot of tests & also created a workflow that runs when there is a push to the master branch.
  2. Also, You are developing a very critical feature that will be consumed by many other modules.

So In the above scenario, it is very important to run the test cases & get the results as early as possible.

One Way could be doing all the development and pushing the branch to the master, In that case, the workflow will be triggered and results will be published but imagine in the result you see a lot of failures.

  • Now you need to fix these failures, push again & wait for the result which is time-consuming & double the effort.

Another way could be to run all the test cases manually.

  • This will again cost you a lot of effort & time.

Hence, To avoid all the issues stated above there is a need to have a capability through which you can run the workflow test cases as and when required.

Sounds interesting?? Let us now get into action & see how we can do this.

How to run the workflow manually?

To understand this, We will take the example of the same workflow that we had created in the previous blog.

We just have to add the below lines of code to the YAML file.

name: webdriverio-cion:
workflow_dispatch:
inputs:
environment:
description: 'Environment to run tests against'
type: choice
required: true
options:
- staging
- prod

Understanding the components in the above file

workflow_dispatch: This is an event in GitHub Actions that takes care of running the workflow manually.

inputs: workflow_dispatch also gives an option to take the input at run time that can be used to set the environment before triggering workflow.

environment: This is just an input variable name.

description: Brief detail about the input field.

type: What type of parameter do you want to pass. It can be String, choice, boolean, etc.

required: If set to True. It will mark the field as mandatory and will show it with red * mark

options: Since we are using type as a choice, We need to give options that will be shown in the dropdown.

Note: There are a lot more input parameters available. You can check this link.

After we add the above code, The YAML file will look like this

How to get the variable values that we are passing as the input?

Now, Since we are getting the input values at run time, let's say we want to use these values. How can we get them? We simply have to call them with the below command.

${{ inputs.<variable_name> }}

For example, let’s take the YAML file above and peek into the run command — we are passing an environment variable that we had passed as an input

run: BROWSERSTACK_USERNAME=${{secrets.BROWSERSTACK_USERNAME}} BROWSERSTACK_ACCESS_KEY=${{secrets.BROWSERSTACK_ACCESS_KEY}} npm run test --env ${{ inputs.environment }}

That's it. We are done. Now you will be able to see the Run Workflow option under the Actions page.

So now when you click on the Run workflow button you will be able to see the input options that you have added and you will be able to run the workflow manually.

Thank You! Feel free to reach out to me for any queries.

--

--