how to exclude Master branch on a push event - github

I have a GitHub Action workflow that I want to run on a push to any of the branches in my repository with an exception of 'Master. I don't want the workflow to run on a push to master branch but run on push to other branches in my repository. Please how can I specify that in the workflow?

In your Github Actions workflows file you can specify
name: Deploy staging
on:
push:
branches-ignore:
- master
You can also explicitly include branches you want by doing the following:
name: Deploy staging
on:
push:
branches:
- develop
Changing or adding branches to include the relevant branches that you want. In this manner, workflows will only run to those elected branches.
Here is the relevant section of the Github docs on branches and tags

Related

Trigger GitHub action from another branch when default branch doesn't have the workflow file

I am able to run workflows on GitHub fine. But I don't want my git history to contain anything unrelated to the development of the software, and nothing specific to any one code hosting site, e.g., GitHub. Basically I don't want to add and track .github/workflows/ in my git repo.
Thinking it would work, I created a separate branch, gh-actions, and added the .github/workflows/main.yml there.
on:
workflow_dispatch:
push:
branches:
- master
pull_request:
branches: [ master ]
jobs:
<snip>
But apparently this workflow won't even be registered by GH. So I added gh-actions branch to on: push: branches. After pushing this branch to GH, the workflow was registered (checked with gh workflow list).
Now I went to run it on the default branch and got error.
$ gh workflow run .github/workflows/main.yml --ref master
could not create workflow dispatch event: HTTP 422: Workflow does not have 'workflow_dispatch' trigger (https://api.github.com/repos/3N4N/TwitchRecover/actions/workflows/14900813/dispatches)
Apparently workflow_dispatch event only works if it's in the default branch.
I'm now out of ideas. One final way is to change the default branch to gh-actions so that the workflow can be run on master branch, but that would mean that cloning this repository would by default clone the gh-actions branch, and I would rather not deal with that.
If anyone else has any idea, I'd be glad to try it.

Cannot trigger GitHub Actions while pull request from a fork repo

There is a private repo and have a GitHub Actions.
If I make pull request between branches in this repo, GitHub Actions triggered correctly.
name: CI
on:
pull_request:
branches:
- pre-production
- production
jobs:
build:
runs-on: self-hosted
steps:
- uses: actions/checkout#v3
with:
fetch-depth: 2
...
Another developer who only has read premission fork this repo, make some commits, then pull request to the Upstream. The GitHub Actions doesn't been triggered. I have confirmed that he pull request to the correct branch.
Is there any setting let other developer who only has read premission trigger the action in Upstream?
Updated:
There is a option in repo settings called "Run workflows from fork pull requests" but I cannot enable it.
Finally, I found a setting called "Run workflows from fork pull requests". Enable it will solve the problem.
If the repo is under an organization, we should enabled it in the organization setting. After that, we can enable it in the repo setting.

Is it possible to allow github workflows defined in master branch only?

my team has a github repo with some github workflows set up. I noticed that if someone updates a .github/workflows/[workflow_name].yml file and push it to a dev branch, the updated workflow definition could also be triggered. I am trying to disallow workflows defined in a dev branch from being picked up, so that any un-reviewed workflow definitions in a dev branch would not be triggered. Is there a way to do that?
Eg. if the yml file in the origin master branch is like
on:
pull_request:
branches:
- master
jobs:
[do_something_here]
Then some one updated the yml file to
on:
pull_request:
branches:
- master
jobs:
[do_something_else_instead]
and push the code to a dev branch, and make a PR to master branch, at this point no one was requested to review anything, but the do_something_else_instead would be triggered.
I want to stop such behavior, only yml files in the master branch should be allowed to trigger workflows.
When you define your workflow to be triggered on a pull_request event, you can set a list of branches to be taken into account:
on:
pull_request:
branches:
- master
- 'releases/**'
So you can manage which target branch of a PR your workflow is triggered on.
If you want your workflow not to be triggered until a commit is in the master branch, the event you need is push. With it, you can also set a branches list, in your case your list would contain only 1 branch: master
on:
push:
branches:
- master
So if your workflow has been modified, do_something_else_instead would not be executed until PR is reviewed and merged into master branch.
You can a have look on Github documentation here: https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions
If you need you can set a mix of filter: branches, branches-ignore, paths, paths-ignore, that can be useful :)

Github actions branch filter. Could not use 'release/' preffix on push workflow trigger

When I am pushing a branch with the name pattern "release/**" then for some reason Github action will not be executed:
name: Deploy
on:
push:
branches:
- 'release/[0-9]+.[0-9]+.[0-9]+'
In case I change "release/" to "releases/" it works just great.
Does "release" word is somehow reserved by Github? I could not find an explanation for this in docs.

How to run a GitHub Action from a branch other than master?

I have a repository in GitHub and I want to create an Action to build a Docker image and push it to the DockerHub. I know how to do it but if I create the action in a branch other than master, GitHub does not run it.
This is a known problem (Workflow files only picked up from master?).
Any ideas to fix it?
According to the official GitHub Actions documentation (About workflow events):
The following steps occur to trigger a workflow run:
An event occurs on your repository, and the resulting event webhook has an associated commit SHA and Git ref.
The .github/workflows directory in your repository is searched for workflow files at the associated commit SHA or Git ref. The workflow files must be present in that commit SHA or Git ref to be considered.
For example, if the event occurred on a particular repository branch, then the workflow files must be present in the repository on that branch.
The workflow files for that commit SHA and Git ref are inspected, and a new workflow run is triggered for any workflows that have on: values that match the triggering event.
The workflow runs on your repository's code at the same commit SHA and Git ref that triggered the event. When a workflow runs, GitHub sets the GITHUB_SHA (commit SHA) and GITHUB_REF (Git ref) environment variables in the runner environment. For more information, see "Using environment variables."
Because of this, in order to test the workflows we need to perform a git action (ie. do push) in the created branch.
What has worked for me (through trial and error)
Create an empty YAML file in the .github/workflows folder
Create a PR to move that file to your branch
In your branch, you can now do the necessary edits to get your GH Action up & running. NOTE: next to updating your YAML, you also need to make a change
that actually triggers the workflow (I am using the below trigger, note the absence of the '.github' path trigger).
on:
push:
paths:
- 'path/to/your/code/**'
on:
push:
branches:
- "YOUR-TEST-BRANCH"
pull_request:
branches:
- "main"
paths:
- ".github/workflows/test.yaml"