I want to pass some dynamic parameters and invoke my GitHub Actions workflow manually (ideally via some API). Is this possible?
With the workflow_dispatch event trigger, you can do the manual triggers easily.
Flow:
on:
workflow_dispatch:
inputs:
logLevel:
description: 'Log level'
required: true
default: 'warning'
tags:
description: 'Test scenario tags'
Manual trigger screenshot:
Blog post announcement reference, https://github.blog/changelog/2020-07-06-github-actions-manual-triggers-with-workflow_dispatch/
I think the correct answer to this is using a repository_dispatch NOT a workflow_dispatch.
Only repository dispatch allows you to trigger a workflow from an API call.
Docs
Hand Holding example
Summary:
URL Endpoint:
https://api.github.com/repos/{owner}/{repo}/dispatches
Example cURL call:
curl -X POST -H "Accept: application/vnd.github.v3+json" -H "authorization: Bearer <token>" https://api.github.com/repos/{owner}/{repo}/dispatches -d '{"event_type": "type1","client_payload": {"key1": "Hello from CRUD"}}'
To trigger a workflow_dispatch via API, the documentation can be found at https://docs.github.com/en/free-pro-team#latest/rest/reference/actions#create-a-workflow-dispatch-event
POST /repos/{owner}/{repo}/actions/workflows/{workflow_id}/dispatches
where {workflow_id} can also be the filename of the workflow (which makes things a lot easier).
An example curl from the documentation:
curl \
-X POST \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/octocat/hello-world/actions/workflows/42/dispatches \
-d '{"ref":"ref"}'
Related
Using the github API I am trying to manually start a workflow using:
curl \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: MY_TOKEN" \
https://api.github.com/repos/djpr-data/djprdashdata/actions/workflows/refresh-data.yaml/dispatches
but I keep getting an authentication error:
{
"message": "Must have admin rights to Repository.",
"documentation_url": "https://docs.github.com/rest/reference/actions#create-a-workflow-dispatch-event"
}
This seems to be a similar issue to this question. But my PAT token has all admin and repo scopes selected. I also have my user account setup as admin for the repository and I have added a workflow dispatch to the workflow yaml file.
workflow_dispatch:
inputs:
tags:
description:
"run from cmdline"
I have been following the docs at https://docs.github.com/en/rest/actions/workflows#create-a-workflow-dispatch-event and have had no problems using the API to retrieve all previous workflow jobs. I have also tried the runs and jobs endpoints but get the same error. So I am now not sure what else I can do. Is there somewhere else I need to set permissions?
Thanks
This is a poor error message to tell you that your request is not formed correctly. If you want to pass a PAT as a header, you need to prefix it with token, as described in the docs:
-H "Authorization: token MY_TOKEN"
Once that's resolved, however, you'll also get an error because you don't pass the required ref payload. Assuming your default branch is main, here's a correct curl command:
> export MY_TOKEN=gha_abcdef
> curl \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: token $MY_TOKEN" \
-d '{"ref": "main"}' \
https://api.github.com/repos/djpr-data/djprdashdata/actions/workflows/refresh-data.yaml/dispatches
Is it possible to create a new environment for a repository https://github.com/org/repo/settings/environments using the gh cli?
The only mention of environment I can find in the manual is here https://cli.github.com/manual/gh_secret_set where it says you can assign a secret to an existing environment, but it seems the environment would have to be created manually.
From what I'm seeing here in the gh cli repo that's going to be a no at this time. The issue [linked] is an enhancement request for just that.
I too wanted to know if this could be done, as it's part of my workflow to inject environment secrets.
You can still use the API to create it though.
curl -X PUT \
-H 'Authorization: Bearer ghp_...' \
-H 'Accept: application/vnd.github.v3+json' \
https://api.github.com/repos/<org>/<repo>/environments/<env>
Basically you need to create the environment first, then you can set branch policies:
jq -n '{"deployment_branch_policy": {"protected_branches": false, "custom_branch_policies": true}}'|gh api -H "Accept: application/vnd.github+json" -X PUT /repos/:owner/:repo/environments/dev --input -
gh api --method POST -H "Accept: application/vnd.github+json" "/repos/Oceaneering/it_infra_base_application_bootstrapper/environments/dev/deployment-branch-policies" -f name=dev
I wrote a python script for my use case that uses the gh cli to create environments and can include a branch pattern.
https://gist.github.com/walkerk1980/8a6f6879b32260360854a89bb880a48d
I have a workflow that is triggered when the pull request is labeled (via pull_request_target).
I would to automatically remove the label that triggered the analysis as the last step of that workflow.
How can I do that?
On Github: every pull request is an issue (but not every issue is a pull request).
You can find more informations about this on this stackoverflow answer.
Therefore, you could use the Github API to remove a (specific) label from an issue (or an action doing the same thing) as the last step of your workflow, using the PR number.
Here is the API on Github to remove label from issue (Official Documentation)
If you want to call it directly from the shell in your workflow with curl, it will looks like this:
curl \
-X DELETE \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/<owner>/<repo>/issues/<pr_number>/labels/<label_name>
GuiFalourd does indeed have the correct answer, but I did have to improve on it a bit.
If you want to use the workflow on a private repo, you do need to add an authorization token. Here's an example that uses GitHub context to populate everything besides the label name you want to delete:
curl --silent --fail-with-body \
-X DELETE \
-H 'Accept: application/vnd.github.v3+json' \
-H 'Authorization: token ${{ github.token }}' \
'https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.number }}/labels/YOUR-LABEL'
Also if your label has spaces, use %20 for the whitespace.
I am trying to trigger a GitHub action workflow, which is in my organisations repository and I am the owner of the organsisation
I am trying to trigger using below POST request
curl -H "Accept: application/vnd.github+json" -H "Authorization: token " --request POST --data '{"event_type": "do-something"}' https://api.github.com/repos/USER/REPO/dispatches
But I am getting below message
{
"message": "Not Found",
"documentation_url": "https://docs.github.com/rest/reference/repos#create-a-repository-dispatch-event"
}
my workflow
name: Node.js CI
on:
repository_dispatch:
schedule:
- cron: '5 12 * * 0'
jobs:
build:
runs-on: ubuntu-latest
I think as it not in my account instead it is in organisation account, that is why it might be giving this error.
Any work-around will be helpful.
Generate a token from Github Setting Panel with appropriate privileges and use the following URL
curl -X POST -u <githubusername>:<token> -H "Content-Type: application/json" -H "Accept: application/vnd.github.v3+json" "https://api.github.com/repos/<organization>/<repository_name>/dispatches" -d #test.json
Where test.json contains your json data.
As per the docs
This endpoint here requires write access to the repository by providing either:
Personal access tokens with repo scope. For more information, see "Creating a personal access token for the command line" in the GitHub Help documentation.
GitHub Apps with both metadata:read and contents:read&write permissions.
token - A repo scoped GitHub Personal Access Token.
Validate that your token has full scope of repo.
curl 'https://api.github.com/repos/USER/REPO/dispatches' --request POST \
-H 'Accept: application/vnd.github.everest-preview+json' \
-H 'Content-Type: application/javascript' \
-H 'Authorization: Bearer token' \
--data-raw '{
"event_type": "do-something",
"client_payload": { "text": "my custom text here" }
}'
Is there a way I can post a comment on a Github Pull Request via the command line? The ideas is that I want Jenkins to post comments on Pull Requests with a summary of the results of a script.
This is absolutely possible with nothing more than curl.
curl \
-X POST \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
https://api.github.com/repos/octocat/hello-world/issues/42/comments \
-d '{"body":"Hello Github!"}'
Read more about the API in use here https://docs.github.com/en/rest/reference/issues#create-an-issue-comment
NOTE: This assumes you have a Personal Access Token stored in an environment variable named GITHUB_TOKEN
It's not possible without third party extensions.
You may be intrested in this: https://github.com/stephencelis/ghi
Sorry I can't help more!
This is a little late, but this sounds like exactly what you are looking for:
gh pr comment 6 --body "Hi from GitHub CLI"
https://cli.github.com/manual/gh_pr_comment
Simply allows you to add comments to a pr from a given pr number.