How can I limit workflow_dispatch to a certain branch? [duplicate] - github-actions

When running a github action manually, I can choose which branch to run it against. This seems like a bad idea for some actions. Especially actions along the lines of DeployToProduction - where I only ever want that to be run against the main branch.
Is there any way to restrict it to only run against main - while still making the action manually-triggered?
An example of a manually-triggered action that I have might look something like this:
---
name: DeployToStaging
on:
workflow_dispatch:
jobs:
...

I had the same issue and I solved it with a condition:
if: github.ref == 'refs/heads/master'
steps:
...
in this way the user can see that the action is skipped when selecting a different branch.
https://github.community/t/limit-branch-for-workflow-dispatch/122099/4

Related

GitHub Action not triggering on push

I can't for the life of me figure out why my GitHub Action isn't triggering on push. For context: I have a data file that is updated daily and pushed to the test branch with a timestamp commit message. I am trying to use this timestamp in a Dynamic Badge for my README. Everything works fine when the workflow is run manually (except, of course, I don't get the event data I am hoping to obtain when the action runs on the trigger.)
on:
push:
branches:
- test
paths:
- 'data/Sales.csv'
env:
BADGE_MESSAGE: ${{ github.event.commits[0].message }}
jobs:
create-badge-test:
runs-on: ubuntu-latest
steps:
- name: Create Dynamic Badge
uses: schneegans/dynamic-badges-action#v1.1.0
with:
auth: ${{ secrets.GIST_PAT }}
gistID: 0123456789 #Not actual gist ID
filename: test.json
label: Last Refresh
message: $BADGE_MESSAGE
color: orange
And yes, I've tried putting the branch name in quotes and updating the paths: to - '**.csv' and the action still does not trigger.
There must be something else wrong - all you have here in this workflows is just fine.
You can see it working here:
https://github.com/grzegorzkrukowski/stackoverflow_tests/actions/runs/1860382530
For this commit:
https://github.com/grzegorzkrukowski/stackoverflow_tests/commit/f3f8dd20fa780746441c7b6623b6a2a9929aa70d
It's exact copy of workflow from your question.
I would expect you are pushing a file with different name - keep in mind some systems will be case-sensitive - so pushing data/sales.csv won't work properly.
Another idea is that you pushing it to wrong branch or you have wrong path to the file.
You have to push to test branch and with data/Sales.csv - it only triggers workflow if both are true.
Short answers is - workflow is fine as it is - no brackets needed.
I could help more seeing the repository with this workflow and exact commits being done.
The action is not running because you also need to satisfy the paths condition as explained on GitHub docs
Note: If you use both the branches filter and the paths filter, the workflow will only run when both filters are satisfied.
If you want the action to run when you push to test you have to remove the paths condition
on:
push:
branches:
- 'test'

Determining the origin repo of pull request in workflow YAML

I am modifying a workflow file so that a job doesn't run when a pull request originates from another repo.
So, I am looking for something like this:
build_and_deploy_job:
if: github.repository == github.pullrequest.repo
github.pullrequest.repo is just something I made up, but the idea would be that it would return the (full) name of the repo where the pull request came from.
I've tried outputting the environment variables to see if they are somehow different if a PR comes from a different branch in the same repo, or from a different repo. Nothing stood out to me as being useful.
Is something like that possible?
Background: I am trying to avoid a build job failure because it can't access a repo secret during a pull_request event when the pull request comes from another repo.
Use the following to check if the PR is coming from the same repo. This only works when the workflow is triggered by a pull request.
on: [pull_request]
jobs:
build_and_deploy_job:
if: github.event.pull_request.head.repo.name == github.repository
Bonus: Use this to check if the repo is a fork.
on: [pull_request]
jobs:
build_and_deploy_job:
if: github.event.pull_request.head.repo.fork == true
Source: https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#webhook-payload-object-34

Trigger a GitHub action on pull request approval and path

I want to build a GitHub Action that triggers on Pull Request (PR) Approval, but only when the PR contains modification to a particular path.
Currently, I have the following implementation:
on:
pull_request_review:
types: [submitted]
paths: ['mypath/**']
jobs:
build:
runs-on: self-hosted
steps:
- uses: actions/checkout#v2
- name: job name
if: github.event.review.state == 'approved'
[Reference: https://github.community/t/github-actions-manual-trigger-approvals]
However, the build job triggers on Approval, and seems to ignore the path. The build runs on any Approval regardless of what files have been modified in the PR.
Is it possible to trigger a GitHub Action only when a PR modifies a particular path and it is approved?
I know fuzzi found an alternative using another solution with labels, but here is my contribution if someone wants to resolve the question issue keeping the original premisses:
Context
The ON trigger conditions work as OR and not as AND. Therefore, in the question sample, the workflow will trigger when a pull_request_review is submitted OR when one of the updated file(s) path is the one informed.
Workaround
It's not possible to check both condition (yet?) through the on workflow level field alone.
Therefore, if you want to check both conditions, you would have to do it separately.
A solution could be to check the submitted PR in the ON trigger first, then checking the folder path in a job step.
Example
Here is an example of what the solution suggested above looks like using the paths-changes-filter action:
on:
pull_request_review:
types: [submitted]
jobs:
build:
runs-on: self-hosted #or any other runner
steps:
- uses: actions/checkout#v2
- uses: dorny/paths-filter#v2
id: changes
with:
filters: |
mypath:
- 'mypath/**'
# run only if some file in 'src' folder was changed
- if: steps.changes.outputs.mypath == 'true' && github.event.review.state == 'approved'
run: ...
I found a solution to this using GitHub Labels instead, rather than file paths.
I implemented a GitHub Action for automatically adding a Label to the PR, based on the updated file paths.
(https://github.com/actions/labeler)
I then modified my existing GH Action to check the Label value. My condition is now:
if: github.event.label.name == 'project' && github.event.review.state == 'approved'

Is there a way to restrict what branch an action can be run on?

When running a github action manually, I can choose which branch to run it against. This seems like a bad idea for some actions. Especially actions along the lines of DeployToProduction - where I only ever want that to be run against the main branch.
Is there any way to restrict it to only run against main - while still making the action manually-triggered?
An example of a manually-triggered action that I have might look something like this:
---
name: DeployToStaging
on:
workflow_dispatch:
jobs:
...
I had the same issue and I solved it with a condition:
if: github.ref == 'refs/heads/master'
steps:
...
in this way the user can see that the action is skipped when selecting a different branch.
https://github.community/t/limit-branch-for-workflow-dispatch/122099/4

How can I not execute a GitHub Action when I push a new tag?

I want that my GitHub Action that runs all tests doesn't execute when we push a new tag to the master repository because when we merge new branches we execute this action before, and when we push a tag to create a new release this action executes again.
Right now this action starts with this
name: Build and Test
on: push
As you see this will execute in every push we would make, and I want to change that this action will execute only when we push commits, no tags. My approximate solution would be this but I am not sure
name: Build and Test
on:
push:
branches:
- '*'
tags-ignore:
- '*'
That should work, but I'd recommend two improvements.
First, use ** instead of * (the edge case here is a branch/tag named /).
Second, omit the tags-ignore clause. In my testing, Github ignores all tags if you specify branches but not tags or tags-ignore.
name: Build and Test
on:
push:
branches:
- '**'