This repository contains an exampleog how to trigger a github workflow using the PyGithub library. It also contains an example github workflow which echos out an inputted string.
.github/workflows/workflow.yml will create a basic workflow which is triggered on a workflow dispatch and accepts one input named my_input
workflow_trigger.py does the following:
- Retrieves environment variables for the GitHub token, my input, repository name, workflow filename, and branch name.
- Defines a function
trigger_workflowthat:- Creates an instance of GitHub and logs in using the provided token.
- Retrieves the repository object.
- Retrieves the workflow object from the repository.
- Triggers the workflow on the specified branch, passing a string as an input.
- Calls the
trigger_workflowfunction.
- Clone/Fork this repository and make sure it creates a new workflow named "Example Workflow".
- Generate a PAT token with at least read/write scope for Actions.
- Set the following environment variables on your local machine:
GITHUB_TOKEN: Your GitHub PAT token from step 2.MY_INPUT: A simple string, for example "Hello World".REPOSITORY: The name of your repository in the formatowner/repo.WORKFLOW_FILENAME: The filename of your workflow file in the.github/workflowsdirectory.BRANCH: The name of the branch you want to run the workflow on.
- Run the script locally with the command
python workflow_trigger.py.
Profit