Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 911
E. Betanzos Avatar asked Aug 19 '20 05:08

E. Betanzos


People also ask

Do GitHub Actions need to be on main branch?

Github actions can only trigger on master or the default branch.

How do I manually run an action in GitHub?

On GitHub.com, navigate to the main page of the repository. Under your repository name, click Actions. In the left sidebar, click the workflow you want to run. Above the list of workflow runs, select Run workflow.

What directory do GitHub Actions run in?

GitHub Actions uses YAML syntax to define the workflow. Each workflow is stored as a separate YAML file in your code repository, in a directory named . github/workflows .

How do I run jobs sequentially in GitHub Actions?

To run jobs sequentially, you can define dependencies on other jobs using the jobs. <job_id>. needs keyword. Each job runs in a runner environment specified by runs-on .


2 Answers

According to the official GitHub Actions documentation (About workflow events):

The following steps occur to trigger a workflow run:

  1. An event occurs on your repository, and the resulting event webhook has an associated commit SHA and Git ref.

  2. 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.

  3. 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.

like image 191
E. Betanzos Avatar answered Oct 13 '22 12:10

E. Betanzos


What has worked for me (through trial and error)

  1. Create an empty YAML file in the .github/workflows folder
  2. Create a PR to move that file to your branch
  3. 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/**' 
like image 42
Wouter Van Ranst Avatar answered Oct 13 '22 12:10

Wouter Van Ranst