GitHub Actions API to run a workflow (and CLI atop of it)

Hello!

Do you have any plans to extend Github Actions API to have a possibility to run a workflow and pass some parameters/environment variables?

Actually, what I need badly is something similar to the https://docs.drone.io/promote/

Such API allows us to write CLI tool to run workflows.

I understand that this is a complicated task, because we need to have some new type of event to trigger workflows, let say, promotion event and also it will extend the context of workflow.

Real-life scenario for such thing

I have a feature branch and write some code in it.
For each push event to my repo some workflow is executed to build and run tests against my code.

But sometimes I want to deploy my code to some kind of staging environment. What can I do in this situations? Currently I have the separate workflow with jobs.if-condition statement in it which checks the text [deploy-staging] in the commit message, but it is awful solution. The promotion event could solve my problem, because I will use it only when I ready to deploy my code somewhere.

I think repository_dispatch events might provide the functionality that you’re looking for. It sounds like you’d want to clearly define your types and the necessary payload for the type, and then add those add types in the workflow triggers block.

https://help.github.com/en/actions/reference/events-that-trigger-workflows#external-events-repository_dispatch

1 Like

I’ve just come off of a small project where we used manually-triggered workflows for things like deploys and database migrations, and our workflow looked something like this:

on:
  repository_dispatch:
    types: db-migrate

jobs:
  migrate:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - run: |
          script/db-migrate ${{github.event.client_payload.direction}} \
            --count ${{github.event.client_payload.count}} \
            --env ${{github.event.client_payload.env}}

This workflow will run on the “repository_dispatch” event when the type is “db-migrate”—there are also other expected payload values such as “direction”, “count”, and “env” which dictated various aspects of our migrations.

I wrote a small tool, dpx, whose purpose it is to make sending these dispatch events easier:

dpx -r owner/repo -t $github_token db-migrate direction=up count=1 env=prod

Since you asked, you could also set environment variables using this, if need be:

on:
  repository_dispatch:
    types: db-migrate

jobs:
  migrate:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - run: echo "::set-env name=DB_HOST::${{github.event.client_payload.DB_HOST}}"
      - run: |
          script/db-migrate ${{github.event.client_payload.direction}} \
            --count ${{github.event.client_payload.count}} \
            --env ${{github.event.client_payload.env}}

See the ::set-env documentation for more info on that, but the idea is that we’re setting an environment variable whose value is something provided in the repository dispatch payload.

1 Like

Thank you very much for pointing me on this event. I missed it. It looks like what I needed. 

Thank you for sharing your CLI tool and workflow examples!