57

I am doing npm test when pull requests are raised using Github actions.

  • Now if tests fail,
  • I would like to add a message,
  • Or at least, reject the PR using actions.

How can I do this?

Top-Master
  • 7,611
  • 5
  • 39
  • 71
Janier
  • 3,982
  • 9
  • 43
  • 96

2 Answers2

82

I assume you have an on: pull_request workflow that runs npm test. This should automatically create a GitHub Check on the pull request that will fail if your tests fail. The best way to "reject" the pull request is to prevent it from being merged unless the tests pass. You can do this by turning on a setting in your repository to "Require status checks to pass before merging," and selecting your workflow as required.

You can find this setting under your repository's Settings > Branches. require status checks

There are further details about these settings in the documentation. https://help.github.com/en/github/administering-a-repository/enabling-required-status-checks

peterevans
  • 34,297
  • 7
  • 84
  • 83
  • 38
    Import to note that branch protections are not available in private repos unless you have github pro – Tomer Shemesh Nov 14 '19 at 13:33
  • 8
    Would you happen to know why an `on: pull_request` workflow wouldn't show up on the list? The workflow runs whenever a new pr is created, but the merge can be done whether it runs successfully or not. ‍♂️ – Crono Sep 15 '21 at 17:40
  • 5
    @Crono make sure you are searching using the job-id(job name in the yml file) – Farhan Haider Jan 11 '22 at 14:01
  • 2
    In my case, the path to YAML does not show up as the suggestions. Only the names of the individual jobs show up. That is, I want a whole workflow as a status check, but I can only add the individual jobs of the workflow one-by-one. – Utku Jan 19 '22 at 17:07
  • By doing this, wouldn't the tests and GitHub Check be applied to the source branch (e.g feature branch) only? This would prevent the target branch from getting merged with a broken source branch, but I'm missing a way to prevent the result of the merge from being broken. The workflow I had imagined is something like: 1. on merge, run tests 2. if tests pass then make a deployment, else revert the merge so that the branch doesn't remain broken. – ballcue Jul 21 '22 at 13:51
  • 3
    if no actions show up follow this: https://stackoverflow.com/a/68613319/3692155 – Castaldi Aug 04 '22 at 14:26
  • 1
    @peterevans how did you add a workflow as the status check?, all I can see are the job names – Dilep Dev May 30 '23 at 04:03
  • Is it possible to specify all actions because this way requires manual update if a job is added? – ddsultan Jun 14 '23 at 16:29
3

in status checks bar, type the action name you have defined in your workflow yml file, then merge will check your defined workflow action

gituser
  • 31
  • 1