Run a GitHub Action only when someone opens a PR - github

Is there a way to run Github-Actions only when PR Opens.
I know there is a possibility to run workflow with on = "pull_request". However it means the workflow will be triggered when someone opens PR, comments it, review and close. I would like to run it only once, when someone opens PR. I tried ENV_VAR but no luck. Perhaps events might be the way to go? https://developer.github.com/webhooks/#events

Run a workflow when a Pull Request is Opened:
on:
pull_request:
types: [opened]
I think this should work.

https://github.com/actions/bin/tree/master/filter#actor can be useful to filter before your action.

Related

Triggering a workflow run after an action commits

In a repository we have :
a workflow that runs on: pull_request
so it runs each time I open/ commit to the pull-request (which I opened)
we have an action (runs manually) that commits to the pull-request (updating some files in the branch)
when the second action finishes to run, I can see its commit on the pull request
but it doesn't trigger the first workflow to run again
(in order to run the workflow after the action's commit, I need each time to insert a dummy commit or close and re-open the pull-request)
Is there another way to trigger the first workflow after a "bot" from the 2nd github action commits?
Don't do that. GitHub generally "dislike" having workflows trigger other workflows, for the obvious reason.1 Instead, write a reusable workflow, then use and re-use it.
See also Github Actions - trigger another action after one action is completed.
1If the reason isn't obvious, see this question. Follow the link until it becomes obvious why this is a bad idea. (In Computer Science, see the definition of recursion. In Philosophy, a closely related idea is called Begging the Question.)
You could add push trigger as well to the workflow as follows:
on:
- push
- pull_request
It should then run the workflow when you push a commit or tag. See docs here.

How do I trigger a github action from the target branch on incoming pull-request reviews?

I'm using GitHub actions to automate pull requests to my repositories. In particular, I'd like to run actions whenever a pull request gets a review to automatically label it. Because of this, I'd like to run code from the context of my base branch (similarly to how pull_request_target works as a counterpart to pull_request).
I've looked at the events, but there doesn't seem to be a pull_request_review_target or any similar events that would be the counterpart to pull_request_review.
And what's about submitted on pull_request_review
on:
pull_request_review:
types: [submitted]
Or maybe I misunderstood you

How to link a GitHub Actions manual run to a PR

For a variety of reasons, I need to manually trigger a GitHub Actions run from a comment on a PR mentioning a bot (I’m using ProBot). I figured out how to start the workflow by setting the start to on: workflow_dispatch and calling the API. Where I’m running into an issue is linking the run to the PR. Right now, the action just starts and completes without ever appearing in the checks section of the PR.
I noticed that there is a checks create method on the API, but it seems more geared towards making your own check suite. I could use that to create a check run, manually watching the GitHub Actions process, and appropriately updating the check run, but it seems like overkill. I haven’t seen anything in The API that would allow this to happen. There may be a way to do it from the action itself too, but I haven’t found anything.
I don't think you can use workflow_dispatch to add/update checks on a PR. This seems to be confirmed by this response to a similar question on the community forums.
Checks are only added/updated for the following events:
pull_request
pull_request_review
pull_request_review_comment
pull_request_target
push
So your manual operation needs to trigger one of these events to run. There are probably a number of different ways you can do this, depending on your use case. Just as an example, you could call the API to add a label and allow a pull_request workflow to execute on that type.
on:
pull_request:
types: [labeled, opened, synchronize, reopened]
The other thing to note is that the API call (or git push) must use a PAT instead of GITHUB_TOKEN. This is to allow further workflows to execute.
It is possible with some workaround. First, you have to identify the PR that invoked your workflow. If you need to use the workflow_dispatch trigger event, you can pass this PR number as input parameter. Otherwise, you mentioned you trigger this workflow on a specific comment so you could also use the issue_comment event which will give you the PR number as github.event.issue.pull_request.
Next, you have to find out the latest commit of this Pull Request. This depends on how your workflow got invoked:
if you use the issue_comment event, you can use the xt0rted/pull-request-comment-branch action to determine the right branch and commit
if you use the workflow_dispatch event, you can use the actions/github-script action to run some query to get the right commit for a given PR number
Finally, you can use the myrotvorets/set-commit-status-action action to attach the workflow result as check on the latest commit of the PR.
I wrote a blog post that describes this process in some more details: Trigger GitHub Workflow for Comments on Pull Request

How can I run some code in github actions when the job is complete whether it passed or failed?

I think what I want is to trigger an event on checkrun.completed, but that event doesn't seem to fire...
What I'm trying to achieve is sending a slack notification when a specific job fails.
How can I run some code in github actions when the job is complete whether it passed or failed?
Found an answer, I'm looking for these on the step
if: failure()

Is there a way to push changes with a GitHub action?

This question refers to this: https://github.com/features/actions
I have written a GitHub action to build my code and create a production bundle. I'd like to have that included into my repository by committing the changes to origin/master. This seems like an obvious feature for GitHub actions to have but I can't find it anywhere. How do I commit changes with a GitHub action and push them?
Update: Please see the following question/answer for full details about how to push changes back to the remote.
Push to origin from GitHub action
An alternative option is create-pull-request action. It will automatically commit changes to a new branch and raise a pull request for you to review the changes. I wrote a detailed explanation about it as an answer to another question here:
https://stackoverflow.com/a/58004257/11934042
I found the stefanzweifel/git-auto-commit-action to be the best solution. It's really as easy as:
- uses: stefanzweifel/git-auto-commit-action#v4
One could, of course, tune the parameters, like commit message, file globs and many others.
The easiest way to do this is to just integrate this action: https://github.com/cds-snc/github-actions/tree/master/auto-commit
It's a little frustrating because it has a default name/email that's ridiculous so you have to fork it to fix it but it otherwise works well.