Trigger github action on push to multiple branches [duplicate] - github

I have created on my main branch with
on:
push:
branches: [ test ]
I have noticed, that while I can trigger it manually, and it will work, it will not actually trigger if I push to test. For that, I needed to create that same action file on test. Now, it seems like I don't even need to have the action on the main branch, is that correct?
So why does even the option so specify the branch that it should trigger on exist? It only triggers on the branches the file exists anyway. That said, I found it frustrating that I have to merge my one file from main to my test branch, is there a way to trigger the action automatically on push even if I do not have it on my test branch, only on main?

No, it's not possible. The order of operations in a workflow run triggered by push or pull request is described in the reference documentation:
An event occurs on your repository. The event has an associated commit SHA and Git ref.
GitHub searches the .github/workflows directory in your repository for workflow files that are present in the associated commit SHA or Git ref of the event.
A workflow run is triggered for any workflows that have on: values that match the triggering event. Some events also require the workflow file to be present on the default branch of the repository in order to run.

Related

Trigger specific branch workflow everytime when PR is created

I'm new to Github action and want to trigger the master branch .github/workflows present in the base branch repo whenever the new PR is created.
Current Code:
on: pull_request: branches: - "**"
Its trigger the workflow code present in head branch and not able to fetch the secret key present in the base branch repo when PR is create from another repo.
I want to trigger the specific branch workflow for every PR created to that repo.
The pull-request trigger for an action runs in the context of the merge commit between the head branch and the base branch. The pull_request_target trigger runs in the context of the base branch of the pull request. That seems like what you want for this action.
However, I should note that a branch on the git repo is probably not the place to store a "Secret key", take a look at Github's secrets feature for securely storing keys for use in actions workflows.

How to make Coogle Cloud Build trigger changes from all commits in branch

We use Google Cloud Build to run different tests for our Github repository when we push a branch. Our problem is that only the last commit in the branch seems to affect which triggers are run.
E.g. say our repository looks like this:
./client/<client code>
./server/<server code>
and in our two triggers we specify the Included files filter like this:
trigger 1: Included files: client/**
trigger 2: Included files: server/**
the problem is that only the last commit in the branch that should trigger builds affects which triggers to run.
To illustrate the problem: say we push a branch with the following commits:
Commit 1: <Changes to client/foo.js>
Commit 2: <Changes to server/bar.scala>
only "trigger 2" is run. And what we want is that both triggers are run, since we want to run tests for all the changes introduced by our branch.
Is there any way to get GCB to "see" all the changes in the branch that is pushed, when deciding which triggers to run? The obvious quick-fix is to create branches with single commits, which makes all the triggers run, but is less than ideal from the perspective of our git workflow.
With the trigger type set to "push to a branch", git is not able to track the changes made to all the branch but only to the last commit as you described.
To accomplish this, you must use Pull Requests to trigger the build. When creating a pull request, git will than be able to track the changes appropriately and your tests will be run as you expected. So change the event that triggers your build to "Pull Request" and than create a pull request from your branch to trigger the build and therefore your tests.

How to test github workflow without merging into master/main branch

I am creating a new git workflow. And just like any other piece of code, I want to test it separately without having to merge it into master first.
This will also help if I have to make few corrections if something doesn't work in the workflow yaml.
Here is the mechanism that I am looking for:
main branch has .github folder which contains all workflows
I create a branch and add my workflow to .github folder
Now I should be able to see(somewhere on Github) workflows from my branch running
When I know that workflows are working fine, I merge my branch in master
Now under github 'Action' tab, new workflows will reflect
Is there a way to do this?
I am actually doing workflow testing all the name, as you can see this test workflow workflow-level-notification is not merged into master branch (ie default branch), and I can still see the workflows in the UI.
Like GuiFalourd said, you can also use act to do the local testing as well. But working directly in the github repo is not that bad. (you can delete the workflow after)
If you would like to test non PR triggerd actions you can simply update your default branch temporarially, run the actions for test, then when you are done switch back.

Github Actions which branch used on PR

I am just getting started with Github actions. In my test workflow I am firing the workflow on PRs to the develop branch.
on:
pull_request:
branches:
- develop
This works fine, but my question is what branch is being built when this runs. Because this action runs before the merge is actually complete (on PR creation) is it just building the source branch? If so, how is that helpful since it isn't taking the changes the PRs code will make to the target branch.
If it is building the target branch it doesnt make sense because the code isn't actually merged yet.
If you check the documentation for the pull_request event it tells you what the environment variables GITHUB_SHA and GITHUB_REF will be for this event.
GITHUB_SHA: Last merge commit on the GITHUB_REF branch
GITHUB_REF: PR merge branch refs/pull/:prNumber/merge
ref: https://docs.github.com/en/actions/reference/events-that-trigger-workflows#pull_request
When you use the official actions/checkout action, these are the settings it uses by default if you don't supply any inputs.
What that means is that by default, pull_request events will checkout a merge commit from the pull request head to the base. This allows you to test against what the source would look like if it was already merged.

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"