Trigger specific branch workflow everytime when PR is created - github

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.

Related

Is it possible to run gitAction workflow on a newly created and randomly named branch?

I'd like to check code formatting when publishing new branch.
My situation is as follows.
I checkout new branch (ex) branch-0XX ) based on develop branch
I added some features and pushed those remotely (branch-0XX)
I liked to check code formatting before requesting pull request to merge branch-0XX into develop branch.
I tried to use Github webhook to trigger a event when a new branch is created and I did it. However, the name of newly created branch is not static and I'm not sure whether it is possible to run jobs based on git action yaml file because I think the name of branch is added on yaml file.
I'm very new in Github and please let me know if you need further information.
You will want to use the push event in combination with branch filters filter out your develop branch.
on:
push:
branches:
- '*'
- '!develop'

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.

Trigger github action on push to multiple branches [duplicate]

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.

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"