Photo by Adi Goldstein

Set runtime parameters to your GitHub Actions workflows

Manually trigger your workflows!

Posted by Damien Aicheh on 01/20/2022 · 4 mins

When developing your projects, you may need to create and deploy your application manually.

There are multiples examples where you will need to run a specific workflow on your project:

  • Test a new framework version
  • Run only a part of your workflow
  • Run conditional scripts

In this tutorial we will see how to set runtime parameters and manually trigger your workflows.

Define manual trigger

To be able to trigger a workflow manually we need to add the workflow_dispatch keyword like this:

on:
  workflow_dispatch:

Define input parameters

Let’s take two examples:

  • We need to test if our project run with the last version of the framework (Here we will use Flutter for the purpose of this tutorial but it will works with any framework)
  • When we test a new version of the Framework, we may not need to publish the artifacts generated

These two use cases can be translated as inputs in our workflow like this:

on:
  workflow_dispatch:
    inputs:
      flutter_version:
        description: 'Framework version'
        required: true
        default: '2.5.3'
      publish_artifacts:
        description: 'Publish artifacts'
        required: true
        default: 'true'

As you can see above we have two inputs parameters flutter_version and publish_artifacts with their default values.

As I write this article the definition of booleans in the GitHub Actions is only possible using strings instead of real booleans.

Apply the inputs

Now we can apply these inputs values directly in the jobs:


jobs:
  build_web:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout the code
        uses: actions/checkout@v2

      - name: Set Flutter version
        uses: subosito/flutter-action@v2.2.0
        with:
          flutter-version: '${{ github.event.inputs.flutter_version }}'

      ## Build scripts here (see the complete example on GitHub)

      - if: github.event.inputs.publish_artifacts == 'true'
        name: Publish Artifacts
        uses: actions/upload-artifact@v2
        with:
          name: artifacts
          path: build/web

In the first case, we just pass the Flutter version as an input parameter for the subosito/flutter-action. In the second one we use an if statement to activate our not the publication of our artifacts.

Run the workflow manually

Inside the Actions tabs of your GitHub project, you will see a new line appearing that indicates that you can manually run your workflow:

Manual workflow information

And if you click on the Run workflow button you will see a little window like below:

Run worflow window

You will then be able to change the version of the Framework or disable the publication of your artifacts.

Final Touch

Now you are able to define your own inputs parameters to have more flexible workflows. You will find a complete example in the Github project repository.

Happy coding!

Source:

Do not hesitate to follow me on to not miss my next tutorial!